Contents

What is it? Video Identification

Often it is helpful to know what your starting point is. If you have a video with unknown characteristics, you can find out more about it by doing something like this.

mplayer -identify -vo null -ao null -frames 0 video.avi

This should tell you things like the codecs, bitrates, and the resolution.

You can use this with something fancier. Here is how to find out how many frames a video contains.

mplayer -identify -vo null -ao null -frames 0 mysteryvid.mp4 \
| awk 'BEGIN{FS="="} \
       /ID_VIDEO_FPS/{rate=$2} /ID_LENGTH/{time=$2} \
       END{print int(rate*time)-1}'

If the video is on a DVD, use acidrip and select the "copy" options for video and audio codecs. This will give you the video off the disc with unmodified characteristics. Query those with the command above and see what’s really there.

Also consider this.

avconv -i video.avi

Pitivi

This is a full featured graphical video editor that I found did a great job at graphically presenting various video elements and then stitching them together in specific customized ways. You import elements like video clips, still images, and audio clips and then arrange them on the movie time line. Then "render" the video. Worked great for a slide show with irregular timed slides. Just record the audio first with Audacity and then move the slides into place and adjust their length on screen with Pitivi.

Backing Up Stubborn DVDs

Some DVDs really piss me off. It’s important that you use a DVD player that is not so fussy about disks and will just follow your instructions to read all the 1s and 0s off the accursed disk. Here’s what you do:

ddrescue -n -b 2048 /dev/dvd output_raw.iso
dvdbackup -M -i output.iso -o dvd_structure
mkisofs -dvd-video -o clean_dvd.iso dvd_structure

Been liking acidrip lately. Good GUI for all the complex mess.

Here’s a project that might be helpful: k9copy

MPlayer

You might consider vlc if mplayer is problematic.

Finding Excerpts To Extract From Larger Video

Often you record all kinds of junk and the cool thing you really wanted is buried in there. Thanks to mplayer or some other clever player with on screen display ("o" in mplayer), you know where exactly you want your excerpted video to begin and end.

This worked for me to find the correct subset clip. This was done on another machine so I could control mplayer from a different location than where it was running full screen.

DISPLAY=:0.0 mplayer -ss 372  -endpos 25 -fs Encode_1080P_4.mp4

The 372 is the starting position in seconds and the 25 is the duration of the clip.

mplayer with the webcam

If your webcam puts a feed in an obvious place, mplayer can probably do nice things with it. Here are some examples.

This takes a single snapshot off the webcam.

mplayer -frames 1 -vo jpeg:outdir=/home/xed/capturedir/ tv://

Here’s a little script that has helped me.

takewebcamphoto
#!/bin/bash

# Need to capture several frames since the first couple are duds.
mplayer -quiet -nosound \
    -tv device=/dev/video0 \
    -ss 143 -frames 3 -vo jpeg:outdir=. tv:// \
    2>/dev/null >/dev/null
# Pick out last one.
mv $(ls 0000[0-9]*.jpg|tail -n1) webcam-$(date '+%Y-%m-%d_%H%M%S').jpg
# Delete the duds. Careful! This isn't very cautious.
rm 0000[0-9]*.jpg

Watch the camera and specify where it is.

mplayer -tv device=/dev/video tv://

Is your camera upside down? This is common with a USB microscope. This might help.

mplayer -vf flip,mirror -tv device=/dev/video0 tv://

Got no graphics? Have no fear! Use ASCII using libcaca.

mplayer -vo caca tv://

MEncoder

Mencoder seems to be related to mplayer but Debian seems to have stopped carrying it. What a mess!

Rotating with mencoder

Sometimes you take a video with your phone and it should be in portrait, but players play it in landscape. This command rotated a goofy 3gp video file from my Andriod phone:

mencoder -vf rotate=1 -ovc lavc -oac pcm fromphone.3gp -o portrait.mp4

Adding And Removing Sound Track

Provide your own sound track:

mencoder -vf rotate=1 -oac copy -audiofile st.mp3 -o out90.avi -ovc lavc in.avi

Mute

Or mute the thing entirely:

mencoder -v -nosound -o outvid.avi -ovc lavc invid.mp4

Here is the ffmpeg/avconv way to remove the sound (note the -av option).

avconv -i invid.avi -vcodec copy -an outvid.avi

