Getting MPlayer to cope cleanly with redshift

Redshift is a nice tool that adjusts the color temperature of your screen according to your surroundings. As result, your eyes hurt less if you are working in front of the screen at night.

It is easy to set up, it is for instance already packaged for Debian (package redshift). Once installed, you have to determine longitude and latitude of your position – googling around should do. And you can made some test to defines which range of temperature you want redshift to work with – I like it cold, so I go from 6500 to 9300. And you add it in autostart, the way you want.

In my case, I added redshift -l 48.799:2.505 -t 6500:9300 & just before startkde in my ~/.xsession

Easy, isn’t it? Sure. But when I watch TV with MPlayer or any video with SMPlayer, especially around 01 AM, I’d like color temperature back to normal. And, no, I’m not fond of the idea of doing a killall each time I start watching a video and then a call to redshift afterwards when I’m done.

Configure SMPlayer to use this MPlayer wrapper that kills and starts redshift at the right time

So I wrote a wrapper that send SIGTERM to any redshift process when starting, call MPlayer then, when over, restart redshift. It uses basic perl functions so it has no dependencies. You may however edit it to the set latitude/longitude and temperature range to whatever you like.

It should be just as if you were using MPlayer, so you can configure SMPlayer, or any MPlayer frontend, to use this wrapper. Obviously, this wrapper could be modified to work with vlc, xine or any else video rendering engine.

Videos misc: saving space by converting .avi, .mpg or .mkv to .mp4, resetting subtitles

These days, while according to neutral sources movies industry has never been so juicy so it made obviously necessary to restrict freedom in France in the name of its survival, I thought nice to share two small scripts handy when dealing with videos on your harddrive.

Surely, you wouldn’t store videos downloaded over the internet that you haven’t paid for. I guess that’s immoral since in 2009 the US box office top $10bn for the first time in history, during worldwide economic downfall . An advertisement paid by these guys that made these $10bn, while bankrupcy was really an option for major financial institutions and eviction just the same for poor tenants, said it is piracy – while it is still hard for me to envision how it relates to these events occuring on regular basis in Malaysia, Cameroon, Red Sea, etc.

And if you are not concerned by the moral issue (a communist like Jesus Christ, aren’t you?), maybe you are afraid to get caught anyway. Well, it is unlikely that police would come to your house with a search warrant looking for piracy evidence. Mostly because there is no such thing in French law as a search warrant. Indeed, police is entitled to enter in your house in some cases: in three cases only. First, there is what is named commission rogatoire, an order given to a policemen by a judge to do something specific in his name such as search in your house. Looks obviously like an US/UK common law search warrant but it is not: sure it gives the same rights to the police, but it is not an usual procedure in France as it applies only to criminal investigations (information judiciaire), not for trivial misdemeanour/regulatory offences. Second, in the case of a enquête de crime ou délit flagrant (felony or misdemeanour punishable by jail time that just occurred), police can enter your house without your consent. Three, in what is actually the only case that would allow police to enter your house for a regulatory offence (which what this piracy is actually more or less about) is the enquête préliminaire – funny, in this case, police requires your (written) consent to enter. If guess that if I had this kind of piracy evidence at home and the police coming to my doorstep asking to enter in an enquête préliminaire, I would probably not consent.

And I would not even dare to bring the issue of the fine endorsed by this HADOPI law. It is said that, as a friendly reminder of your place you should not have forgotten so easily (customer, yes, that’s you – nothing else – even if you do no harm, it is not up to you to proceed otherwise as you’d wish), by Law, your Internet access will be discarded while you’ll still pay for it. Well, French penal Law states also that “Nul n’est responsable que de son propre fait”, meaning that you can only be punished for your own doings. Sure, there are exceptions (boss that somehow forced employee to misbehave, etc). But none that I think of such as the case of two persons living in the same house and being punished together for the actions of only one of them without the knowledge and consent of the other. By principle, this idea is outlaw, a regression of two thousands years of penal law, dropping us back in the days when you were entitled to take possessions (by force if necessary) of the belongings of a spartian you’ve just met because, as athenian (or whatever), you were recently spoiled by another spartian, no matter that they had no ties aside their citizenship.

1. So there is a script called dir2x264.sh that I wrote for the purpose of saving harddisk space by cleanly converting .avi and .mpg files to an x264-encoded .mp4 file.

It could surely be tuned – I noticed issues when dealing with .mkv files. So far it uses mencoder (mplayer’s encoder) with lavc as audio codec and x264 as video codec. So obviously it requires mencoder, with support of these two (usual) codecs. It implies ffmpeg as well.

To use it, go in the directory where you have avi or mpg files, put the script in there and call it (it will always process all the files in the current `pwd`).

$ cd ~/myvideos
$ chmod +x dir2x264.sh # (if not made executable already)
$ ./dir2x264.sh

It will log work being done in dir2x264log, to easily evaluate the harddisk space saved.

2. In case you cannot find (Have you tried SMPlayer?) the exact correct subtitle file for your video but found one that is just fine except there is a delay between the sound/image and the subtitle, the subtitle_reset.pl could do the trick for you. It depends on Subtitles.pm (libsubtitles-perl in debian).

This script takes two command line arguments: –file and –time (in seconds, positive value or negative), so the usage is quite obvious.
It will make a backup of the original file. If you run it several times to finely adjust your file, it will always restart from the backup file, unless removed obviously.

