从youtube-dl库的youtube链接下载音频剪辑后,无法使用pydub.AudioSegment切片音频文件。以下是有关我的代码的信息。
import youtube_dl
options = {
'format': 'bestaudio/best',
'extractaudio': True, # only keep the audio
'audio-format': "wav", # convert to wav
'outtmpl': whole_path, # name the file the ID of the video
'noplaylist': True, # only download single song, not playlist
'audioquality': 1
}
with youtube_dl.YoutubeDL(options) as ydl:
print('url is:', web_url)
ydl.download([web_url])
from pydub import AudioSegment
audio_data = AudioSegment.from_wav(path_to_downloaded_file)
然后,我首先在Mac上出现错误,然后在Linux上出现第二个错误:
b'avconv version 12.3, Copyright (c) 2000-2018 the Libav developers\n built on Jul 26 2018 18:08:50 with Apple LLVM version 9.1.0 (clang-902.0.39.2)\n/Users/***/random17560390.wav: Invalid data found when processing input\n'
b'ffmpeg version 2.8.15-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers\n built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 20160609\n configuration: --prefix=/usr --e
xtra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-
shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-li
bbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmod
plug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-li
bspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --e
nable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv\n libavutil 54. 31.100 / 54.
31.100\n libavcodec 56. 60.100 / 56. 60.100\n libavformat 56. 40.101 / 56. 40.101\n libavdevice 56. 4.100 / 56. 4.100\n libavfilter 5. 40.101 / 5. 40.101\n libavresa
mple 2. 1. 0 / 2. 1. 0\n libswscale 3. 1.101 / 3. 1.101\n libswresample 1. 2.101 / 1. 2.101\n libpostproc 53. 3.100 / 53. 3.100\n[wav @ 0x20854c0] invalid start
code [0][0][0][28] in RIFF header\n/home/users/test_download/random10485395.wav: Invalid data found when processing input\n'
我认为问题可能出在youtube-dl对下载文件的编码上。我可以在其他普通的.wav文件上使用pydub.AudioSegment。通过youtube-dl检查下载文件的详细信息后,我发现了以下功能:
Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
与其他具有以下内容的普通.wav文件不同:
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s