gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:46:53 25 4
gpt4 key购买 nike

我编写了一小段代码,使用 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 Site20MB文件大小限制,因此您可能需要拆分 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 的标准模块 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 库的最小工作子代码。
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')

关于python - 从 YouTube-DL 下载的音频文件已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64085443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com