Python Youtube dl无法在mp3中输出

Python Youtube dl无法在mp3中输出,python,youtube-dl,Python,Youtube Dl,我写了这个函数。我试图让它输出mp3文件,但它不工作。我一直在犯这个错误。我尝试过将.mp3放在“output\u 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)

我写了这个函数。我试图让它输出mp3文件,但它不工作。我一直在犯这个错误。我尝试过将.mp3放在“output\u 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'

变量名错误,
output\u file
应替换为
output\u filepath

temp_文件路径='temp/%s-%s.%s'(uuid.uuid4(),输出文件,%(ext)s')

这条线导致了问题。字符串格式不正确

temp\u文件路径='temp/%s-%s'(uuid.uuid4(),output\u文件路径)

temp\u filepath='temp/%s-%(title)s.%(ext)s'%uuid.uuid4()


temp\u filepath='temp/%s-%s.%(ext)s%%(uuid.uuid4(),output\u filepath)
调试一段时间后,确定。我发现文件被保存为m4a。还需要替换变量,因为它发生了变异。因此,对于那些将youtube dl与itunes结合使用的人来说,这里是我能够想到的解决方案

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
    'postprocessors': [{
      'key': 'FFmpegExtractAudio',
      'preferredcodec': 'mp3',
      'preferredquality': '192',
      }],
    '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.
        temp_filepath = temp_filepath.replace('.%(ext)s', ".mp3")
        shutil.move(temp_filepath, output_filepath)

    return result

我自己没有使用过这个库,但是如果你看一下,它肯定支持文件名中的
%(ext)s
之类的模板它可以工作,但文件无法在itunes中播放,但这是另一个问题我想你为什么拒绝回答?实际上你更新的答案没有一个选项起作用:-/它们给出了一个映射错误。是的,很奇怪,你得到的答案实际上没有使用没有映射错误的%(ext)s。然后这导致了错误,所以我不得不说它是不正确的。对不起,谢谢你到目前为止的帮助。