我写了这个函数。我试图让它输出 mp3 文件,但它不工作。我不断收到此错误。我曾尝试将 .mp3 放在“output_filepath”或“%(ext)s”上,但无济于事。
def start_extraction(url, output_file):
output_filepath = 'music/%s.mp3' % output_file
temp_filepath = 'temp/%s-%s.%s' % (uuid.uuid4(), output_file, '%(ext)s')
ydl_opts = {
'format': 'bestaudio/best', # choice of quality
'extractaudio' : True, # only keep the audio
'audioformat' : 'mp3', # convert to mp3
'outtmpl': temp_filepath, # name the location
'noplaylist' : True, # only download single song, not playlist
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
result = ydl.download([url])
#
if result == 0:
# Move the temporary file to the proper location.
shutil.move(temp_filepath, output_filepath)
return result
所以我得到这个错误
[youtube] TZB8HRrJ0KM: Downloading webpage
[youtube] TZB8HRrJ0KM: Extracting video information
[youtube] TZB8HRrJ0KM: Downloading DASH manifest
Done downloading, now converting ...
05:54:31 IOError: [Errno 2] No such file or directory: u'temp/61499bc8-0c2e-4719-8556-826af80fd028-How_To_Shave_Your_Beard_Like_A_Man.%(ext)s'
Traceback (most recent call last):
File "/vagrant/yout/env/lib/python2.7/site-packages/rq/worker.py", line 557, in perform_job
rv = job.perform()
File "/vagrant/yout/env/lib/python2.7/site-packages/rq/job.py", line 492, in perform
self._result = self.func(*self.args, **self.kwargs)
File "/vagrant/yout/worker.py", line 100, in extract_audio
extraction_result = start_extraction(url, audio_filename)
File "/vagrant/yout/worker.py", line 130, in start_extraction
shutil.move(temp_filepath, output_filepath)
File "/usr/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
File "/usr/lib/python2.7/shutil.py", line 130, in copy2
copyfile(src, dst)
File "/usr/lib/python2.7/shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: u'temp/61499bc8-0c2e-4719-8556-826af80fd028-How_To_Shave_Your_Beard_Like_A_Man.%(ext)s'
Traceback (most recent call last):
File "/vagrant/yout/env/lib/python2.7/site-packages/rq/worker.py", line 557, in perform_job
rv = job.perform()
File "/vagrant/yout/env/lib/python2.7/site-packages/rq/job.py", line 492, in perform
self._result = self.func(*self.args, **self.kwargs)
File "/vagrant/yout/worker.py", line 100, in extract_audio
extraction_result = start_extraction(url, audio_filename)
File "/vagrant/yout/worker.py", line 130, in start_extraction
shutil.move(temp_filepath, output_filepath)
File "/usr/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
File "/usr/lib/python2.7/shutil.py", line 130, in copy2
copyfile(src, dst)
File "/usr/lib/python2.7/shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: u'temp/61499bc8-0c2e-4719-8556-826af80fd028-How_To_Shave_Your_Beard_Like_A_Man.%(ext)s'