Youtube-dl 无法在 mp3 中输出

Youtube-dl trouble outputting in mp3

这个函数是我写的。我试图让它输出 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'

你弄错了变量名,output_file应该换成output_filepath

temp_filepath = 'temp/%s-%s.%s' % (uuid.uuid4(), output_file, '%(ext)s')

这一行导致了问题。字符串格式不正确。

temp_filepath = 'temp/%s-%s' % (uuid.uuid4(), output_filepath)

temp_filepath = 'temp/%s-%(title)s.%(ext)s' % uuid.uuid4()

temp_filepath = 'temp/%s-%s.%(ext)s' % (uuid.uuid4(), output_filepath)

好的,经过一段时间的调试。我发现该文件被保存为 m4a。还需要替换变量,因为它发生了变异。因此,对于在 iTunes 上使用 youtube-dl 的任何人来说,这里是我能够想出的解决方案。

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