我编写了一小段代码,使用 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。
您还可以使用 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
重命名文件。为了清晰起见,扩展。
如果您希望稍后处理 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 连接处理后的 WAV如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 转换,那么我的下一个代码将进行正确的 WAV 转换,它只是该库仅用于 WAV 的最小工作子代码。
# 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')
关于python - 从 YouTube-DL 下载的音频文件已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64085443/