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.
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”.
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!
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
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:
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:
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:
Show it what the target clip is with:
-i [clip name and file extension]
copy all the values of the original video clip with:
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 in short:
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” keys inside the terminal window.
If you want to record the audio as well, 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:
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
For your system just replace the device ID with your own audio device, and leave all the rest the same.
To stop the recording, you may hit the “Ctrl + C” keys again
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
Here are a few other useful tutorials on the subject:
Converting video/audio using avconv
Useful FFmpeg Commands
Convert Videos For Import Into Lightworks With 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.
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!