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?