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

苏希尔

我编写了一小段代码,使用来从YouTube下载音频和视频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!

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

附庸风雅

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

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

您也可以通过运行FFMpeg来检测格式ffmpeg -i filename.mp3,对于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为清楚起见,您也可以使用扩展名重命名文件

如果您希望稍后(例如8D Audio Site)处理通过Youtube下载的文件,则在处理过程中不会丢失质量,您需要将文件转换为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用作级联处理过的WAV sox sound0*.wav soundall.wav并转换为像一些压缩,但无损格式FLACffmpeg -i soundall.wav soundall.flac

使用Python的标准模块进程,可以轻松实现所有转换/拆分/合并命令例如,像ffmpeg -i sound.mp3 sound.wav在Python中一行一样运行任何命令subprocess.run(['ffmpeg', '-i', 'sound.mp3', 'sound.wav'], check = True)

所有专业人员都以某种无损格式(例如WAV或FLAC)编辑和存储音频文件,以免在处理的每个阶段都失去质量,因此,这是一个好习惯,尤其是如果您要对8D处理文件进行MP3转换,肯定会多次。一个坏主意。

如果该库在数学上正确地进行8D转换,那么我的下一个代码将进行正确的WAV转换,这仅是该库的最小工作子代码,仅适用于WAV。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

不显示提取的音频文件的YouTube-DL Python详细信息

来自分类Dev

youtube-dl:下载Opus音频还是AAC音频?

来自分类Dev

YouTube的-DL - 从YouTube下载视频字幕 - 教程

来自分类Dev

使用 ffmpeg 和 youtube-dl 从 Youtube 视频下载几秒钟的音频结果 [youtube]:没有这样的文件或目录错误

来自分类Dev

在python脚本中使用youtube-dl仅从youtube视频下载音频

来自分类Dev

带有 youtube-dl 和 ffmpeg 的 YouTube 音频下载器

来自分类Dev

无法下载youtube-dl

来自分类Dev

无法下载youtube-dl

来自分类Dev

youtube_dl:通过终端同步下载文件

来自分类Dev

使用youtube-dl下载youtube播放列表,跳过现有文件

来自分类Dev

使用youtube-dl下载整个youtube频道

来自分类Dev

youtube-dl:下载youtube用户的完整视频集

来自分类Dev

使用youtube-dl下载整个youtube频道

来自分类Dev

youtube-dl无法下载音频如何解决该问题?

来自分类Dev

我可以使用youtube-dl直接下载音频吗?

来自分类Dev

我可以使用youtube-dl直接下载音频吗?

来自分类Dev

youtube-dl无法下载音频如何解决该问题?

来自分类Dev

如何在youtube-dl中下载本身具有音频的格式?

来自分类Dev

无法使用youtube-dl下载

来自分类Dev

youtube-dl无法下载网页

来自分类Dev

无法使用youtube-dl下载

来自分类Dev

youtube-dl下载自动翻译字幕

来自分类Dev

youtube-dl删除下载的部分

来自分类Dev

youtube-dl直接下载网址

来自分类Dev

从youtube搜索和下载纯音频

来自分类Dev

youtube-dl; 如何仅下载播放列表,而不下载其中的文件

来自分类Dev

Zip 文件下载已损坏

来自分类Dev

youtube-dl错误

来自分类Dev

youtube-dl错误