Benningtons.net

Stuff what I did

Linux Video Tips —

There are many open source solutions for recording and editing video. Here are a few that I prefer to use.

Recording:

I have a Raspberry Pi dedicated to running TVheadend. Not for streaming live TV (which it is very capable of) but because it provides an excellent recording service.
The Pi hosts a website on my home network where TVheadend presents its Electronic Programme Guide (EPG) showing TV shows for the week ahead. Individual programmes can be selected and set to record (to an attached USB hard drive), it can also be set to record whole series or to simply record any show that matches your search criteria.

There’s no need for me to repeat the TVheadend installation instructions but you may find these points useful.

– If you do use a Raspberry Pi then, as with any other Pi server, give it just one service to perform. The busier the Pi the greater the chance of disruption which could lead to it missing your favourite programme. My Pi has been rock-solid running TVHeadend non-stop for many months.

– Recordings are made via a USB TV tuner stick connected to an aerial (DVB-T), satellite (DVB-S) or cable (DVB-C). These small sticks can be power hungry and feel warm to touch, so on a Pi you need to use them via a powered USB hub. I use a cheap (<£10) Hauppage WinTV Nova stick which shows as a DiBcom 7000PC in the TVheadend config page.
– One DVB stick means one channel can be viewed or recorded at a time. So if you also use TVheadend to stream live TV remember that you could be interfering with planned recordings. TVheadend can be used with multiple DVB sources but I haven’t tried that, yet.

– To improve the reliability of the server I have removed all multiplexors (frequencies to monitor) and services (TV channels) that I don’t need or because their signal is too weak. Don’t worry, TVheadend configuration is fairly easy to follow and all done via its web page http://your-server-name:9981 (that’s port 9981 on your server).

– Finally, for a more reliable TV recording service you may want to consider adding a real time clock to your Pi or have a backup power supply, depending on how often you suffer from power-cuts.

Ripping:

Other than its speed, one of the reasons I stick with Puppy Linux as my day to day distro is the array of useful apps that it comes bundled with. They’re all selected for their speed/efficiency and I rarely need to install anything extra. [I also use Lubuntu as a more universal base for my programming and electronics tinkering and, whilst I used to use Arch Linux, I now use the cut-down Raspbian Server Edition (RSE) as the base distro for all my Pi’s.]

Puppy comes with the well named Asunder for ripping CDs and the less well named Pdvdrsab for ripping DVDs. The rsab stands for rip, shrink, author and burn; although I mainly use it to rip and then use avconv and other programs to convert and edit video files.

My only tip for using Pdvdrsab is to first use the dvd-info option to see what’s on the disc. When ripping it will default to just the title that contains the most chapters but seeing what other titles are listed (they’re just numbered not named) will give you a clue about what else to rip.

Downloading:

Another source of video files is of course downloading from the web. I won’t condone the use of pirate sites but on-demand TV services often come with download facilities so you can watch their programmes when off-net.
Often these sites are not Linux friendly (shame on them) but get_iplayer still works as a Linux command line tool for the BBC iPlayer service. Although the BBC seem determined to use our license fees to encourage us to only use Microsoft or Apple software, so I’m not sure how much longer get_iplayer will last.
get_iplayer can be used in conjunction with avconv to download and convert in one go but I prefer to run the two tasks separately.

Converting:

The variety of media container formats (MKV, VOB, FLV, etc…) and video and audio codecs is bewildering. Then there are choices that trade quality with file size, display aspect, data streams for sub-titles, etc, etc… So I don’t know which is best and I’m sure each will have their pluses and minuses.

Below is a breakdown of my usual conversion command, using the excellent avconv utility. As I say, I’m no expert and I’m not sure if these are the best choices for quality and freedom but I’m basically interested in keeping file sizes low, without sacrificing too much quality, whilst ensuring audio and video remain well sync’d and in a format that can be viewed on all the devices in our home and a format that can be streamed.

