Saturday, March 16, 2013

Converting Sony's PlayTV M2TS recordings to Xvid

Recently, I had to replace my PS3 and one of my PS3's main roles for a long time has been as a DVR. The PS3 together with the PlayTV peripheral and the official BD remote does an excellent job of this providing the ability to watch, pause, rewind & record over-the-air TV with a nice HD interface, it even improves image quality due to the PS3's built-in upscaling.

The slow death of my old PS3 together with the timely introduction of Live TV support on XBMC 12 prompted me to take the PlayTV peripheral and stick it in my Linux-based media centre PC running XMBC and setup TVheadend to pull the signal from the PlayTV's tuners. The result of this is the XBMC box has now become our DVR and TV viewing platform as well as all the other media content it serves up, however there was still a lot of decent recordings left on the PS3 that I wanted to keep and having exported them from PlayTV and transferred them from the PS3, I felt that the M2TS files are a little too large to warrant leaving them in that format using all that disk space.

So for any out there who are left with a bunch of these huge M2TS files and want to make them smaller or simply transcode them to a format that is supported by more devices then this post is for you.
Firstly you will need to have ffmpeg installed on your PC together with appropriate Xvid & LAME MP3 libraries.

ffmpeg

As always ffmpeg is my goto toolkit for video convertion and a single MT2S file can be converted to a smaller file without much loss of quality with a one-liner.

Example

 ffmpeg -y -i "awesomeshow.m2ts" \  
 -vcodec libxvid \  
 -b 1200k \  
 -acodec libmp3lame \  
 -ac 2 -ar 44100 -ab 128k \  
 -s 576x460 -threads 2 -deinterlace "awesomeshow.avi"  

In this example the filename of the M2TS file that is being converted (the input file) is 'awesomeshow.m2ts' and I've given the output file the name 'awesomeshow.avi'.

Option tips

-vcodec = video codec

Here I've used the Xvid MPEG4 library to achieve a higher compression ratio than the source file's MPEG2, reducing the size of the output file, also many devices (including the PS3) support Xvid playback.

-b = video bitrate

I've specified a video stream bitrate of 1200 Kbits per second, after lots of tries this seems to be a nice compromise between quality & output file size.

-s = frame size

My PAL TV signal is processed at 576i, the resulting resolution of the original recording is 720x576. In the above example I've used the -s option to reduce the output file resolution to 576x460. This helps to further reduce the output file size.

-acodec = audio codec

MP3 is the common counterpart to the Xvid video codec, it compresses audio well and playback is supported everywhere.
I specified the output audio stream to be a 2 channel stream with a sample rate of 44KHz and a  bitrate of 128Kbps with the -ac, -ar & -ab options.

-deinterlace

As the video source is an interlaced TV signal it will look terrible when played back on a progressive display unless deinterlacing is applied on the transcode or on the player playing the convertion. Therefore I've specified to deinterlace during the conversion with the delinterlace option, this also introduces additional processing on the transcode so ffmpeg will take noticeably longer to complete.

-threads = number of conversion processes

Set this figure to match the number of processor cores on your computer, the more processors you can get to work on the conversion job the quicker it will finish.

Finally, a script...

I had a lot of recordings to convert so naturally, to convert all of them all in one go on my media centre PC which runs a popular Linux distro I wrote a little shell script.
If you want to use this script just save it to your local filesystem (your Downloads directory presumably), pop it in your /usr/bin directory & make it executable;
 cd ~/Downloads  
 chmod +x playtv2xvid  
 sudo mv playtv2xvid /usr/bin  

Then in a terminal 'cd' to the location of your *.m2ts files and run...
 playtv2xvid  

It will show you what M2TS files have been found in the current location, press enter to begin the conversions and all will be logged in a log file for you to review later.

Download the playtv2xvid script here.


Tuesday, November 03, 2009

Auto starting and shutdown of Virtualbox VMs

Just a quickie to document a nice simple way of automatically starting and stopping Virtualbox virtual machines when you login and shutdown your Linux box.

Firstly, what is the name of virtual machine that you want to do this with?
You can list the names of your VMs in a terminal as below
$ VBoxManage list -l vms|grep "Guest OS" -B1
...and choose the name of your virtual machine.

For this example my VM is called 'Windows XP' and my host is running Ubuntu 9.10, other distros running Gnome shouldn't be much different.

To autostart your VM upon login to Gnome...

Launch System > Preference > Startup Applications, select the Add button.
Then enter a name for the program in the Name: field and in the Command field enter;
VBoxManage startvm "Your VM Name"
Replacing the 'Your VM Name' with the name of your VM, if there are spaces in the name your will need the quotes.


Lastly to have your system cleanly stop your running VMs for you when you shut down your Linux box you can edit the vboxdrv init script. To do this you will need root privileges.

You have the choice of 3 methods of stopping the VM, a poweroff, an acpi button emulation or my preference, a savestate shutdown which will stop the VM and preserve it's runtime state, it will then resume the next time it is started.

Edit the init script with the nano text editor, if you have sudo setup or if you're using an Ubuntu/Debian style Linux distribution...
$ sudo nano /etc/init.d/vboxdrv
... and enter your password.

If you don't use sudo, you can su to root before running nano or you can use su as below..
$ su root -c nano /etc/init.d/vboxdrv
... and enter the root account's password.

In nano hit [Ctrl]+[W] to search and enter SHUTDOWN in caps. This will take you line 235.

A few lines down uncomment the appropriate "SHUTDOWN=" line of choice, for example to choose a savestate shutdown edit the line;
# SHUTDOWN=savestate
to read,
SHUTDOWN=savestate
Then hit [Ctrl]+[O] to save the change, then [Ctrl]+[X] to exit nano.


This will ensure that your system when shut down will cleanly stop your running VMs without you having to do so manually.