使用 ffmpeg 将 webm 转换为 wav [英] Coverting webm to wav with ffmpeg

查看:34
本文介绍了使用 ffmpeg 将 webm 转换为 wav的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地在 Python 中使用 ffmpeg 将 mp3 文件转换为 wav,以便我可以将它们发布到 Google Speech-To-Text.现在我对 webm 文件有同样的情况,我拥有的旧功能不起作用.它应该将文件转换为 wav 并将其拆分为 15 秒的块.我可以从 webm -file 执行此操作,还是需要先将其转换为其他格式?

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?

我使用过的函数:

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)

编辑.忘了提及该函数目前对 webm -files 的作用.它产生一个 wav -file out000000000.wav 这是空的.在控制台中,我收到如下错误消息:

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

编辑2.我认为我是对的,但想听听是否有更好的方法来做到这一点.

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

首先,我将文件转换为单声道 wav,然后将其拆分为多个块.请随时指出任何错误或错误.

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)

编辑3.我尝试了 lllogan 提供的功能,但出现此错误:

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!

当我使用两个单独的函数时它可以工作,但我认为 wav 中的音频质量比原始 webm 中的要差.我能做些什么吗?

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?

推荐答案

需要转换为pcm数据:

You need to convert to pcm data:

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

输出:

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%

这篇关于使用 ffmpeg 将 webm 转换为 wav的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