Coverting webm to wav with ffmpeg

夙愿已清 提交于 2020-12-13 03:53:08

问题


I've succesfully used ffmpeg in Python to convert mp3-files into wav so I can post them to Google Speech-To-Text. Now I have same situation with webm files and the old function I have doesn't work. It should convert the file into wav and split it into 15 second chunks. Can I do this from webm -file or do I need to convert it first into some other format?

The function I've used:

def convert_and_split(filename):
    command = ['ffmpeg', '-i', filename, '-f', 'segment', '-segment_time', '15', '-c', 'copy', 'parts/out%09d.wav']
    subprocess.run(command,stdout=subprocess.PIPE,stdin=subprocess.PIPE)

EDIT. Forgot to mention what the function does at the moment with webm -files. It produces one wav -file out000000000.wav which is empty. In the console I get an error message like this:

[segment @ 0x55970b22fe80] Opening 'parts/out000000000.wav' for writing
[wav @ 0x55970b1ffbc0] opus codec not supported in WAVE format
Could not write header for output file #0 (incorrect codec parameters ?): Function not implemented

EDIT2. I got it right I think but would like to hear if there's a better way to do this.

First I convert the file to a mono wav and then split it into chunks. Please feel free to point out any mistakes or errors.

def convert_webm_to_wav(file):
    command = ['ffmpeg', '-i', file, '-acodec', 'pcm_s16le', '-ac', '1', '-ar', '16000', '/home/janip/openvidu_files/' + file.name[:-5] + '.wav']
    subprocess.run(command,stdout=subprocess.PIPE,stdin=subprocess.PIPE)

def split_audio(filename):
    command = ['ffmpeg', '-i', filename, '-f', 'segment', '-segment_time', '15', '-c', 'copy', '/home/janip/work/Holda/Nut_ideas/voice_chat_demos/openvidu-tutorials/openvidu-js-node/python_scripts/parts/out%09d.wav']
    subprocess.run(command,stdout=subprocess.PIPE,stdin=subprocess.PIPE)

EDIT3. I tried the function llogan offered and I get this error:

error:[segment @ 0x55f1c28d2740] Opening 'parts/out000000000.wav' for writing
[segment @ 0x55f1c28d2740] Failed to open segment 'parts/out000000000.wav'
Could not write header for output file #0 (incorrect codec parameters ?): No such file or directory
Error initializing output stream 0:0 -- 

error:Conversion failed!

It works when I use two seperate functions but I think the audio quality is worse in wav than in the original webm. Anything I can do to that?


回答1:


You need to convert to pcm data:

ffmpeg -i ./big-buck-bunny_trailer.webm -c:a pcm_f32le ./out.wav

Output:

ffmpeg version 4.2.3 Copyright (c) 2000-2020 the FFmpeg developers
  built with Apple clang version 11.0.3 (clang-1103.0.32.59)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.3 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags=-fno-stack-check --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, matroska,webm, from './big-buck-bunny_trailer.webm':
  Metadata:
    encoder         : http://sourceforge.net/projects/yamka
    creation_time   : 2010-05-20T08:21:12.000000Z
  Duration: 00:00:32.48, start: 0.000000, bitrate: 533 kb/s
    Stream #0:0(eng): Video: vp8, yuv420p(progressive), 640x360, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 1k tbn, 1k tbc (default)
    Stream #0:1(eng): Audio: vorbis, 44100 Hz, mono, fltp (default)
Stream mapping:
  Stream #0:1 -> #0:0 (vorbis (native) -> pcm_f32le (native))
Press [q] to stop, [?] for help
Output #0, wav, to './out.wav':
  Metadata:
    ISFT            : Lavf58.29.100
    Stream #0:0(eng): Audio: pcm_f32le ([3][0][0][0] / 0x0003), 44100 Hz, mono, flt, 1411 kb/s (default)
    Metadata:
      encoder         : Lavc58.54.100 pcm_f32le
size=    5597kB time=00:00:32.50 bitrate=1410.7kbits/s speed= 625x
video:0kB audio:5597kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.001989%



回答2:


No need to convert to WAV then do a separate command to segment. Just remove -c copy and do it all in one command:

def convert_and_split(filename):
    command = ['ffmpeg', '-i', filename, '-f', 'segment', '-segment_time', '15', 'out%09d.wav']
    subprocess.run(command,stdout=subprocess.PIPE,stdin=subprocess.PIPE)

-c copy enables stream copy mode. It is like a copy and paste, but you can't put Opus audio into WAV. Removing -c copy will allow ffmpeg to convert Opus to WAV.



来源:https://stackoverflow.com/questions/62064665/coverting-webm-to-wav-with-ffmpeg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!