代码之家  ›  专栏  ›  技术社区  ›  Sushil

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

  •  1
  • Sushil  · 技术社区  · 5 年前

    我写了一小段代码,可以使用 YouTube-DL

    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!
    

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

    1 回复  |  直到 5 年前
        1
  •  2
  •   Arty    5 年前

    AAC (通常具有文件扩展名) .aac ),这是的默认格式 .mp4 以及AVC视频编解码器和youtube。

    AudioSegment.from_mp3(path) 试着做 AudioSegment.from_file(path) 它将自行或自动检测格式 AudioSegment.from_file(path, format = 'aac') 强迫它解码AAC。

    您还可以使用 FFMpeg 通过跑步 ffmpeg -i filename.mp3 Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 320 kb/s ,而对于其他格式,它将是非- mp3

    youtube-dl 如果你真的只需要MP3,库中有一些选项可以强制重新编码为MP3,但要注意,重新转换会降低一些质量。现在你的选择包括 preferred MP3的意思是它更喜欢MP3下载,但不强制这种输出格式。

    .aac 为清晰起见,请进行扩展。

    8D Audio Site ,然后在处理过程中不丢失质量,您需要将文件转换为WAV等无损格式。你可以使用 FFMpeg 工具,像这样 ffmpeg -i filename.aac filename.wav

    而且 8D音频网站 20MB 文件大小限制,因此您可能需要拆分WAV文件。为了分成30秒的部分,你需要跑步 ffmpeg -i filename.wav -f segment -segment_time 30 -c copy "filename%03d.wav" sox sox sound0*.wav soundall.wav FLAC ffmpeg -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的最小工作子代码。