在python脚本中使用youtube-dl仅从youtube视频下载音频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27473526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
download only audio from youtube video using youtube-dl in python script
提问by lollercoaster
There's a fewpostson downloading audiofrom YouTube using youtube-dl
, but none of them are concrete or too helpful. I'm wondering what the best way to do it from a Python script is.
有一些关于使用从 YouTube下载音频的帖子,但没有一个是具体的或太有帮助。我想知道从 Python 脚本执行此操作的最佳方法是什么。youtube-dl
For example, here's the README example for downloading videos:
例如,这里是下载视频的 README 示例:
import youtube_dl
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
Obviously if you just care about the audio, you'd rather not download the whole video...
显然,如果你只关心音频,你宁愿不下载整个视频......
The youtube-dlsource is only so helpful (ie, not very).
在YouTube的-DL源仅仅是如此的帮助(即不太)。
Any suggestions how to script this?
任何建议如何编写脚本?
采纳答案by phihag
Read on in the developer instructionsfor an amended example:
阅读开发人员说明以获取修改后的示例:
from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
This will download an audio file if possible/supported. If the file is not mp3 already, the downloaded file be converted to mp3 using ffmpegor avconv. For more information, refer to the format
and postprocessors
documentation entries in a current version of youtube-dl.
如果可能/支持,这将下载音频文件。如果文件还不是 mp3,则使用ffmpeg或avconv将下载的文件转换为 mp3 。有关更多信息,请参阅youtube-dl 当前版本中的format
和postprocessors
文档条目。
回答by Pheon
Use postprocessors
argument. The list of all the available postprocessors can be found here.
使用postprocessors
参数。可以在此处找到所有可用后处理器的列表。
If you want to pass additional ffmpeg
or avconv
options, which are not included in youtube-dl
library (like audio bitrate - -ar <BR>
in ffmpeg
), add postprocessor_args
as a list.
如果要传递库中未包含的其他ffmpeg
或avconv
选项youtube-dl
(如音频比特率 - -ar <BR>
in ffmpeg
),请添加postprocessor_args
为列表。
You can also prefer ffmpeg
over avconv
setting prefer_ffmpeg
to True
.
你也可以喜欢ffmpeg
在avconv
设置prefer_ffmpeg
到True
。
And to keep both original and converted audio file set 'keepvideo'
to True
.
并将原始和转换后的音频文件设置'keepvideo'
为True
.
For example:
例如:
from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
'preferredquality': '192'
}],
'postprocessor_args': [
'-ar', '16000'
],
'prefer_ffmpeg': True,
'keepvideo': True
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
The list of all the available options is in the documentation. You can read ffmpeg posprocessor's code here.
所有可用选项的列表在文档中。您可以在此处阅读 ffmpeg 后处理器的代码。
And a less complex example is in their GitHub README.
一个不太复杂的例子在他们的 GitHub README 中。