【问题标题】:Audio Files downloaded from YouTube-DL are corrupted从 YouTube-DL 下载的音频文件已损坏
【发布时间】:2021-01-13 01:09:27
【问题描述】:

我编写了一小段代码,使用 YouTube-DL 从 YouTube 下载音频和视频。这是我的代码:

from __future__ import unicode_literals
import youtube_dl

link = input("Enter the video link:")

while True:
    choice = input("Enter a for audio file, v for video file:")
    if choice == "a" or choice == "v":
        break

ydl_opts = {}

if choice == "a":
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
else:
    ydl_opts = {'format':137}
    
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    info_dict = ydl.extract_info(link, download=False)
    video_title = info_dict.get('title', None)

if choice == "a":
    path = f'D:\\DwnldsYT\\{video_title}.mp3'
if choice == "v":
    path = f'D:\\DwnldsYT\\{video_title}.mp4'

ydl_opts.update({'outtmpl':path})

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([link])

这很好用。我可以播放下载的音频。但是当我尝试读取相同的音频文件时,它会显示一个错误。

代码:

from pydub import AudioSegment

song = AudioSegment.from_mp3("D:\\DwnldsYT\\Cartoon - On & On (feat. Daniel Levi) [NCS Release].mp3")

输出:

Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 000001f2425c2500] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[abuffer @ 000001f242280300] Value inf for parameter 'time_base' out of range [0 - 2.14748e+09]
    Last message repeated 3 times
[abuffer @ 000001f242280300] Error setting option time_base to value 1/0.
[graph_0_in_0_0 @ 000001f242280e80] Error applying options to the filter.
Error reinitializing filters!
Error while filtering: Result too large
Finishing stream 0:0 without any data written to it.
[abuffer @ 000001f242280300] Value inf for parameter 'time_base' out of range [0 - 2.14748e+09]
    Last message repeated 3 times
[abuffer @ 000001f242280300] Error setting option time_base to value 1/0.
[graph_0_in_0_0 @ 000001f242280e80] Error applying options to the filter.
Error configuring filter graph
Conversion failed!

这是因为下载的音频损坏了吗?以及如何解决这个问题?任何帮助,将不胜感激。谢谢!

【问题讨论】:

  • 会不会是这样的情况,虽然文件具有.mp3 扩展名,但它可能被编码为.aac 格式(AAC 编解码器)?这是.mp4 和 AVC 视频编解码器的默认格式。
  • 试试ffmpeg -i filename.mp3 看看,对于MP3,它应该输出类似Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 320 kb/s 的东西。
  • 尝试使用AudioSegment.from_file(path) 而不是AudioSegment.from_mp3(path)
  • 是的...AudioSegment.from_file(path) 有效。但是,如果我将下载的音频文件上传到网站,该网站将无法正确处理音频文件。
  • 例如,这是我要上传音频文件的网站 -- audioalter.com/8d-audio 如果我将音频文件上传到该网站,它不会将其转换为 8d。

标签: python python-3.x ffmpeg youtube-dl


【解决方案1】:

您的音频文件可能未编码为 MP3。它可能是AAC(通常具有文件扩展名.aac),这是.mp4 和AVC 视频编解码器和youtube 的默认格式。

而不是AudioSegment.from_mp3(path) 尝试执行AudioSegment.from_file(path) 它将自行检测格式或AudioSegment.from_file(path, format = 'aac') 强制它解码AAC。

您还可以通过运行ffmpeg -i filename.mp3 使用FFMpeg 检测格式,对于MP3,它应该输出类似Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 320 kb/s 的内容,对于其他格式,它将是非mp3 字符串。

youtube-dl 库可能有一些选项可以始终强制重新编码为 MP3,如果您真的只需要 MP3,但要注意重新转换会损失一些质量。现在你的选项包含preferred MP3 字样,这意味着它更喜欢 MP3 下载,但不强制这种输出格式。

如果是 MP3 或 AAC 对您来说无关紧要,那么请保持原始 youtube 的格式,这样您将保持最佳质量,您还可以将文件重命名为 .aac 扩展名以清楚起见。

如果您希望稍后处理 Youtube 下载的文件,例如通过8D Audio Site,然后在处理时不损失质量,您需要将文件转换为一些无损格式,如 WAV。您可以使用FFMpeg 工具执行此操作,例如ffmpeg -i filename.aac filename.wav

8D Audio Site 也有20MB 文件大小限制,因此您可能需要拆分 WAV 文件。为了分成 30 秒的部分,您需要运行 ffmpeg -i filename.wav -f segment -segment_time 30 -c copy "filename%03d.wav" 然后上传和处理每个部分。之后,您可以使用 sox 作为 sox sound0*.wav soundall.wav 连接处理过的 WAV。并转换为一些压缩但无损的格式,如FLACffmpeg -i soundall.wav soundall.flac

使用 Python 的标准模块 subprocess 可以轻松自动化所有转换/拆分/合并命令。例如。要运行像 ffmpeg -i sound.mp3 sound.wav 这样的任何命令,您只需在 Python 中执行一行 subprocess.run(['ffmpeg', '-i', 'sound.mp3', 'sound.wav'], check = True)

所有专业人员都在以 WAV 或 FLAC 等无损格式编辑和存储音频文件,以免在处理的每个阶段都损失质量,所以这是一个很好的做法,特别是如果你想对文件进行 8D 处理,肯定是 MP3 再转换几个时间将是一个坏主意。

如果 this library 在数学上正确进行 8D 转换,那么 my next code 将进行正确的 WAV 转换,它只是该库的最小工作子代码,仅用于 WAV。

【讨论】:

  • 谢谢!一个小问题。我应该将preferred 更改为wav,以便youtube-dl 将音频下载为wav 文件吗?
  • 但是当我以wav 下载音频时,文件太大了。 youtu.be/Hn4sfC2PbhI的音频文件大小将近40mb。
  • @Sushil By preferred 这个库可能意味着下一个行为 - 它向 Youtube 查询所有可用格式的视频/音频流,然后如果它在原始流格式中找到 preferred 音频格式,那么它下载这个首选,否则它下载一些最接近首选格式。因此,这并不意味着下载的格式将与首选指定的格式完全相同。它只是试图找到最接近的可下载格式,但没有重新转换。
  • 好的...但是文件大小呢?
  • @Sushil 你是说 WAV 太大了。这不是一个大问题,因为我将答案更改为包括切割成 30 秒或更多部分的命令,使用该命令。如果要8D处理,一定要下载原版WAV,剪下来。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 2015-02-12
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多