The Autodidacts

Exploring the universe from the inside out

Convert media files like a geek: a guide to video transcoding with Avconv

Avconv is a powerful audio and video converter with a mind-bending number of possible configurations and uses. For those of you who took one look at the 7 page table of contents and threw your hands in the air, here’s a straightforward introduction and some handy conversion snippets.

Cartoon: Convert media files like a GEEK


What is Avconv?

Avconv is a command-line tool for transcoding multimedia files. It stands on its own, but it’s part of the bigger Libav project, a set of free & open-source libraries for dealing with multimedia formats of all sorts.

The Libav project was forked from the FFmpeg codebase in 2011. There have been many debates about the fork, and comparisons between the two projects, the politics of which I won’t go into here. This tutorial is written and tested for Avconv, but most of the commands will work with FFMpeg as well — just replace “avconv” with “ffmpeg”.

Install Avconv

Open up terminal by hitting Ctrl + Alt + T and type the following:

sudo apt-get install ffmpeg libav-tools

When prompted, enter your password, and confirm installation (by hitting Y and then Enter). That’s all there is to installing Avconv!

The Basics

Here’s the basic command format:

avconv [options] [[infile options] -i infile] [[outfile options] outfile]

A simple example conversion script would look like this:

avconv -i GOPR7617.MP4 -qscale:v 2 GOPR7617-converted.OGG

If that looks a bit confusing, let me explain.

Avconv is called at the beginning. Next are the input commands followed by -i and path to the input file. Then we have the output options (a simple video quality setting in the example) and the output file to be created. This will run the script in the current folder and output the new file there with the name that you specified — in this case, “GOPR7617-converted.OGG”.

Essential Avconv parameters

Video quality: Set the video quality of the output file with -qscale:v, followed by an integer between 1 and 31 (1 = best, 31 = worst). To output the highest possible quality, you’d use -qscale:v 1.

Framerate: Set the output framerate with -r. To set the output framerate to 30 fps (frames per second) you would put -r 30 in the outfile options section.

Video and Audio codecs: Tell avconv what codecs to use with -c:v for video and -c:a for audio. For example, -c:v mpeg2video -c:a mp2 would let avconv know you want it to encode the video with the mpeg2video codec and the audio with the mp2 codec.

Useful Avconv Conversion Snippets

Here are some handy Avconv snippets for the common media conversions:

Create an Image Sequence from Video

For this snippet, replace [INPUT] with the filename of your target clip. Avconv will then convert the video into a sequence of PNGs. The “%04d” means it will automatically give the images four digit iterated filenames.

avconv -i [INPUT] %04d.png

Trim a Video File

This is a useful way to trim off the unwanted section of a video file without going through the import and export process of opening it with an editor, and without even needing to re-encode it.

With Avconv there is one parameter for setting the “in point” and one for the “out point”. You can use just an “in point”, just an ”out point”, or you can use both, depending on what part of the video you’d like to keep and what part you’d like to ditch. For example, if you shot a performance by your favourite band, but they were having difficulties getting their act together and started late, which meant your video clip had a long section of frazzled musicians plugging in wires and testing their mics rather than actually playing music, you might want to trim off that beginning part of the clip and get straight to the action.

To do this you use the -ss parameter to set the “in point”. After -ss you put the time (in hours:minutes:seconds) that you would like the clip to start at. For example:

-ss 00:10:00

would trim the first 10 minutes off the clip, leaving the rest intact.

-t is what you use to set the “end point”. If you have a 10 minute video clip, but only want to keep the first 5 minutes of it, you’d put:

-t 00:05:00

which would trim off the last five minutes of the clip.

If there’s a clip you’d like only the middle section of, you can use both commands together, like this:

-ss 00:05:10 -t 00:10:00

For the rest of the script call the program at the beginning with:

avconv

Show it what the target clip is with:

-i [clip name and file extension]

copy all the values of the original video clip with:

-codec copy

and specify the output clip with a filename and an extension that resembles the original but isn’t the same. In my case I used the same filename but added a “-converted” onto the end of it before the file extension. Make sure you include the file extension of the target clip otherwise the command will not work.

avconv -i FM180050.AVI -ss 00:05:10 -t 00:10:00 -codec copy FM180050-converted.AVI

Convert Entire Folder with Avconv’s For Loop

