Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

For testing, I want to convert an MP3 and WAV file I have to Opus, what are the steps to doing this?

share|improve this question
    
Converting an mp3 to an opus is a horrible idea. –  Evan Carroll Jan 11 '13 at 16:44
    
@EvanCarroll Hence the part that says "For testing" ;) –  Luis Alvarado Jan 11 '13 at 18:18
1  
@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 4 '13 at 22:05
    
@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 4 '13 at 22:34
    
re: 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 4 '13 at 22:40

4 Answers 4

up vote 9 down vote accepted

Per default the audio converter shipped with the opus-tools Install 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 avconv to opusenc like this:

avconv -i input.mp3 -f wav - | opusenc --bitrate 256 - output.opus
share|improve this answer

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 file 0. 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
share|improve this answer
1  
Works on Ubuntu 14.04 perfectly! Could you explain what -map 0:a does? (And maybe detail the entire line?) –  piña Apr 21 at 6:26
    
@piña I have updated the answer with an explanation of the arguments and an additional example. –  Daniel Jonsson Apr 21 at 11:05

Another option, if you are using 12.04 you can use the KXStudio PPA's to get the best selection of audio-related everything, and they now include backported opus-tools!

See http://kxstudio.sourceforge.net/

share|improve this answer
    
This sounds like an advert... –  papukaija Jul 15 '13 at 23:21
    
The one whose description says "DO NOT USE THIS PPA!"? launchpad.net/~kxstudio-team/+archive/ppa –  Christian Mar 22 at 5:06

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 of yasm, then unpack the downloaded archive, cd to the folder, run ./configure && make and then sudo 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, or fdk-aac packages before you start compiling.

  • It is critical that you compile and install x264, fdk-aac, libvpx and opus before you build ffmpeg, as those libraries will be used in the build.

  • Do not forget to add --enable-opus to the configure options when you run the ffmpeg 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.

share|improve this answer
    
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. –  user76204 Jan 11 '13 at 16:44
1  
A 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. –  LordNeckbeard Jan 11 '13 at 19:38
    
@Mik I think he wanted to say that the stand alone encoder will give better results. –  LiveWireBT Mar 4 '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. –  user175483 Jul 15 '13 at 21:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.