This rotates and cuts off the first 3 seconds and nulls the sound track:

mencoder -vf rotate=2 -ss 3 -ovc xvid -oac copy -audiofile /dev/null \
-o t_xed.avi -xvidencopts bitrate=300:pass=2 MVI_1393.AVI

Note that -endpos can be used for clipping the end off video.

Sometimes the -ss option doesn’t really work when I think it should. Here is a way to cut off the beginning of a video. This cuts off the first half a second.

mencoder -v -ss 00:00:20 -nosound -o outvid.avi -ovc lavc invid.mp4

avconv

This article sums up the Real Housewives of libav/ffmpeg quite well: "…it was actually pretty funny to see them acting like children, even though the situation was kind of serious…The main problem is that external projects who want to support both FFmpeg and Libav are just fucked…" Ya, so this is tricky. It sounds like the ffmpeg lead maintainer was being a bit of an imperious wanker and then the avconv guys forked it (or something), started up with their own management style problems, and proved why he needed to be.

I’m finally breaking out avconv to its own section because I use Debian enough to care and I find enough irritating differences.

Resizing

Here is the avconv way to resize. This shrinks the geometry with -s and removes the audio with -an. This size is quarter HD and used in some driving videos I’ve worked with.

avconv -i dashcamHD.mov -s 960x540 -an dashcam_qHD.mp4

Extracting subset

Here’s the avconv way I got working.

avconv -ss 00:06:00  -i wholegame.mp4 -t 00:00:30 -acodec copy -vcodec copy -async 1 dunk.mp4

Note here that the video is only 30s long. Also note that it is important that the -ss come before the -i option.

ffmpeg

ffmpeg is a great tool for converting video from something to something else. See the ffmpeg documentation.

Real Housewives Of FFmpeg

Note
It seems that ffmpeg has gone through some crazy reorganization. It’s hard to tell what’s really going on but it seems there was a fork to avconv which used the Debian package called libav-tools. There was a ffmpeg package that just provided backward name compatibility to avconv. Then there seems to have been something of a reconciliation. Now it looks like there may be an ffmpeg package that is the real thing again, now no longer forked. Whew! The active ffmpeg web site seems like it’s still going strong. Doing a Debian apt-get install libav-tools in Feb 2017 produced a working avconv and no ffmpeg. Here is a discussion of this situation.

Install

If you need to download FFmpeg from source here’s the official web site.

If you’re using Debian or Ubuntu and really need FFmpeg (for OpenCV for example), you may need to compile it from scratch. It’s pretty polite and behaves as you’d expect. You do need to put some prerequisites in place first. Here’s a look at how it’s done.

apt-get install yasm pkg-config
# Get latest URL from: https://ffmpeg.org/download.html
wget http://ffmpeg.org/releases/ffmpeg-3.2.4.tar.bz2
./configure
make
sudo make install # Puts things in `/usr/local/` by default.

This should give you ffmpeg, ffprobe, ffserver.

Problems with video in OpenCV? That can be related to FFmpeg.

sudo apt-get install libopencv-dev python-opencv

If you’re using a system that has ffmpeg properly provisioned you might be able to just install it with the package manager. Here are some packages I have needed to consider to get things working properly.

sudo apt-get install  libavcodec-dev libavcodec-extra-52 libavcodec-unstripped-52

GUI

Check out FFmpeg Little Helper for a web based GUI that generates the correct command line.

Shrinking Videos

A good way to shrink videos from the Canon A590 to send to fb:

ffmpeg -i mvi_0378.avi -f avi -vcodec mpeg4 -s 320x240 -r 20 -ar 22050 -b 1000000 ski2.mpg

Another application to shrink videos to save space (factor of 5 without noticeable quality loss):

ffmpeg -i inputfile.avi -ar 32000 -s 480x360 -b 1252.4k outputfile.avi

The -s is the new size. The -ar is the audio bitrate.

Extracting subset

Here is a way to select a portion of a video to make a smaller one:

ffmpeg -i SK8.mp4 -ss 00:08:10.0 -vframes 5100  -acodec copy -vcodec copy -async 1 OSK8-test.mp4

