从 YouTube-DL 下载的音频文件已损坏

Audio Files downloaded from YouTube-DL are corrupted

我写了一小段代码,使用 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 视频编解码器和 youtube 的默认格式。

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

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

可能 youtube-dl 库有一些选项总是强制 re-encoding 到 MP3,如果你真的只需要 MP3,尽管要注意 re-conversion 你会失去一些质量。现在您的选项包含 preferred MP3 字样,这意味着它更喜欢 MP3 下载,但不强制使用此输出格式。

如果您不介意是 MP3 还是 AAC,那么请使用原始的 youtube 格式,这样您将保持最佳质量,为了清晰起见,您也可以使用 .aac 扩展名重命名文件。

如果您希望稍后处理 Youtube-downloaded 个文件,例如通过 8D 音频站点 ,然后在处理时不损失质量,您需要将文件转换为某种无损格式,如 WAV。您可以使用 FFMpeg 工具来执行此操作,例如 ffmpeg -i filename.aac filename.wav.

8D Audio Site has 20MB file size limit, hence you may need to split your WAV file. In order to split into parts of 30 seconds each you need to run ffmpeg -i filename.wav -f segment -segment_time 30 -c copy "filename%03d.wav" and then upload and process each part. You can concatenate processed WAVs afterwards using sox as sox sound0*.wav soundall.wav. And convert to some compressed but lossless format like FLACffmpeg -i soundall.wav soundall.flac

所有 conversion/splitting/merging 命令都可以使用 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 转换,那么我的下一个代码将进行正确的 WAV 转换,这只是该库的最小工作 sub-code 仅适用于 WAV。

Try it online!

# Needs: python -m pip install pydub numpy

def ConvertTo8D(input_file_name, output_file_name, *, period = 200):
    import numpy as np
    from pydub import AudioSegment
    
    if period < 0:
        period = -period
    elif period == 0:
        period = 200
        
    audio = AudioSegment.from_file(input_file_name)
    audio = audio + AudioSegment.silent(duration = 150)
    
    eightD = AudioSegment.empty()
    pan = 0.9 * np.sin(np.linspace(0, 2. * np.pi, period))

    for i, chunk in enumerate(audio[::100]):
        if len(chunk) < 100:
            continue
        newChunk = chunk.pan(pan[i % period])
        eightD = eightD + newChunk

    eightD.export(output_file_name, format = output_file_name[output_file_name.rfind('.') + 1:])

# Test Usage
ConvertTo8D('sound.wav', 'sound-8D.wav')