Note that however this script will not help if the matter is that the video and subtitles file frame-rate differs. You may want to give a try to subs, a script that is now shipped with Subtitles.pm (that was not there or that I missed when I first wrote this one).

July 31, 2010 Update: I thought I found a workaround the Matroska sound issue (Too many audio packets in the buffer). As mencoder is unable to properly convert them, after googling around, I gave a try to the idea of extracting apart the audio and video stream and to put them back together afterwards with mencoder. It actually now is able to produce a working mp4 file with the two streams but it seems that the audio is out of synch with the video. SMPlayer can correct the delay while running, though. So it is better than before but it is still buggy.

August 11, 2010 Update: I noticed several issues with mencoder-produced mp4. I modified the script to use only ffmpeg (except the mkv-specific part that I have not modified).

Watching Freebox’s TV stream on a Workstation in a software-independant way

There are plenty of ways to get TV broadcast on a workstation. You can browse TV show website that provides it. You can use software like VLC that will connect to whatever server (that may be an ISP router) that will serve such broadcast.

Freebox is somekind of ADSL modem/router modern enough even to carry a harddisk, provided by the French ISP Free.fr. Freebox provides several output plugs for video stream: HDMI, S-Video, numeric audio and one called “TV”.

Freebox plugs

What is of interest to me is to make use of my Freebox stream with my workstation just as if it was a mere TV set. I do not want to use a specific software that will connect to whatever address, I do not want to use a specific video player. I just want to mimic the TV-set behavior. And so forth, it should run on whatever Operating System. Debian GNU/Linux, obviously, since that’s what I’m using, but any other else if wanted.

If I were using a real TV set, I would have two options: using the HDMI plug, if my TV-set supports it, to gain high-quality image ; using otherwise the SCART (called Péritel in France) cable with an adaptator in the “TV” plug.

The hardware is on one end the Freebox HD, as shipped by Free.fr.
On the other end, the workstation is recent enough, with modern video controller (ATI Radeon HD 4870) and “hybrid video recorder” (WinTV-HVR-1300, sold in Europe) which means in its producer’s marketting slang that it supports both analog and numeric streams. But actually, it does not, since all it’s video input plug are analogic (Composite video and S-Video).

Happauge WinTV-HVR-1300 plugs

$ lspci | grep VGA
02:00.0 VGA compatible controller: ATI Technologies Inc RV770 [Radeon HD 4870]
$ lspci | grep video
01:08.0 Multimedia video controller: Conexant Systems, Inc. CX23880/1/2/3 PCI Video and Audio Decoder (rev 05)

Will also be necessary a S-Video cable, adaptator “TV” to SCART and a RCA stereo cable.

So, let get down to business:

1) Plug the S-Video from the Freebox to the WinTV-HVR-1300 (or any similar card that have a S-Video plug). Then you should be able to get video.

S-Video plugged in video recorder PCI card

2) Plug the adaptator “TV” to SCART in the “TV” Freebox plug, connect it with the RCA stereo to your tuner or workstation soundcard. This looks clumsy, as the SCART part (that handle video) is dismissed, and surely could be improved. Nonetheless, then you should get audio.

RCA stereo plugged in Freebox TV plug via a SCART adaptator

3) Start a video player. For instance, you can use mplayer with the following script:

#!/bin/sh
# -ao not set as sound is going directly through the tuner
# -vo gl2 : slurp too much CPU, use xv instead, it is lighter
# we assume video recorder device is /dev/video0
# we use PAL-BG, assuming that's how the Freebox is configured
# (this is all one big line)
/usr/bin/mplayer -stop-xscreensaver -aspect 16:10 -ontop \
-dr -vo xv -ao null -framedrop \
-vf pp=fd \
-contrast 10 -brightness 5 -hue 5 -saturation 5 \
-tv mjpeg:norm=PAL-BG:noaudio:driver=v4l2:device=/dev/video0:input=2:width=768:height=576 \
-nocache -quiet 'tv://' -identify

Freebox stream through MPlayer, in KDE

Regarding sound output, it depends whether you plugged the output in a classic tuner or in your workstation soundcard (or even in the video recorder card, that is also an audio recorder card, at your convenience). But that should be easy-going.

Here it works with MPlayer. But it could work with plenty other video player, whether they are dedicated to TV (like KDEtv) or not making no difference.
It is also easy to envision how to really record video with this setup (if you have the audio going through your workstation soundcard).

I’m sure there is room for improvement here. I’m not very fond of the idea of using through this SCART / RCA stereo cable. But it works.
I guess next step would be to use HDMI directly.

Videos with subtitles: SMPlayer can help!

It is always a mess to get the correct subtitle for a given TV-show or movie you obtained.

First, you have to browse allsubtitles, opensubtitles and whatever -some of these sites are full of ads and are even ugly-coded enough to kill your fragile webbrowser. And afterwards, you may find that, in fact, the subtitle you have relates to another version of the video you are attempting to watch, and this results in delay issues.

Not to mention that you may even use a video player dumb enough to force you to always select the subtitle (.srt, .sub, etc) each time by hand while its name is consistent with the video file name.

There is, finally, a work around. It is called SMPlayer. It is a frontend to MPlayer that have a menu that allows to automatically download subtitles or even set, on the fly, delay for the current subtitle file used.

On a decent GNU/Linux system, you could probably do apt-get install smplayer. For Microsoft Windows, go to MPlayer HQ, select download MPlayer with SMPlayer GUI. Mac OS X users… well… should take spend some time to port it – it shouldn’t be so hard.