Note that ffmpeg has a -t option to set the duration time of the video, but I’ve never gotten it to work and have seen reports of others with the same problem. But -vframes does seem to cut off the video at the right place. It’s annoying they don’t just have start time and stop time which would make sense. To get the correct vframes value you have to figure out the duration in seconds you want and then multiply by the frame rate (often 30). To get frame rate, play the video with mplayer and watch its console output for "FPS".

JPEG to Video

The following technique is useful for making stop motion animation videos and videos of slide shows. It takes a list of jpeg files (and only jpeg files as I understand it) and converts them into a video format. The following was able to take hundreds of jpegs and make them into a video that could be uploaded to YouTube.

avconv \
-i myvid.mp4 \
-ss 00:05:05 \
-t 00:00:03 \
-r 120 \
-s 1280x720 \
-f image2 \
frame_%d.jpg

The -r is the framerate, here 120 frames per second (30 is common). These particular settings were to extract video from a GoPro. Note that with this starting offset (-ss) the program can sit there apparently doing not much of anything for quite a while until it gets to the relevant section; I think it must calculate the previous frames since many codecs are basically diffs.

Note
There may be other ways of doing this, but the easiest is to name all the jpegs to use 001.jpg, 002.jpg, etc. If the sequence is broken, ffmpeg will stop encoding. So make sure the names are in order. (Look into using Chris' numbfile program. Try numbfile -z3 -s.jpg *jpg if less than 1000 images).
Note
If you need an animated png, mplayer seems to do that too. Look in the man page for the png video output setting.

This seems to work for PNG:

ffmpeg -r 10 -b 64k -qscale 2 -i torcs-0001-0000%04d.png snakeoil.mp4

This makes a large but high quality video. I’ll let YouTube sort out the details.

Video to JPEG

Just like the other way around, you can pull still images out of video.

ffmpeg -i video.webm -ss 00:00:07.000 -vframes 1 thumb.jpg

Here’s another example. This one takes a still every second (fps=1). The qscale:v is a quality specifier. Apparently 2 is the highest.

ffmpeg -i in.mp4 -qscale:v 2 -vf fps=1 stills%04d.jpg

Note that stuff like fps=1/2 for an image every two seconds.

Video to MP3

Extract mp3 track of the video to sound files with:

ffmpeg -i DubFX-Made.flv -acodec copy DubFX-Flow.mp3

This assumes that the video has an mp3 encoded sound track. YouTube appears to do this. Here’s a more complicated way to do this that might be necessary:

ffmpeg -y -i INPUT.flv -acodec libmp3lame  -ar 22050 -ab 128000 OUTPUT.mp3

Here’s a function that can turn Youtube videos into mp3 files on the fly:

function ripme {
    youtube-dl -q -o - "$1" | \
    ffmpeg -y -i - -acodec libmp3lame  -ar 22050 -ab 128000 "$2.mp3"
} # End function ripme.
# Call like so:
ripme http://www.youtube.com/watch?v=CknuCTRAW_I Dire_Straits-Walk_Of_Life

To check what streams, audio and video, are in a video file:

ffmpeg -i inputvideo.flv

To actually convert some other audio stream:

ffmpeg -i inpuvvideofile.flv -ab 128k outputaudiofile.mp3

Where -ab specifies the audio bitrate.

To extract a subset of video to a new video:

mencoder -ss 2693.0 -endpos 1350.0 All_Four_Episodes.avi  -ovc copy -oac copy -idx -o Episode_Three.avi

Not that the -ss is the start always in seconds. The -endpos is more ambiguous. If an -ss is used, it means seconds running time of the subset. If -ss isn’t used, it means the same thing and it actually corresponds to endposition (time of subset from zero). This is why in the example, the "end position" is less than the -ss, starting position.

General ripping:

/usr/bin/mencoder dvd:// -dvd-device /dev/sr0 -ovc xvid -xvidencopts pass=2:bitrate=1000 -ni -alang en -oac mp3lame -lameopts vbr=3 -o moviename.avi

Animated GIFs

First use ffmpeg/avconv to decompose the video.

avconv -i example.mpeg -r 30 -s 160x120 /tmp/gif/out%04d.gif

This didn’t preserve color and probably degrades things. This takes longer, but retains color and makes a nicer final product.

avconv -i example.mpeg -r 30 -s 160x120 /tmp/tiff/out%04d.tiff

Then take the decomposed images and combine them with ImageMagick (which has pretty excellent documentation about animated gifs).

convert -delay 1x30 -layers Optimize /tmp/tiff/* -loop 1 example.gif

A loop setting of 0 will endlessly repeat.

youtube-dl

Also use youtube-dl to get videos from YouTube:

youtube-dl -o Nobodys_Fool.flv http://www.youtube.com/watch?v=MCOrtJMQmVs

Sometimes you get the annoying webm format that mplayer is not so good with (maybe that will get fixed). So it’s sometimes good to try and get a "better" file format, where better doesn’t necessarily mean image quality.

First you need to see what’s available:

youtube-dl --all-formats --get-filename http://www.youtube.com/watch?v=MCOrtJMQmVs
MCOrtJMQmVs-43.webm
MCOrtJMQmVs-35.flv
MCOrtJMQmVs-34.flv
MCOrtJMQmVs-18.mp4
MCOrtJMQmVs-5.flv

Then you need to download the one that makes sense:

youtube-dl -f 35 http://www.youtube.com/watch?v=MCOrtJMQmVs

Another useful option is -t which names the file after the title of the video instead of the messy (to us) hashed name.

Make sure you have

sudo apt-get install ffmpeg  libavcodec-extra-53

Also, this can take a Youtube URL and make an mp3 out of it. Very handy.

youtube-dl -t --extract-audio --audio-format=mp3 'http://www.youtube.com/watch?v=9hXXXXXXIv4'

Avidemux

After having way too much trouble doing simple track substitutions with the command line tools, I installed this:

sudo apt-get install avidemux

And it was able to put an mp3 over a video no problem. I didn’t figure out 1% of what this thing did but it seemed to work for that task pretty easily.

Webcam Tricks

Note
Some webcam problems can be traced to the BIOS. Obvious, but easily overlooked is whether the web cam is enabled in the BIOS. This also is a hint at how to do quite a bit to ensure web cam privacy (i.e. disable it). Also sometimes BIOS updating can be required for Linux to properly see a webcam, especially from a new laptop.

Cheese

sudo apt-get install cheese

This program is a nice GUI client to the webcam and an easy way to test to see if it’s working properly. Also it can take still snapshots which turns your laptop with a camera into a digital camera. There are "effects" that are helpful when the camera needs to be reversed or turned upside down, etc.

The Program Called "motion"

motion is a very excellent program that will turn on your camera and save what it sees only if there is something changing (if it sees "motion"). This is excellent for security cameras. Fairly easy to use on Ubuntu.

sudo apt-get install motion
sudo vi /etc/motion/motion.conf
sudo /etc/init.d/motion start
ls /tmp/motion # Find output here.

RecordMyDesktop

The utility recordmydesktop is for making screencasts of graphical sessions on Linux systems.

To install this on Ubuntu systems:

sudo apt-get install recordmydesktop
sudo apt-get install gtk-recordmydesktop

Since I was interested in recording the full screen, I found the best way to use this program was as follows.

First I enter the command to record following a sleep command. This gives me a chance to kick off the thing that I really want to be on the screen (perhaps a slide show or a game).

$ sleep 5; recordmydesktop --no-cursor --fps=5 --display=:0 \
--stop-shortcut=Control+s --s_quality=10 --output=myscreencasttest.ogv

Then before hitting enter, in another terminal (or with the previous command suitably in the background) I do this:

$ feh -FZ ~/slideshow/*png

Then before hitting enter here, I switch to the first terminal, hit enter, and then within my sleep time switch back here and hit enter and 5 seconds after starting the first command, it’s recording what I want.

Note
The man page seems to indicate that long options can be specified without an =. But I’m not so sure this is correct. The = does seem to work.
Warning
Before getting too excited about recordmydesktop you should test it out. I found that it sort of worked and perhaps would have been fine for some kinds of demos, however, I wanted to record a slide show at a specific irregular pace and some slides were missing and the audio sync was generally bad using feh or eog as the viewer.

Converting To A YouTube Acceptable Format

Use PiTiVi to "render" the ogv file into something YouTube will accept. The following settings might do the job.

Container: MP4Muxer [mp4mux]
Audio Codec: Ffmpeg ALAC (Apple lossless Audio Encoder) encoder [ffenc_alac]
Video Codec: Ffmpeg MPEG-4 part 2 encoder [ffenc_mpeg4]
Video codec bit rate: 10300000