How to convert a sound file to Opus
In newer Ubuntu releases the Opus codec is included in the libavcodec libraries that will be installed with ffmpeg. Audio encoding is then done with
ffmpeg -i infile.ext <options> outfile.opus
The audio converter shipped with the opus-tools can convert audio in raw, wave or AIFF format. The minimal syntax uses default settings:
opusenc input.wav output.opus
We may want to add a better bitrate as the default 96 kbps with the option --bitrate N.nnn
(for all options consult the manpage for opusenc).
To convert mp3 "on the fly". i.e. without creating a temporary file we can pipe the output from ffmpeg to opusenc like this:
ffmpeg -i input.mp3 -f wav - | opusenc --bitrate 256 - output.opus
Ubuntu 14.04 and Debian 8 ship with version 9 of libav-tools
in their repositories, and it has built-in support for Opus through the package libopus0
.
Example 1: Reencode an audio file as opus
With version 9 of libav-tools
and libopus0
installed you can simply, for example, do:
avconv -i file.mp3 -map 0:a -codec:a opus -b:a 100k -vbr on file.opus
What the options do
-i file.mp3
sets the input file.-map 0:a
will select all audio streams (a
) from the input file0
. Read more about-map
on https://libav.org/avconv.html#Advanced-options-codec:a opus
selects the opus encoder for the audio streams (a
). Read more about-codec
on https://libav.org/avconv.html#Main-options.-b:a 100k
sets the audio's bitrate to 100 kilobit/s. Read more about-b
on https://libav.org/avconv.html#Codec-AVOptions-vbr on
turns on variable bitrate. This is an option specific for libopus. Here are all options for libopus:$ avconv -h full | grep opus -A 11 avconv version 9.11-6:9.11-3+b2, Copyright (c) 2000-2013 the Libav developers built on Apr 6 2014 17:45:45 with gcc 4.8 (Debian 4.8.2-16) libopus AVOptions: -application <int> E..A. Intended application type voip E..A. Favor improved speech intelligibility audio E..A. Favor faithfulness to the input lowdelay E..A. Restrict to only the lowest delay modes -frame_duration <float> E..A. Duration of a frame in milliseconds -packet_loss <int> E..A. Expected packet loss percentage -vbr <int> E..A. Variable bit rate mode off E..A. Use constant bit rate on E..A. Use variable bit rate constrained E..A. Use constrained VBR
file.opus
sets the output file.
Example 2: Grab the audio from a video file and encode it as opus
Take the second stream of the first input (-map 0:1
), which is the audio stream. Encode it with libopus at 100 kbit/s with variable bitrate on:
$ avconv -stats -i linuxactionshowep309-432p.mp4 -map 0:1 -c libopus -b 100k linuxactionshowep309-432p-audio-only.opus
avconv version 9.11-6:9.11-3+b2, Copyright (c) 2000-2013 the Libav developers
built on Apr 6 2014 17:45:45 with gcc 4.8 (Debian 4.8.2-16)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'linuxactionshowep309-432p.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.33.100
Duration: 01:14:48.45, start: 0.042667, bitrate: 466 kb/s
Stream #0.0(und): Video: h264 (High), yuv420p, 768x432 [PAR 1:1 DAR 16:9], 330 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc
Stream #0.1(und): Audio: aac, 48000 Hz, stereo, fltp, 128 kb/s
Output #0, ogg, to 'linuxactionshowep309-432p-audio-only.opus':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf54.20.3
Stream #0.0(und): Audio: libopus, 48000 Hz, stereo, flt, 100 kb/s
Stream mapping:
Stream #0:1 -> #0:0 (aac -> libopus)
Press ctrl-c to stop encoding
size= 54360kB time=4488.47 bitrate= 99.2kbits/s
video:0kB audio:53875kB global headers:0kB muxing overhead 0.900602%
With the package mediainfo
installed:
$ mediainfo linuxactionshowep309-432p-audio-only.opus
General
Complete name : linuxactionshowep309-432p-audio-only.opus
Format : OGG
File size : 53.1 MiB
Duration : 1h 14mn
Overall bit rate : 99.2 Kbps
Writing application : Lavf54.20.3
major_brand : isom
minor_version : 512
compatible_brands : isomiso2avc1mp41
Audio
ID : 2104437746 (0x7D6F2BF2)
Format : Opus
Duration : 1h 14mn
Channel(s) : 2 channels
Channel positions : Front: L R
Sampling rate : 48.0 KHz
Compression mode : Lossy
Writing library : Lavf54.20.3
Opus on 12.04
On 12.04 (Precise), however, there are dependency problems with installing the opus codecs and tools, so I have found by far the best solution is the one that has become available very recently: compile the opus audio encoder and decoder as noted here, and build ffmpeg
with opus support by adding --enable-opus
to the configure options of ffmpeg
(as listed on the compilation guide).
I know that ffmpeg
is deprecated in Ubuntu in favour of Libav
, but compiling is a good way to get a fully functioning opus encoder/decoder integrated into ffmpeg
itself. You can then use it to convert files (first to wav) and then to .opus
. The documentation installed with libopus and ffmpeg will reveal all the options that can be used to convert files.
When converting files with ffmpeg
after compilation, you must specify -acodec libopus
or ffmpeg
will not use the opus codec:
ffmpeg -i pc.wav -ar 48000 -ac 2 -acodec libopus -ab 256k man.opus
You can then test the file created with
ffplay man.opus
Compilation Tips
There's no need to reproduce the guide here in its entirety, but it's worth noting one or two things:
You should first install the dependencies as listed (I omit
yasm
from the list: see my second point):sudo apt-get -y install autoconf build-essential checkinstall git libass-dev libfaac-dev libgpac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev librtmp-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libx11-dev libxext-dev libxfixes-dev pkg-config texi2html zlib1g-dev
There is one issue that should be pointed out: the git build seems to want
yasm-1.2
, and that is not available, so you have to compile the source from the official site, but it is simple. Just remove any installed versions ofyasm
, then unpack the downloaded archive,cd
to the folder, run./configure && make
and thensudo checkinstall
. If any other builds require the earlier version, you can just remove this version and install the repository version.It is necessary to remove any existing
libav
,ffmpeg
,x264
,libvpx
, orfdk-aac
packages before you start compiling.It is critical that you compile and install
x264
,fdk-aac
,libvpx
andopus
before you buildffmpeg
, as those libraries will be used in the build.Do not forget to add
--enable-opus
to the configure options when you run theffmpeg
compilation.The version of opus compiled was 1.1alpha, so you may need to re-compile the opus library and ffmpeg in the future again when a new version is released.
You can use
ffplay
to play any opus files you create.