Youtube-dl麻烦输出mp3

时间:2015-05-20 06:02:45

标签: python youtube-dl

我写了这个函数。我想让它输出mp3文件,但它不起作用。我一直收到这个错误。我已经尝试过将.mp3放在" output_filepath"或"%(分机)s"没有用。

[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'

所以我收到了这个错误

if(userchoice!="..." && userchoice!="...") 
console.log(...)

2 个答案:

答案 0 :(得分:2)

好的,经过一段时间的调试。我发现文件被保存为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

答案 1 :(得分:0)

您的变量名称错误,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)