Mac 上 ffprobe/avprobe 和 youtube_dl 的 Python 问题

Posted

技术标签:

【中文标题】Mac 上 ffprobe/avprobe 和 youtube_dl 的 Python 问题【英文标题】:Python problems with ffprobe/avprobe and youtube_dl on Mac 【发布时间】:2017-08-19 08:58:46 【问题描述】:

我昨天开始自学 Python,如有任何礼仪错误,请提前道歉。我的挑战是获取歌曲名称的 .csv 列表,在 YouTube 上搜索每个视频,然后使用 youtube-dl 下载音频。我的代码似乎适用于一个输出文件,但是由于我无法解决的原因,循环结束了。主要错误是DownloadError: [0;31mERROR:[0m ffprobe or avprobe not found. Please install one. 但我已经安装了这些(据我所知)。我已经尝试并试图解决这个问题,但没有任何基本知识我会淹死。我正在使用 python 2.7.13。有什么建议吗?对不起,丑陋的代码!

主要功能:

import csv
import youtube_extract

#concatenate column elements of each row:
with open('Songs for wedding test.csv') as f:
    reader = csv.reader(f)
    with open('Songs for wedding test output.csv', 'w') as g:
        writer = csv.writer(g)
        for row in reader:
            new_row = [' '.join([row[0], row[1]])] + row[2:]
            writer.writerow(new_row)

with open('Songs for wedding test output.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=';')
    #Make into a list:
    data = list(spamreader)
    #Convert into usable strings:
    data2=data
    for i, item in enumerate(data): 
        data2[i] = str(data[i])
        youtube_extract.extract_download(data2[i])    
    print data2

它使用的函数,youtube_extract.py:

#!/usr/bin/env python2.7

from __future__ import unicode_literals
import sys; sys.path
sys.path.append('/usr/local/bin/youtube-dl')
sys.path.append('/usr/local/Cellar/ffmpeg/3.2.4')
import os
import youtube_dl
import urllib
import urllib2
import bs4
from bs4 import BeautifulSoup


def extract_download(textToSearch):
    #Get rid of square brackets and quotes:
    query = urllib.quote(str(textToSearch).replace('[','').replace(']','').replace('"','').replace('"',''))
    url = "https://www.youtube.com/results?search_query=" + query
    response = urllib2.urlopen(url)
    html = response.read()
    soup = BeautifulSoup(html)
    for a in soup.findAll(attrs='class':'yt-uix-tile-link', limit=1):
        vidid = 'https://www.youtube.com' + a['href']
        print vidid

    os.chdir('/Users/smurfo/Music/Wedding songs')
    ydl_opts = 
        'format': 'bestaudio/best',
        'download_archive': 'downloaded_songs.txt',
        'verbose': True,
        'outtmpl': 'Wedding songs/%(title)s.%(ext)s',
        'postprocessors': [
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        ],
    
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([vidid])

    os.chdir('/Users/smurfo/Dropbox/Coding/Python learning')

以及 shell 窗口中的完整输出。第一个 mp3 被保存,但随后循环结束,我的屏幕被红色错误淹没。

================================== RESTART: /Users/smurfo/Dropbox/Coding/Python learning/csv reading.py ==================================

Warning (from warnings module):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/beautifulsoup4-4.5.3-py2.7.egg/bs4/__init__.py", line 181
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 1 of the file <string>. To get rid of this warning, change code that looks like this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "html.parser")

https://www.youtube.com/watch?v=7fkOqXAHLKQ
[debug] Encodings: locale US-ASCII, fs utf-8, out us-ascii, pref US-ASCII
[debug] youtube-dl version 2017.03.24
[debug] Python version 2.7.13 - Darwin-16.4.0-x86_64-i386-64bit
[debug] exe versions: none
[debug] Proxy map: 
[youtube] 7fkOqXAHLKQ: Downloading webpage
[youtube] 7fkOqXAHLKQ: Downloading video info webpage
[youtube] 7fkOqXAHLKQ: Extracting video information
[youtube] 22 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 43 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 18 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 36 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 17 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 136 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 247 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 135 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 244 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 134 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 243 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 133 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 242 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 160 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 278 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 140 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 171 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 249 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 250 signature length 43.40, html5 player en_US-vflEzRdnB
[youtube] 251 signature length 43.40, html5 player en_US-vflEzRdnB
[debug] Invoking downloader on u'https://r5---sn-fpoq-hm2z.googlevideo.com/videoplayback?dur=202.950&lmt=1449647638777560&ip=151.18.103.48&key=yt6&id=o-AO0F6ZMx0LP0Swm_iDiWZtFd0HZozItNeM1bUmT7Q2Ey&itag=171&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Crequiressl%2Csource%2Cupn%2Cexpire&upn=jTIcpTuU5gc&mime=audio%2Fwebm&pcm2cms=yes&pl=17&source=youtube&gir=yes&ipbits=0&initcwndbps=838750&keepalive=yes&clen=3160765&mn=sn-fpoq-hm2z&mm=31&requiressl=yes&expire=1490574223&ms=au&ei=LwfYWNH4E4bLWcuTpcgK&mv=m&mt=1490552546&signature=10CC47D7073DC66C89435916227A67196D22DA2D.D55F1282DFF0CC8992D3680B6220F5FC5078BD72&ratebypass=yes'
[download] Destination: Wedding songs/Parov Stelar - Jimmys Gang (Official Video).webm

[K[download]   0.0% of 3.01MiB at 10.26KiB/s ETA 05:00
[K[download]   0.1% of 3.01MiB at 20.50KiB/s ETA 02:30
[K[download]   0.2% of 3.01MiB at 36.44KiB/s ETA 01:24
[K[download]   0.5% of 3.01MiB at 65.40KiB/s ETA 00:46
[K[download]   1.0% of 3.01MiB at 111.40KiB/s ETA 00:27
[K[download]   2.0% of 3.01MiB at 192.12KiB/s ETA 00:15
[K[download]   4.1% of 3.01MiB at 334.31KiB/s ETA 00:08
[K[download]   8.3% of 3.01MiB at 543.36KiB/s ETA 00:05
[K[download]  16.6% of 3.01MiB at 748.14KiB/s ETA 00:03
[K[download]  33.1% of 3.01MiB at 940.02KiB/s ETA 00:02
[K[download]  66.3% of 3.01MiB at  1.22MiB/s ETA 00:00
[K[download] 100.0% of 3.01MiB at  1.43MiB/s ETA 00:00
[K[download] 100% of 3.01MiB in 00:02
[0;31mERROR:[0m ffprobe or avprobe not found. Please install one.
Traceback (most recent call last):
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 1930, in post_process
    files_to_delete, info = pp.run(info)
  File "/usr/local/bin/youtube-dl/youtube_dl/postprocessor/ffmpeg.py", line 239, in run
    filecodec = self.get_audio_codec(path)
  File "/usr/local/bin/youtube-dl/youtube_dl/postprocessor/ffmpeg.py", line 154, in get_audio_codec
    raise PostProcessingError('ffprobe or avprobe not found. Please install one.')
PostProcessingError: ffprobe or avprobe not found. Please install one.


Traceback (most recent call last):
  File "/Users/smurfo/Dropbox/Coding/Python learning/csv reading.py", line 26, in <module>
    youtube_extract.extract_download(data2[i])
  File "/Users/smurfo/Dropbox/Coding/Python learning/youtube_extract.py", line 39, in extract_download
    ydl.download([vidid])
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 1884, in download
    url, force_generic_extractor=self.params.get('force_generic_extractor', False))
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 772, in extract_info
    return self.process_ie_result(ie_result, download, extra_info)
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 826, in process_ie_result
    return self.process_video_result(ie_result, download=download)
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 1527, in process_video_result
    self.process_info(new_info)
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 1865, in process_info
    self.post_process(filename, info_dict)
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 1932, in post_process
    self.report_error(e.msg)
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 589, in report_error
    self.trouble(error_message, tb)
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 559, in trouble
    raise DownloadError(message, exc_info)
DownloadError: [0;31mERROR:[0m ffprobe or avprobe not found. Please install one.
>>> 

【问题讨论】:

【参考方案1】:

您希望音频为 mp3。 YouTube 仅提供其他格式的音频,例如 m4a。因此,youtube-dl 下载 m4a 文件并将其转换为 mp3。 youtube-dl 不包含自定义 mp3 转换器。它使用 ffmpeg 或 avconv 进行音频转换。

因此,如果要转换音频,需要安装 ffmpeg 或 avconv。如何在 macOS 上做到这一点is documented in detail at our sister site superuser。

您收到的其他警告是因为 beautifulsoup 配置错误。与其尝试自己解析搜索结果,不如让 youtube-dl 来做,一个类似于 'ytsearch1:flying bunny' 的 URL 应该可以解决问题。

【讨论】:

感谢您的快速回答!每当我尝试安装 ffmpeg 时,我都会得到这个答案:Error: Could not link: /usr/local/share/man/man1/brew.1 Please delete these paths and run brew update. 当我运行 echo $PATH 时,我会得到这个答案:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Library/TeX/texbin 我还不太了解路径。会不会是 ffmpeg 安装在某处但我设置不正确? 这听起来像是一个单独的问题。随时ask that question。确保包含您正在运行的实际命令和 full 输出。我不确定为什么$PATH 应该是相关的,但如果你相信它,也将它发布在新问题中。您是否尝试过按照消息中的说明删除/usr/local/share/man/man1/brew.1?你的理论可能是正确的,但我没有看到任何证据。 是的,我试过了。好的,我将其作为一个单独的问题提出 - 感谢您的耐心等待! (编辑:显然我已经达到了我的问题限制 - 在一个问题之后) @DavidSmerdon 这可能是因为这个问题被否决了——原因我不确定,但很可能是因为你没有创建a minimal example。您的问题现在是 +0,因此您应该可以再次提问。请参阅this page 了解更多信息。 非常感谢。与此同时,我发现我的问题和完整的输出错误与这个问题密切相关:link,答案指向这个问题:link。但我一直无法实施这个解决方案。发布我的问题值得复制吗?

以上是关于Mac 上 ffprobe/avprobe 和 youtube_dl 的 Python 问题的主要内容,如果未能解决你的问题,请参考以下文章

在 Mac 10.8.3 上安装 fltk

mac 上使用移动硬盘

vim + COPY + mac 通过 SSH

mac vim 复制粘贴

mac vim 复制粘贴

如何在mac上使用docker连接sql server?