【发布时间】:2023-04-11 08:02:02
【问题描述】:
我想简单地从 youtube 链接中提取 MP3 格式的音频,但如果不使用 --extract-audio 选项从命令行调用 youtube-dl,我无法弄清楚如何做到这一点。有没有办法在 YoutubeDL 类中执行此操作,类似于给出的示例 here?
这是我目前所拥有的,但它仍然会创建一个 mp4 文件。
import youtube_dl
# download using optimal audio settings
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'quiet': True,
'restrictfilenames': True}
ydl = youtube_dl.YoutubeDL(ydl_opts)
ydl.download(['https://www.youtube.com/watch?v=Pnt2cy5MWyw'])
澄清:上面的代码会生成一个MP3文件,但它也会生成一个MP4视频文件。我只希望它生成 MP3。
更新:以下是 quiet 设置为 False 时的输出示例:
[youtube] et6sSlEn8LE: Downloading webpage
[youtube] et6sSlEn8LE: Extracting video information
[youtube] et6sSlEn8LE: Downloading DASH manifest
[download] Destination: downloaded_tracks/et6sSlEn8LE.m4a
[download] 0.0% of 7.63MiB at 47.07KiB/s ETA 02:46
[download] 0.0% of 7.63MiB at 138.74KiB/s ETA 00:56
[download] 0.1% of 7.63MiB at 319.81KiB/s ETA 00:24
[download] 0.2% of 7.63MiB at 679.31KiB/s ETA 00:11
[download] 0.4% of 7.63MiB at 1.12MiB/s ETA 00:06
[download] 0.8% of 7.63MiB at 1.19MiB/s ETA 00:06
[download] 1.6% of 7.63MiB at 1.65MiB/s ETA 00:04
[download] 3.3% of 7.63MiB at 2.43MiB/s ETA 00:03
[download] 6.5% of 7.63MiB at 3.38MiB/s ETA 00:02
[download] 13.1% of 7.63MiB at 4.32MiB/s ETA 00:01
[download] 26.2% of 7.63MiB at 4.96MiB/s ETA 00:01
[download] 52.4% of 7.63MiB at 5.15MiB/s ETA 00:00
[download] 100.0% of 7.63MiB at 6.87MiB/s ETA 00:00
[download] 100% of 7.63MiB in 00:01
[ffmpeg] Correcting container in "downloaded_tracks/et6sSlEn8LE.m4a"
[avconv] Destination: downloaded_tracks/et6sSlEn8LE.mp3
Deleting original file downloaded_tracks/et6sSlEn8LE.m4a (pass -k to keep)
【问题讨论】:
-
它在这里工作,你应该尝试删除
'quiet': True,行,看看它是否输出更有用的东西。您也可以open an issue at GitHub,在那里您可能会在调试问题时获得更好的帮助。
标签: python youtube youtube-dl