HOWTO Convert audio files

From LinuxReviews
Jump to navigationJump to search

A huge variety of file formats have been and are used to store and distribute digital audio files. Sometimes you need to convert between these. In most cases you shouldn't. Here's how you can do it if you need to.

First, a word of warning (or opinion)

The audio file formats WAV and FLAC has lossless which means they are an identical copy of the original audio source. A FLAC version of a CD will be identical to the orignal CD. Most other audio formats such as MP3, M4A and WMV are lossy formats which means parts of the original file are thrown out. Converting a MP3 audio file to Opus will further degrade audio quality. Avoid converting one lossy format to another unless you need to because you would like to play music in WMV format on a device which only supports MP3 files. Converting lossless audio files like FLAC to Opus in order to save space so you can store more audio on a portable device like a phone is fine.

Converting audio using a graphical program like VLC

The free software media player VLC for Linux/Windows/MacOS comes with a built-in file-convert which works really well for converting any audio file to MP3, MP4, FLAC, Vorbis, OPUS and WAV.

Select Media in VLCs menu then Convert / Save. Click + Add to add the audio file or files you'd like to convert. Vlc-convert-audio-files.png

Next, click the Convert / Save button at the bottom of the dialog. There you will be asked to select a Profile for your output file(s). The default audio profiles to choose from are just Vorbis, MP3, Flac and CD. The trick to using other more modern audio formats in this dialog is to press the "Create a new profile" on the right side of the Profile-selector. This brings up "Profile edition".

Vlc-profile-dialog.png

You could choose "Ogg/Ogm" under "Encapsulation" and click "Audio codec" and choose "Opus" and a Bitrate of 192kb/s to use the currently best lossy audio format. Name the profile something sensible such as Audio - Opus. Click Create to save it. You can now use that profile in the Convert dialog. Next, choose a Destination file to save to if you only selected one input file.

Vlc-audio-opus.png

All that's left is to click Start to get a new converted file.

VLC supports converting multiple files. Simply click Add in the Open Media dialog and select as many files as you would like. You will not be able to choose output filenames or destination when doing this; the files you convert will be saved in the same folder as the original using a different extension.

Converting audio files using the command-line using ffmpeg

The go-to tool for converting anything, audio, or video, using CLI in a terminal on Linux is ffmpeg. The most basic use-case of converting a file from one format to another is as simple as:

ffmpeg -i inputfile.wav -vn outputfile.m4a

Do note the -vn switch. It tells ffmpeg to not invoke any video-related code. You will in some cases get a video track showing a cover image added to the output file without the -vn option.

The audio format is chosen intelligently based on the container indicated by the file extension. You can use the option -codec:a to specify audio coder. You can use -c: as as short-hand for -codec:. You can use -b:a to specify a bitrate. To convert file to Opus you could use this example:

ffmpeg -i inputfile.flac -vn -c:a libopus -b 192k outputfile.ogg

Run the command ffmpeg -encoders to see all the formats ffmpeg can convert to. The amount of audio formats you can convert to using ffmpeg is staggering.

Batch converting a folder full of audio files

The simplest solution is to just create a little script file like one of the following examples.

File: flac2m4a.sh
#!/bin/bash
for a in *.flac; do
  ffmpeg -i "$a" -vn -codec:a aac -b:a 320k  \
  "${a[@]/%flac/m4a}"
done
File: flac2ogg.sh
#!/bin/bash
for a in *.flac; do
  ffmpeg -i "$a"  -vn  -codec:a libvorbis  -qscale:a 9  \
  "${a[@]/%flac/ogg}"
done

Converting audio files using the command-line using gstreamer

You're better off using FFmpeg for this. But if you must use Gstreamer (for the unlucky ones on Gnome), this is the way to do it.

gst-launch-1.0 -v filesrc location=music.wav ! decodebin ! audioconvert ! audioresample ! lamemp3enc target=quality quality=0 ! id3v2mux ! filesink location=music.mp3.


This will convert a wav file into mp3 format. But for every different reencode target the bins change, so you'll need the Gstreamer documentation to find out what bins and elements you need (See why I said FFmpeg was better for this?)


Questions?


avatar

Anonymous (c155e3000b)

31 months ago
Score 0

Hi, Thank you for sharing HOW TO Convert audio files. It’s helped me to solve my problems. By the way, I’m also writing an article about the Top 10 Online M4A TO MP3 Converter Tools. You can check the below links.

https://www....verter-free/
Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.