For testing, I want to convert an MP3 and WAV file I have to Opus, what are the steps to doing this?
-
Converting an mp3 to an opus is a horrible idea. – Evan Carroll Jan 11 '13 at 16:44
-
1@EvanCarroll Hence the part that says "For testing" ;) – Luis Alvarado Jan 11 '13 at 18:18
-
3@EvanCarroll That depends on what you want to achieve and the quality of the input material. If you're looking for an absolute audiophile solution, you probably never want to consider Opus in the first place. Saying it is a horrible idea is just a horrible statement, when there is no explanation. – LiveWireBT Mar 04 '13 at 22:05
-
1@LiveWireBT Says that encoding an mp3 in opus is not a horrible idea unless you're looking for an absolute audiophile solution is just a horrible statement, when there is no explanation. – Evan Carroll Mar 04 '13 at 22:34
-
1re: the top. Encoding an mp3 in Opus is a bad idea because you compound the failures of both formats. If you assume MP3 is ABX %5 at 128 kbps, then the Opus is 2% ABX at 128 kbps, the final stream is significantly higher than the 2% rate by Opus, or the 5% rate by MP3. Moreover, it's likely that file has no size advantages whatsoever, and the encoding time is compounded by the second encoding. You get **no benefits whatsoever if you have the mp3 input.** And, **with the original, you get no benefits by using MP3 as an intermediary format.** – Evan Carroll Mar 04 '13 at 22:40
-
3@EvanCarroll Opus has specialized support for speech, MP3 doesn't. This means that you can reduce your podcasts and audiobooks *significantly* in size without any noticeable loss in quality. I fail to see how that is a horrible idea. – Christian Mar 22 '14 at 05:10
-
@Christian Opus deos not have *specialized support* for speech. And, the problems of compounded failures still persist: anytime you encode in opus, you'll incur a loss in quality whether or not it is noticeable is subjective. – Evan Carroll Mar 22 '14 at 05:22
-
5@EvanCarroll "automatic detection of speech or music to decide which encoding mode to use" from http://opus-codec.org/. And of course you will incur a loss in quality. That's why it's a lossy codec. You said that this was never justifiable though and I gave you a good example where size is a huge issue, quality of secondary importance and a lossless source not available. – Christian Mar 22 '14 at 10:21
6 Answers
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
-
-
1
-
-
1To do this on a bunch of flac files: `for f in *.flac; do ffmpeg -i "$f" -f wav - | opusenc --bitrate 140 - "${f%.flac}.opus"; done` (I chose 140 as a bitrate because according to [this](https://wiki.xiph.org/Opus_Recommended_Settings) it's more than enough for stereo files. – Joschua Aug 14 '18 at 14:42
-
In Ubuntu 20.04, it works directly with `ffmpeg -i input.mp3 -f opus output.opus`. `sudo apt install ffmpeg` (which includes `libopus0`). – darkdragon Apr 13 '20 at 19:39
-
1@darkdragon: thank you, added this. Actually libopus was already included in libavcodec from 16.04. – Takkat Apr 13 '20 at 20:38
-
This isn't Ubuntu related but might be good for Windows users that end up here via Google: piping from ffmpeg to opusenc works on Windows too, but in my case *only* in cmd.exe, not in Powershell. – pzkpfw Dec 05 '20 at 11:34
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.mp3sets the input file.-map 0:awill select all audio streams (a) from the input file0. Read more about-mapon https://libav.org/avconv.html#Advanced-options-codec:a opusselects the opus encoder for the audio streams (a). Read more about-codecon https://libav.org/avconv.html#Main-options.-b:a 100ksets the audio's bitrate to 100 kilobit/s. Read more about-bon https://libav.org/avconv.html#Codec-AVOptions-vbr onturns 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 VBRfile.opussets 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
- 1,147
- 11
- 23
-
1Works on Ubuntu 14.04 perfectly! Could you explain what `-map 0:a` does? (And maybe detail the entire line?) – 425nesp Apr 21 '14 at 06:26
-
@piña I have updated the answer with an explanation of the arguments and an additional example. – Daniel Jonsson Apr 21 '14 at 11:05
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
yasmfrom 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-devThere 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,cdto the folder, run./configure && makeand 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-aacpackages before you start compiling.It is critical that you compile and install
x264,fdk-aac,libvpxandopusbefore you buildffmpeg, as those libraries will be used in the build.Do not forget to add
--enable-opusto the configure options when you run theffmpegcompilation.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
ffplayto play any opus files you create.
-
ffmpeg is a bad solution for anything there is a replacement for. – Evan Carroll Jan 11 '13 at 16:43
-
2@EvanCarroll What do you mean by this? It's only someone's decision to deprecate it: ffmpeg is still as good as libav, which is just a fork of it. – Jan 11 '13 at 16:44
-
1A great answer, and thanks for the reminder on the Yasm requirement. x264 increased the minimum version to 1.2.0 on their last push. Guide updated with Yasm instructions. – llogan Jan 11 '13 at 19:38
-
@Mik I think he wanted to say that the stand alone encoder will give better results. – LiveWireBT Mar 04 '13 at 21:57
-
I saw some people mentioned having "opus not found" error when following the instructions to compile ffmpeg. I had the same issue in Ubuntu 12.04, and later found out the PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" needs a slash at the end. – Jul 15 '13 at 21:54
That's how I do it:
- First, open a terminal in the same directory where your audio files are.
- Then, type this command:
$ opusenc --bitrate 320 --max-delay 10 "18 - Soul Asylum - Runaway Train (Album Version).flac" "18 - Soul Asylum - Runaway Train (Album Version).opus"
EDIT:
For Audiophiles:
$ opusenc --bitrate 510 --max-delay 10 "18 - Soul Asylum - Runaway Train (Album Version).flac" "18 - Soul Asylum - Runaway Train (Album Version).opus"
No need to specify --maxdelay 10 option because opusenc do this by default.
Console Output for this file conversion (--bitrate 320):
Encoding using libopus 1.1.2 (audio)
-----------------------------------------------------
Input: 44.1kHz 2 channels
Output: 2 channels (2 coupled)
20ms packets, 320kbit/sec VBR
Preskip: 356
Encoding complete
-----------------------------------------------------
Encoded: 4 minutes and 22.4 seconds
Runtime: 8 seconds
(32.8x realtime)
Wrote: 10955530 bytes, 13120 packets, 13124 pages
Bitrate: 317.691kbit/s (without overhead)
Instant rates: 1.2kbit/s to 510.4kbit/s
(3 to 1276 bytes per packet)
Overhead: 4.89% (container+metadata)
It's super fast! Less than 8 seconds with a complexity of 10 (Encoding computational complexity (0-10, default: 10). Zero gives the fastest encodes but lower quality, while 10 gives the highest quality but slower encoding) and a maximum delay time of 10ms (Maximum container delay in milliseconds (0-1000, default: 1000)), so if you skip time in a song, the clipping effect will have a duration of 10ms so it is inperceptible (try with 1000 and hear the difference skipping time with your mouse). Bitrate is VBR by default. 320kbps worked for me so is optional, play with this number:
--bitrate N.nnn => Target bitrate in kbit/sec (6-256 per channel)
By the way, encoding from MP3 to OPUS is not a good idea, it's not going to sound better, their compression algorithms are way too different. But from FLAC or WAV or any other Lossless Audio Format, that's another story.
Note: To encode another file, just press the Up Arrow in the same terminal to call the last command and change the name of the input and output files.
If you are looking for a ffmpeg/avconv GUI, maybe TraGtor is what you need.
You can also check the spectogram differences between Lossless and Lossy formats at high bitrates with Spek or Audacity.
- 311
- 2
- 14
-
It didn't work for me. I get `Error parsing input file: Hardvapour remix-.mp3` I'm using 16.04 – Sarah Szabo Mar 21 '18 at 02:08
-
If only the mp3 to opus route is needed, mpg123 can do the decoding to wav/pcm.
mpg123 -w - input.mp3 | opusenc - output.opus
For the unfamiliar the dash "-" functions as stdout on the left to be piped to opusencs stdin on the right.
Of course ffmpeg is excellent for general media conversion and editing, but its install size and usual distribution dependencies also have a bigger footprint.
- 469
- 3
- 6
- Find a CD (or flac with CDEMU) with the same content as mp3.
- Instal
opus-toolsandAudex. Open Audex and add a new profile called Opus, add Command pattern;
opusenc $i --comment="TRACKNUMBER="$trackno"" --artist "$artist" --album "$title" --title "$ttitle" --date "$date" --picture "$cover" $o
and suffix opus
- let it rip!
- 9
- 2