hi,gays!
今天处理音频切割的时候,遇到了如下问题,如果你也遇到过同样的问题,还麻烦指点一二~
1、调用第三方模块pydub 尝试切割网易云音乐的音频文件失败
这是我的测试代码
# -*- coding: utf8 -*-
import re
__author__ = 'imagine'
from pydub import AudioSegment as au
def cut(file_path, start_time, end_time):
audio_format = re.match('.*(\..*)', file_path).group(1)
mp3 = au.from_file(file_path, format=audio_format)
mp3[start_time:end_time].export(file_path, format=audio_format)
if __name__ == '__main__':
cut('/home/createbugofmaster/123.mp3', 17*1000+500, 12*1000+500)
详细的traceback信息
Traceback (most recent call last):
File "/home/createbugofmaster/Desktop/annotationswebsite/common/audio.py", line 16, in
cut('/home/createbugofmaster/123.mp3', 17*1000+500, 12*1000+500)
File "/home/createbugofmaster/Desktop/annotationswebsite/common/audio.py", line 11, in cut
mp3 = au.from_mp3(file_path)
File "/home/createbugofmaster/.virtualenvs/django_py3/lib/python3.6/site-packages/pydub/audio_segment.py", line 716, in from_mp3
return cls.from_file(file, 'mp3', parameters)
File "/home/createbugofmaster/.virtualenvs/django_py3/lib/python3.6/site-packages/pydub/audio_segment.py", line 704, in from_file
p.returncode, p_err))
pydub.exceptions.CouldntDecodeError: Decoding failed. ffmpeg returned error code: 1
Output from ffmpeg/avlib:
b'ffmpeg version 3.4.4-0ubuntu0.18.04.1 Copyright (c) 2000-2018 the FFmpeg developers\n built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)\n configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared\n WARNING: library configuration mismatch\n avcodec configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared --enable-version3 --disable-doc --disable-programs --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libtesseract --enable-libvo_amrwbenc\n libavutil 55. 78.100 / 55. 78.100\n libavcodec 57.107.100 / 57.107.100\n libavformat 57. 83.100 / 57. 83.100\n libavdevice 57. 10.100 / 57. 10.100\n libavfilter 6.107.100 / 6.107.100\n libavresample 3. 7. 0 / 3. 7. 0\n libswscale 4. 8.100 / 4. 8.100\n libswresample 2. 9.100 / 2. 9.100\n libpostproc 54. 7.100 / 54. 7.100\n[mp3 @ 0x562e661d59a0] Failed to read frame size: Could not seek to 1026.\n/home/createbugofmaster/123.mp3: Invalid argument\n'
事实上我查看相关源代码
貌似在执行subprocess.Popen()调用shell(ffmpeg)脚本命令出的问题,
这是详细的ffmpeg shell脚本语句~['ffmpeg', '-y', '-f', 'mp3', '-i', '/home/createbugofmaster/123.mp3', '-vn', '-f', 'wav', '-']
conversion_command += [
"-vn", # Drop any video streams if there are any
"-f", "wav", # output options (filename last)
"-"
]
if parameters is not None:
# extend arguments with arbitrary set
conversion_command.extend(parameters)
log_conversion(conversion_command)
p = subprocess.Popen(conversion_command, stdin=stdin_parameter,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p_out, p_err = p.communicate(input=stdin_data)
if p.returncode != 0 or len(p_out) == 0:
file.close()
raise CouldntDecodeError(
"Decoding failed. ffmpeg returned error code: {0}\n\nOutput from ffmpeg/avlib:\n\n{1}".format(
p.returncode, p_err))