youtube_dl 使用 Python 获取音频 link
youtube_dl get audio link with Python
我正在尝试使用 youtube_dl 检索 youtube 上纯音频文件的 link。我想知道是否有可能这样做。我正在使用 youtube_dl 作为 python 代码而不是终端。
非常感谢
如果你只想下载音频,你可以这样做,
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': '%(title)s.%(etx)s',
'quiet': False
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url]) # Download into the current working directory
这是我从我从事的项目中提取的一段代码。您可以在此处查看完整的源代码:https://github.com/francium/audiosave/blob/master/audiosave.py
您可以在此处查看 API 和 youtube_dl 的文档:https://github.com/rg3/youtube-dl/blob/master/README.md#embedding-youtube-dl
这是一个现有问题的重复:download only audio from youtube video using youtube-dl in python script
不过,我会提供帮助并在这里分享答案。使用此代码:
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(['insert youtube link here'])
它应该使用 ffmpeg 将音频转换为 mp3。有关更多示例和说明,请参阅 here。
只有极少数 websites supported by youtube-dl 实际提供 audio-only 文件。但有些确实如此,包括 YouTube 上的大多数视频。对于这些,您可以请求文件类型 bestaudio
并提取信息而不是下载:
from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'format': 'bestaudio',
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(
'https://www.youtube.com/watch?v=BaW_jenozKc', download=False)
print(info['formats'][0]['url'])
请注意,这将为您提供正确的 URL。但是,如果您更改以下任何内容,则无法保证此 URL 会起作用:
- 使用URL的IP地址(换句话说,如果你将这个URL转移到另一台机器上并尝试从那里下载,它是行不通的)。
- HTTP headers(User-Agent 等)。可以在格式字典的
http_headers
键中找到。
- Cookie(可以在
ydl.cookiejar
中找到)。
必须满足哪些限制条件取决于视频,并且可能会突然发生变化。例如,目前看来 URL 对于许多 YouTube 视频来说已经足够了,但 YouTube 肯定已经屏蔽了其他 IPv4 地址,甚至屏蔽了某些视频的所有不同 IP 地址,或者仅屏蔽了音乐和其他高度盈利的视频,超过时间.
另请注意,您将获得的文件格式可能很奇怪。例如,YouTube 过去常常发送大多数播放器无法读取的无效 m4a 文件。您通常会得到 opus, which may not be supported everywhere. If you just want the audio file, it's better to let youtube-dl download and convert it, as described in the documentation 和其他答案。
我正在尝试使用 youtube_dl 检索 youtube 上纯音频文件的 link。我想知道是否有可能这样做。我正在使用 youtube_dl 作为 python 代码而不是终端。
非常感谢
如果你只想下载音频,你可以这样做,
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': '%(title)s.%(etx)s',
'quiet': False
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url]) # Download into the current working directory
这是我从我从事的项目中提取的一段代码。您可以在此处查看完整的源代码:https://github.com/francium/audiosave/blob/master/audiosave.py
您可以在此处查看 API 和 youtube_dl 的文档:https://github.com/rg3/youtube-dl/blob/master/README.md#embedding-youtube-dl
这是一个现有问题的重复:download only audio from youtube video using youtube-dl in python script
不过,我会提供帮助并在这里分享答案。使用此代码:
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(['insert youtube link here'])
它应该使用 ffmpeg 将音频转换为 mp3。有关更多示例和说明,请参阅 here。
只有极少数 websites supported by youtube-dl 实际提供 audio-only 文件。但有些确实如此,包括 YouTube 上的大多数视频。对于这些,您可以请求文件类型 bestaudio
并提取信息而不是下载:
from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'format': 'bestaudio',
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(
'https://www.youtube.com/watch?v=BaW_jenozKc', download=False)
print(info['formats'][0]['url'])
请注意,这将为您提供正确的 URL。但是,如果您更改以下任何内容,则无法保证此 URL 会起作用:
- 使用URL的IP地址(换句话说,如果你将这个URL转移到另一台机器上并尝试从那里下载,它是行不通的)。
- HTTP headers(User-Agent 等)。可以在格式字典的
http_headers
键中找到。 - Cookie(可以在
ydl.cookiejar
中找到)。
必须满足哪些限制条件取决于视频,并且可能会突然发生变化。例如,目前看来 URL 对于许多 YouTube 视频来说已经足够了,但 YouTube 肯定已经屏蔽了其他 IPv4 地址,甚至屏蔽了某些视频的所有不同 IP 地址,或者仅屏蔽了音乐和其他高度盈利的视频,超过时间.
另请注意,您将获得的文件格式可能很奇怪。例如,YouTube 过去常常发送大多数播放器无法读取的无效 m4a 文件。您通常会得到 opus, which may not be supported everywhere. If you just want the audio file, it's better to let youtube-dl download and convert it, as described in the documentation 和其他答案。