PyDub:ffmpeg 标头丢失

问题描述 投票:0回答:2

我制作了一个简单的 Python 程序,在 mp3 文件的开头添加短暂的暂停

import os

from pydub import AudioSegment

AUDIO_DIR = 'C:\\Users\\Ray\\Downloads\\sentence-audio'
OUT_DIR = 'C:\\Users\\Ray\\Downloads\\out-audio'

pause = AudioSegment.silent(duration=400)

for fname in os.listdir(AUDIO_DIR):
    print(f'Processing {fname}')
    newf = os.path.join(OUT_DIR, os.path.basename(fname))
    if os.path.isfile(newf):
        print(f'{fname} exists')
        continue
    audio = AudioSegment.from_mp3(os.path.join(AUDIO_DIR, fname))
    new = pause + audio
    new.export(newf, format='mp3')
    print(f'{fname} lengthened')

运行此程序时,我收到特定 mp3 文件的以下错误消息

Traceback (most recent call last):
  File "C:\Users\Ray\Downloads\lengthen.py", line 16, in <module>
    audio = AudioSegment.from_mp3(os.path.join(AUDIO_DIR, fname))
  File "C:\Python39\lib\site-packages\pydub\audio_segment.py", line 796, in from_mp3
    return cls.from_file(file, 'mp3', parameters=parameters)
  File "C:\Python39\lib\site-packages\pydub\audio_segment.py", line 773, in from_file
    raise CouldntDecodeError(
pydub.exceptions.CouldntDecodeError: Decoding failed. ffmpeg returned error code: 1

Output from ffmpeg/avlib:

ffmpeg version git-2020-04-03-52523b6 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3.1 (GCC) 20200328
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libsrt --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-ffnvcodec --enable-cuda-llvm --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf
  libavutil      56. 42.102 / 56. 42.102
  libavcodec     58. 77.101 / 58. 77.101
  libavformat    58. 42.100 / 58. 42.100
  libavdevice    58.  9.103 / 58.  9.103
  libavfilter     7. 77.101 /  7. 77.101
  libswscale      5.  6.101 /  5.  6.101
  libswresample   3.  6.100 /  3.  6.100
  libpostproc    55.  6.100 / 55.  6.100
[mp3float @ 0000016489995380] Header missing
    Last message repeated 71 times
[mp3 @ 000001648998b500] decoding for stream 0 failed
[mp3 @ 000001648998b500] Could not find codec parameters for stream 0 (Audio: mp3 (mp3float), 0 channels, fltp): unspecified frame size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mp3, from 'C:\Users\Ray\Downloads\sentence-audio\354.mp3':
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Audio: mp3, 0 channels, fltp
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
[mp3float @ 0000016489995800] Header missing
Error while decoding stream #0:0: Invalid data found when processing input
...

我一开始以为mp3文件已损坏,因此无法播放。但是,当我在媒体播放器中播放该文件时,一切都很好。

我不知道如何解决这个问题。有什么解决办法吗?

python audio ffmpeg
2个回答
0
投票

可以在这里找到适合我的解决方案。问题是 mp3 音频是从 YouTube 中提取的,必须以 mp4 的形式读入。

audio =  AudioSegment.from_file(file_path, format="mp4")

-2
投票

我认为我们需要有关您的音频文件的更多信息。尝试将它们转换为

wav
文件而不是使用 mp3。这可能对你更有效。

ffmpeg -i input.mp3 output.wav  

© www.soinside.com 2019 - 2024. All rights reserved.