There’s a simple command for running Avconv on everything in a target folder which has the specified file extension, and outputting the converted files with an auto-generated filename that matches the input files. This is useful for converting a whole folder of video files to some specific output format. You can do this with the following snippet:

for i in *.AVI; do avconv -i "$i" [OUTPUT SETTINGS] $(basename "$i" ".AVI").mp4 ; done

Record Screencast with avconv

avconv -f x11grab -r 25 -s 1920x1080 -i :0.0 -vcodec libx264 -threads 4 $HOME/output.avi

Now let’s explain the command:

avconv -f x11grab is the default command to capture video from the X server.
-r 25 is the frame rate you want, you may change it if you like.
-s 1920×1080 is your system’s screen resolution, change it to your current system resolution, it’s very important to do this.
-i :0.0 is where we want to set our recording start point, leave it like this.
-vcodec libx264 is the video codec that we’re using to record the desktop.
-threads 4 is the number of threads, you may change it as well if you like.
$HOME/output is the destination path where you want to save the file.
.avi is the video format, you may change it to “flv”, “mp4”, “wmv”, “mov”, “mkv”.

After you enter the command, the recording will start automatically as a process running from the terminal, in order to stop it, hit Ctrl + C inside the terminal window.

Capture System Audio

With this command you can record high-quality MP3 or WAV files of whatever audio is running through your system. This is handy for all sorts of things such as web casting¸ Skype calls¸ and video tutorials.

First run this command to list all the available input sources for the audio.

pactl list sources | grep Name:

It should return a result that looks something like this:

Name: alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
Name: alsa_input.pci-0000_00_1b.0.analog-stereo

Then record audio with avconv by using the exact device name after the -i switch. If you'd like to record system audio, chose the alsa output device, and if you'd like to record audio from your microphone, choose the alsa input device. In my case the command would be:

avconv -f alsa -ac 2 -ar 48000 -f pulse -i alsa_output.pci-0000_00_1b.0.analog-stereo.monitor -acodec libvorbis -aq 6 out3.ogg

Replace the device ID with your own audio device and leave all the rest the same.

To stop the recording¸ hit Ctrl + C.

Extract Audio from a Video

This command lets you strip the audio tracks off a video and save them as an ogg vorbis file.

avconv -i INPUT-film.mp4 -qscale:v 2 -acodec libvorbis -r 30 OUTPUT.ogg

Lightworks

Lightworks is a cross platform non-linear video editor. It’s the best option I’ve found for Linux, but it has some drawbacks. It’s transitioning to open source, and the codec support is still fairly limited. This means it’s pretty picky about what types of files it will deal with. Here are the scripts I run my files through before and after editing to get around this.

Lightworks/HTML5 conversion profile. I run this profile on Lightworks exports before uploading to web, when I’m going to use them with the default HTML5 video player.

avconv -stats -i [INPUT] -qscale 20 -vcodec libtheora -acodec libvorbis -r 30  output.ogg

Lightworks Input and Frame Rate Change. This converts media files to an audio and video codec that’s compatible with Lightworks.

avconv -i FM170692.AVI -c:v mpeg2video -pix_fmt yuv422p -g:v 1 -q:v 1 -qmin:v 1 -c:a mp2 -ac 2 -r 30 -r:a 48000 -b:a 384k output.mp4

Looped Lightworks Input and Frame Rate Conversion. This command is good for converting files from many different types and codecs into the Lightworks compatable mp4 format and ensuring the frame rate is at 30fps.

for i in *.AVI; do avconv -i "$i" -c:v mpeg2video -g:v 1 -q:v 1 -qmin:v 1 -r 30 -c:a mp2 -ac 2  -b:a 384k $(basename "$i" ".AVI")_30fps.mp4; done

Useful References

Here are a few other useful tutorials on the subject:

  1. Converting video/audio using avconv

Once you’ve got a hang of the basics, it might be worth taking a look at the more in-depth avconv command documentation mentioned earlier, to learn about avconv’s more exotic capabilities. And there’s always the avconv manpage (man avconv), if you want to learn avconv the hard way.

Conclusion

I hope this tutorial has been helpful for others trying to wrap their heads around Avconv. If you have any questions or suggestions, send me an email or leave a comment below. Thanks for reading!

Sign up for updates

Join the newsletter for curious and thoughtful people.
No Thanks

Great! Check your inbox and click the link to confirm your subscription.