Convert from opus to ogg file using ffmpeg [python soundfile] - audio

I'm using python soundfile to audio files in one of my projects. I have a dataset that contains opus files.
python soundfile can not read opus files directly but can read ogg files. (https://github.com/bastibe/python-soundfile/issues/252)
How can I convert all the opus files to ogg files with ffmpeg?
I have tried the following command,
ffmpeg -i test_file_2.opus -c:a libvorbis -b:a 16k test_file_2.ogg
But I get an error,
ffmpeg version 4.3 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 7.3.0 (crosstool-NG 1.23.0.449-a04d0)
configuration: --prefix=/opt/conda --cc=/opt/conda/conda-bld/ffmpeg_1597178665428/_build_env/bin/x86_64-conda_cos6-linux-gnu-cc --disable-doc --disable-openssl --enable-avresample --enable-gnutls --enable-hardcoded-tables --enable-libfreetype --enable-libopenh264 --enable-pic --enable-pthreads --enable-shared --disable-static --enable-version3 --enable-zlib --enable-libmp3lame
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
[ogg # 0x55bad6b9cbc0] 543 bytes of comment header remain
Input #0, ogg, from 'test_file_2.opus':
Duration: 00:00:14.10, start: 0.000000, bitrate: 34 kb/s
Stream #0:0: Audio: opus, 48000 Hz, mono, fltp
Metadata:
album : Onder Moeders Vleugels
ENCODER_OPTIONS : --quiet
artist : Louisa May Alcott
title : 02 - Een vroolijk kerstfeest
encoder : opusenc from opus-tools 0.1.10;Lavf57.83.100
Unknown encoder 'libvorbis'

Unknown encoder 'libvorbis'
Your ffmpeg does not have libvorbis support. It is missing --enable-libvorbis in the configure line. libvorbis is an audio encoder that outputs the Vorbis audio format. Vorbis is the audio format that is typically used in the OGG/OGA container. libvorbis is the recommended encoder for Vorbis audio.
Find a new version/package of ffmpeg that has --enable-libvorbis or re-compile ffmpeg and include this.
Experimental vorbis encoder
FFmpeg has a built-in encoder named vorbis, but it is considered to be experimental and not comparable to libvorbis. Use libvorbis instead if you care about the quality. If you want to try vorbis:
ffmpeg -i input.opus -c:a vorbis -strict experimental output.ogg
Or pipe to oggenc
ffmpeg -i input.opus -f wav - | oggenc -o output.ogg -

I'm using librosa right now for conversion, but this is extremely slow:
# converting opus to ogg files for reading audio files with python soundfile
from glob import glob
from tqdm import tqdm
import soundfile as sf
import librosa
import os
for f in tqdm(glob("/Data/AUDIO_DATA/mls_dutch_opus/*/audio/*/*/*.opus") + glob("/Data/AUDIO_DATA/mls_german_opus/*/audio/*/*/*.opus")):
audio, sr = librosa.load(f, res_type='kaiser_fast', sr = 16000)
sf.write(f.replace(".opus", ".ogg"), audio, sr, format='ogg', subtype='vorbis')
os.remove(f)

Related

Python conversion from wav to mp3 file

How can I convert wav files to mp3 files in Python ? The only answers I found say to use "pydub" but it's not working at all for me :
from pydub import AudioSegment
AudioSegment.from_wav("myfile.wav").export("myfile.mp3", format="mp3")
Result with the error :
CouldntEncodeError: Encoding failed. ffmpeg/avlib returned error code: 1
Command:['ffmpeg', '-y', '-f', 'wav', '-i', 'C:\\Users\\user\\AppData\\Local\\Temp\\tmp4bj663tf', '-f', 'mp3',
'C:\\Users\\user\\AppData\\Local\\Temp\\tmp0kadz5q2']
Output from ffmpeg/avlib:
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 10.2.1 (GCC) 20200726
configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libsrt --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libgsm --enable-librav1e --disable-w32threads --enable-libmfx --enable-ffnvcodec --enable-cuda-llvm --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'C:\Users\user\AppData\Local\Temp\tmp4bj663tf':
Duration: 00:49:30.40, bitrate: 800 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 50000 Hz, mono, s16, 800 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_s16le (native) -> mp3 (mp3_mf))
Press [q] to stop, [?] for help
[mp3_mf # 00000217000ebdc0] MFT name: 'MP3 Encoder ACM Wrapper MFT'
[mp3_mf # 00000217000ebdc0] unsupported input sample rate set
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!
Paths to files are correct (else I have a different error) and I've tried with frequencies from 50k to 5k for the input signal, same error.
Searching for another way to convert or a fix for this.
The error message suggests that the issue is related to the sample rate of the input audio file. The error message states that the sample rate of the input audio file is not supported by the encoder.
One solution would be to use the set_frame_rate function from pydub, which can change the sample rate of an audio file, before exporting it to mp3. You can try something like this:
from pydub import AudioSegment
audio = AudioSegment.from_wav("myfile.wav")
audio = audio.set_frame_rate(44100)
audio.export("myfile.mp3", format="mp3")
It's also possible that the ffmpeg library pydub uses doesn't support the sample rate of your wav file.
You can try specifying the ffmpeg path explicitly when you create an AudioSegment object and see if that fixes the issue:
from pydub import AudioSegment
AudioSegment.converter = r"path\to\ffmpeg.exe"
audio = AudioSegment.from_wav("myfile.wav")
audio.export("myfile.mp3", format="mp3")
If none of the above solution is working, you may try other libraries like moviepy or soundfile to perform the conversion, or use command line tools such as ffmpeg directly.

FFmpeg problems with playing a stream

I want to play a stream over network. While my server works I can't get it working receiving audio on client. It's router with openwrt installed and usb soundcard.
When i run
ffmpeg -i "udp://#:5555" -f alsa hw:0 i get a following error
[NULL # 0x76b2b390] Requested output format 'alsa' is not a suitable output format
hw:0: Invalid argument
What's wrong with my command?
On router there is limited space. I hardly handled to install ffmpeg.
There is complete log :
root#LEDE:/proc/asound# ffmpeg -i "udp://#:5555" -f alsa hw:0
ffmpeg version 3.2.2 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 5.4.0 (LEDE GCC 5.4.0 r3101-bce140e)
configuration: --enable-cross-compile --cross-prefix=mips-openwrt-linux-musl- --arch=mips --target-os=linux --prefix=/usr --pkg-config=pkg-config --enable-shared --enable-static --enable-small --enable-pthreads --enable-zlib --disable-doc --disable-debug --enable-gpl --enable-version3 --disable-dxva2 --disable-lzma --disable-vaapi --disable-vda --disable-vdpau --disable-outdevs --disable-altivec --disable-vsx --disable-power8 --disable-amd3dnow --disable-amd3dnowext --disable-mmx --disable-mmxext --disable-sse --disable-sse2 --disable-sse3 --disable-ssse3 --disable-sse4 --disable-sse42 --disable-avx --disable-xop --disable-fma3 --disable-fma4 --disable-avx2 --disable-aesni --disable-armv5te --disable-armv6 --disable-armv6t2 --disable-inline-asm --disable-mipsdsp --disable-mipsdspr2 --disable-mipsfpu --disable-msa --disable-mmi --disable-fast-unaligned --disable-runtime-cpudetect --disable-yasm --enable-libopus --enable-decoder=libopus --enable-libx264 --enable-libmp3lame
libavutil 55. 34.100 / 55. 34.100
libavcodec 57. 64.101 / 57. 64.101
libavformat 57. 56.100 / 57. 56.100
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
Input #0, mpegts, from 'udp://#:5555':
Duration: N/A, start: 1.400000, bitrate: 384 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Audio: mp2 ([3][0][0][0] / 0x0003), 44100 Hz, stereo, s16p, 384 kb/s
[NULL # 0x76b2b390] Requested output format 'alsa' is not a suitable output format
hw:0: Invalid argument
Your ffmpeg was compiled with --disable-outdevs which disabled all output devices including ALSA.
You will need to re-compile by either omitting --disable-outdevs, or by adding --enable-outdev=alsa.

puppeteer-recorder: Cannot call write after a stream was destroyed

i use puppeteer-recorder,code is:
await record({
browser: browser,
page: page,
output: '8output.webm',
fps: 8,
frames: 8 * 10,
prepare: function (browser, page) {},
render: function (browser, page, frame) {}
});
but run with error:
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
at doWrite (_stream_writable.js:413:19)
at writeOrBuffer (_stream_writable.js:401:5)
at Socket.Writable.write (_stream_writable.js:301:11)
at /data/pupp_test/node_modules/puppeteer-recorder/index.js:72:12
at new Promise (<anonymous>)
at write (/data/pupp_test/node_modules/puppeteer-recorder/index.js:71:3)
at module.exports.record (/data/pupp_test/node_modules/puppeteer-recorder/index.js:44:11)
at processTicksAndRejections (internal/process/task_queues.js:82:5)
at async /data/pupp_test/test2.js:22:5
my ffmpeg is configure with "--enable-libvpx --enable-libvorbis"
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-4)
configuration: --enable-libvpx --enable-libvorbis
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
libswscale 5. 5.100 / 5. 5.100
libswresample 3. 5.100 / 3. 5.100
Hyper fast Audio and Video encoder
how to resolve the error(Cannot call write after a stream was destroyed)?
Solved.
to reinstall libvpx。see in https://trac.ffmpeg.org/wiki/CompilationGuide/Centos
git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git
cd libvpx
./configure --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
make
make install

ffmpeg Unrecognized option 'tune'

I'm using ffmpeg on node.js. And option '-tune zerolatency' for low latency
But when I insert "-tune zerolatency", I get an error: Unrecognized option 'tune'.
Can anyone help? I have no idea what to do.
Here's the script.
let token = '#!#!##%#$##%#^^!#$##$##$##$^##$'
let option = [
'-i', 'pipe:0', //mpeg4
'-acodec', 'copy',
'-vcodec', 'copy',
'-s', '284x164',
'-preset', 'ultrafast',
'-tune', 'zerolatency',
'-f', 'flv',
'rtmp://localhost:1935/s/' + token,
];
ffmpeg = spawn("ffmpeg", option);
ffmpeg.stdin.write(streaming_data);
Here's the ffmpeg version
ffmpeg version N-82166-g894e7ef Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.8.3 (GCC) 20140911 (Red Hat 4.8.3-9)
configuration: --prefix=/usr/local/ffmpeg_build --extra-cflags=-I/usr/local/ffmpeg_build/include --extra-ldflags=-L/usr/local/ffmpeg_build/lib --bindir=/usr/local/bin
libavutil 55. 35.100 / 55. 35.100
libavcodec 57. 65.100 / 57. 65.100
libavformat 57. 57.100 / 57. 57.100
libavdevice 57. 2.100 / 57. 2.100
libavfilter 6. 66.100 / 6. 66.100
libswscale 4. 3.100 / 4. 3.100
libswresample 2. 4.100 / 2. 4.100
tune is a private option of certain encoders, not a common option. Since you are copying the video stream over, no encoder is invoked, and so the option isn't recognized. Similar is true of the size and preset options, although those are common options and they are simply inert instead of unknown in this command.
If you change '-vcodec', 'copy' to '-vcodec', 'libx264', all three options will take effect. But this will transcode the video, so time and CPU use will increase.

Can't obtain information with ffmpeg from Smooth Streaming Manifest file

I'm trying to get details for a stream of protocol Microsoft Smooth Streaming, with ffmpeg tools (ffprobe and ffplay) but when I run the next command:
ffprobe -i http://b31282.cdn.telefonica.com/31282/Manifest
I obtain this:
http://b31282.cdn.telefonica.com/31282/Manifest: Invalid data found when processing input
I assume that this Manifest file is correct and complies with standards of Smooth Streaming . This is a brief extract of Manifest file:
<SmoothStreamingMedia MajorVersion="2" MinorVersion="0" Duration="0" TimeScale="10000000" IsLive="TRUE" LookAheadFragmentCount="2" DVRWindowLength="6000000000" CanSeek="TRUE" CanPause="TRUE">
<Protection>
<ProtectionHeader SystemID="9a04f079-9840-4286-ab92-e65be0885f95">oioAAAIAAQCUAjwAV...</ProtectionHeader>
</Protection>
<StreamIndex Type="audio" Name="audio_esp" Language="esp" Subtype="" Chunks="0" TimeScale="10000000" Url="QualityLevels({bitrate})/Fragments(audio_esp={start time})"></StreamIndex>
<StreamIndex Type="video" Name="video" Language="" Subtype="" Chunks="0" TimeScale="10000000" Url="QualityLevels({bitrate})/Fragments(video={start time})"></StreamIndex>
</SmoothStreamingMedia>
Is a Fedora 21 and ffmpeg version and configuration is:
ffmpeg version git-2015-02-27-d490b26
configuration: --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264
libavutil 54. 19.100
libavcodec 56. 26.100
libavformat 56. 23.105
libavdevice 56. 4.100
libavfilter 5. 11.102
libswscale 3. 1.101
libswresample 1. 1.100
libpostproc 53. 3.100
PD: I prefer use only ffmpeg tool
PD2: Same thing with a Microsoft Smooth Streaming Manifest file:
ffmpeg -i http://mediadl.microsoft.com/mediadl/iisnet/smoothmedia/Experience/BigBuckBunny_720p.ism/Manifest
Invalid data found when processing input
Thanks for all
smoothstreaming demux is not supported.
Here's the official doc, smoothstreaming doesn't show up in the demux part. And here's the output when you asking what types are supported
chang#p300a:~/git-root/FFmpeg$ ffmpeg -formats|grep smooth
ffmpeg version n4.2.3-3-gbf7ed52c25 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --enable-libv4l2 --enable-zlib --enable-vaapi --enable-pthreads --enable-vdpau --enable-xlib --enable-libv4l2 --enable-libx264 --enable-gpl --enable-gnutls --enable-openssl --enable-nonfree --disable-gnutls --enable-libxml2 --enable-libdrm
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
libswscale 5. 5.100 / 5. 5.100
libswresample 3. 5.100 / 3. 5.100
libpostproc 55. 5.100 / 55. 5.100
E smoothstreaming Smooth Streaming Muxer

Resources