avconv
        -i input_filename		# input = file to be converted
        -y						# if the output_filename already exists, replace it
        -f mp4					# output file format (not needed if you have the right extension on your filename)
        -vcodec libx264			# video codec to use
        -threads 0				# use the 1st video thread
        -b 1200k				# bitrate in bits per second
        -async 30				# keeps audio and video well sync'd. This is quite high at 30 samples a second. (useful when you jump back/forward when viewing)
        -acodec ac3				# audio codec to use
        -ab 128k				# audio bitrate (mid quality range used)
        -ar 48000				# audio sampling rate (Hz)
        -ac 2					# number of audio channels (so 2 = stereo)
        output_filename			# output = the converted file

The 1200k video bitrate that I use may seem too low for home cinema buffs but I’m happy with it and the smaller file size is great for mobile devices and also for streaming. A normal DVD would be about 9800k and Blu-ray is 40000k.

Editing:

Probably the most common editing needed is to ‘top and tail’ a recording (i.e. removing any unwanted video at the start and end) and maybe to remove the adverts.
There have been attempts to automate such edits by using code to watch for changes in the video that may signal the start and end of each episode, but it’s sadly far more reliable to quickly watch the video and make a note of the start/end times.
This could no doubt be done with GUI video editing tools but for my old computer the command line and avconv work just fine.

avconv -ss 00:05:00 -i recording.mp4 -t 00:30:00 -y -codec copy episode1.mp4
avconv -ss 00:35:00 -i recording.mp4 -t 00:20:00 -y -codec copy episode2.mp4

Here I’m cutting two episodes from the file recording.mp4. Ignoring the first 5 minutes and then copying the next 30 minutes to episode1.mp4.
Then I’m extracting 20 minutes from the same file, after first skipping 35 minutes from the start of the file, and I’m putting that into episode2.mp4.

These separate episodes can then be viewed but really we should stitch them back together into one file. For this I use the MP4Box utility from the GPAC project

MP4Box -cat episode1.mp4 -cat episode2.mp4 full_programme.mp4

This utility scans each episode and produces a seamlessly merged version in full_programme.mp4

Any other tricks:

Corrupt VOB files (usually from ripped DVDs) can be common. Yet the VOB format can actually be roughly chopped about with common Linux file commands. i.e.
cat episode1.vob episode2.vob > full_programme.vob
would be a simple replacement for the earlier MP4Box utility if we were dealing with VOBs.

A common VOB corruption is where the video or audio settings are not being picked up correctly at the start of the video. This can result in a) a blank display yet video can be found if you jump forward, b) ‘no key frame’ errors, or c) a video that thinks it’s shorter than it really is (which doesn’t usually fool players but will cause avconv to stop prematurely).
A rough fix for this can be as simple as chopping-off a chunk from the start of the file (I said it was rough). e.g. tail -n +5k corrupt_file.vob > new_file.vob
will remove the first 5KB from the start of the corrupt file. Try varying this amount (trial and error) until you get a usable output file.

Sometimes a file converted into an mp4 can sound much quieter than others and I don’t want to have to keep adjusting the volume control when playing videos. The best solution I’ve seen for this is the below script courtesy of James Lloyd on the Ubuntu forums. avconv is used to extract the audio track, it is then normalized with gain and then merged back with the video using avconv again.

VIDEO_FILE=$1
VIDEO_FILE_FIXED=${VIDEO_FILE%.*}-fixed.${VIDEO_FILE##*.}
avconv -i $VIDEO_FILE -c:a pcm_s16le -vn audio.wav
normalize-audio --gain=16dB audio.wav
avconv -i $VIDEO_FILE -i audio.wav -map 0:0 -map 1:0 -c:v copy -c:a libvo_aacenc \
   $VIDEO_FILE_FIXED

The earlier avconv conversion examples use the default audio track (i.e. the first track which is number 0:0). So it can be frustrating to find you have produced a foreign language version of the video.
To rectify this the original VOB file can be converted to extract the video and the second audio track (numbered 0:1) with this command
avconv -i original.vob -map 0:v -map 0:a:1 -codec copy new_language.vob
Of course that could prove to be another foreign language so you could try again with the third audio track (-map 0:a:2) and so on…


Finally, always respect copyrights, patents, licenses and the law. Although the more you learn about them the more you’ll appreciate Open Source and Creative Commons.


Categorised as: Computer Stuff

Comments are disabled on this post


Comments are closed.