banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Watching Live OTA TV With MPlayer


by Paul Arnote (parnote)

Last month, I took you through how to set up VLC to watch live, OTA (over the air) broadcasts via a USB TV tuner. This month, let's take a look at how to do the same with MPlayer.

MPlayer is one POWERFUL multimedia powerhouse program. Did I mention that it was powerful? As with anything that wields that much power, it can sometimes present a level of complexity that is daunting, as it strives to cover as many multimedia bases as it can. In fact, MPlayer will often play files that other multimedia programs simply shrug at.

MPlayer is a multiplatform program, with versions for Linux, MacOS, and Windows. MPlayer is also "desktop agnostic" That means that it doesn't use either the Qt or GTK+ programming toolkits, opting instead for good, sound code that runs equally well regardless of which programming toolkit is employed.

MPlayer also serves as the "backbone" for other multimedia programs. SMplayer is one such program on Linux that wraps MPlayer in a "simpler" wrapper to playback multimedia files, insulating end users from the complex command line options for MPlayer.

So, how difficult can the command line options get for MPlayer? Take a look at MPlayer's man page. This is NOT an exercise in "light reading" It will take you a while to go through all of the options available to MPlayer. Also, the sheer number of available options makes it easy to miss what you are looking for.

In short, MPlayer is a workhorse for playing back most, if not all, multimedia sources.




Watching OTA TV With MPlayer

Of the programs in the PCLinuxOS repository that I've tried for watching OTA broadcasts, MPlayer is, by far, the most challenging one to set up and use. Most users are familiar with MPlayer as a GUI program, launched from the "Video" section of their desktop's applications menu. But MPlayer is actually a command line program. What users "see" when it's launched from their applications menu is a GUI wrapper. At its core, though, MPlayer is a command line program. It's only through the command line that the real power of MPlayer is unleashed.

The ability to watch OTA broadcasts hinges on, first of all, knowing how to pass the command line parameters to MPlayer. The second requirement is the presence of a list of channels from w_scan saved in the format recognized by MPlayer, with the name of channels.conf, stored in the hidden .mplayer directory in your home directory. Phew! Yep, that's a lot to take in.

The file listing the channels found by w_scan MUST be named channels.conf, or MPlayer cannot find the list when it's needed. While the ability might exist (the MPlayer command line options are more than plentiful), I've not found any command line parameters that allow you to specify a different filename.

This is vastly different from VLC, which allows you to load up a file containing the listing of all the channels, and which can be named whatever you want to name it, just so long as it has the proper file extension and is formatted in VLC's special XML formatting. The latter is taken care of by w_scan when you select the -L command line option when scanning for OTA broadcasts, or when using the vlc-channel-scan.sh bash file I shared last month.



So, thanks to the complexity of MPlayer, I ended up writing two scripts to make it easier to watch OTA broadcasts with the program. The first script is to scan for channels with w_scan, and to place the output into the hidden .mplayer directory, in the file named channels.conf. The second script makes it easier to start up MPlayer with the OTA broadcast of your choice. Without it, it's a matter of not only remembering a complex MPlayer command line, but also switching back and forth between a terminal session and reading the channels.conf file in a text editor to discover the proper name of the OTA broadcast you want to watch. Instead, my script lists the channels in a zenity dialog box. Then, you just click on the channel you want to watch, and then the OK button to launch MPlayer playing that chosen channel.

For those of you who like to avoid the command line at all costs, never fear. These scripts include zenity dialog boxes, which insulate you from the mundane/complicated command line execution. It would be a trivial task to create a custom .desktop file, and place that in your usr/bin directory. Then, the scripts could easily be launched from your desktop's applications menu. Without the .desktop file, you can simply launch a terminal window and type in the name of the script. That is how I run them. Or, you could simply create a launcher on either your desktop or on one of your panels that execute the scripts. Isn't it nice to have so many choices under Linux?!


Script #1: Scanning For OTA Broadcasts

I have to praise the developers of w_scan. They seem to have made it adaptable to several different formats. In our case here, we need to output the channel listing of OTA broadcasts in a format that MPlayer can read and understand.

But, because MPlayer only accepts the channel listing as a specially formatted file named channels.conf, we can't give the file different names to indicate the locale and date for the channel scan. This part is important if you travel to different areas. So, I had to come up with a different way to indicate the locale that the current channels.conf file was configured for.

While my "solution" isn't as good as being able to look at the filename and determine the locale and date of the last scan, it's probably the second best solution. Any line in the channels.conf file (or any other MPlayer configuration file, or even bash files for that matter) that starts with # is interpreted as a comment. So, when you open up the channels.conf file and look at it in a text editor, there'll be no mistaking the locale and the date of the last channel scan, as it's right at the top of the file. Running the bash script to rescan for channels will completely overwrite the existing channels.conf file, if one already exists. That's handy for a couple of reasons. First of all, if the channels.conf file doesn't already exist, then it is created. Secondly, it helps make sure your list is up-to-date and current for the current locale.

So, here's the first script, which I've named mplayer-channel-scan.sh. As usual, you can download the script from the magazine website, or you can type it in yourself (it's quite short), or you can cut and paste it into a text editor. If you download it, strip the .txt file extension from the download file. And, in any of the cases mentioned, don't forget to mark the file as executable. Also, as I usually do, I've numbered the lines of the script to make it easier to tell what exactly comprises an entire line (before you have to press the "Enter" key to start a new line and execute a new command).

download script

The MPlayer channel scan script is almost exactly like the VLC script from last month, with some notable exceptions to accommodate MPlayer's needs. So, let's instead only cover those items that are different.



Line 10 starts off by writing the locale of the channel scan, as it was specified by the user in the dialog box as a comment in the channels.conf file. Line 11 appends (hence, the >> redirection) the date as a comment in the channels.conf that the scan was done. Line 12 appends a line of 40 dashes to provide a visual separator from the rest of the REAL data that MPlayer needs, while line 13 appends a blank comment line to channels.conf, merely for more visual separation.




Lines 15 through 19 setup the same while … do loop as the VLC script. The only differences are that the w_scan command uses a -M command line switch to output the MPlayer formatted channel list, and the scan data is appended to the channels.conf file.

Below is a screenshot snippet from my channels.conf file.



As you can see, w_scan picked up all 53 of the OTA broadcasts in my area (Kansas City area). Wait a minute, you say? I see 57 lines there! Remember that the first four lines written to the channels.conf file were comments that we added. (Sneak peek: we'll use those comment lines in the second script!)

Take care to note how many OTA broadcasts are in your area. I've run w_scan enough times just in writing these articles to know that there are 53 OTA broadcasts in my area. So, one time when I ran the MPlayer channel scan script, it only picked up 49 of those. For whatever reason (there are several possibilities, ranging from low signal strength to fringe stations to atmospheric conditions to just ghosts and goblins), w_scan missed four of the channels. So, I ran the script again, and that time, all 53 OTA broadcasts were picked up and listed.


Script #2: MPlayer TV Player

Now that we have the channels listed in channels.conf, we'll need an easier way to start up MPlayer with the OTA broadcast we want to watch already loaded. Like I mentioned before, if you use the command line to playback the OTA broadcasts from your tuner card, you'll have to switch back and forth between the channels.conf file opened up in a text editor and a terminal session to be able to issue the command properly. In my humble opinion, that's just too much to ask of anyone, even a command line commando. Plus, I'm all about working smarter, not harder.

So, below is the script, which I've called mplayer-tv-player.sh. As with the other script, it will be available for download from the magazine website, or you can type it in (it's really short), or copy and paste from the PDF into your text editor. Be sure to mark the file as executable, and if downloading from the magazine website, remove the .txt file extension. I've numbered the lines of the script to make it easier to know when to press the "Enter" key. All total, this script is 13 lines, subtracting out the blank lines that were added to aid readability.

download script


Now we will walk through this script line by line. Of course, line 1 is the typical shebang that designates this as a bash script.

Line 3 uses grep to read the first three lines (-m3) that start with # from the channels.conf file that we created with the mplayer-channel-scan.sh script, and places the string into the variable $DATA.



There's a lot going on in line 5. First, we read in the channel data from channels.conf, using grep and telling grep to forget about all lines that start with the # symbol. Then, that is piped to the cut command, because we ONLY want the first field. Each field of the channel data uses a colon as a field separator. So we "cut" the results at the first colon. Then that is all piped to a Zenity list dialog box to populate the list. In that dialog box, we display the comments we put into the channels.conf file as part of the static text in the dialog box, along with some light instructions. The user's selection is then stored in the $CHANNEL variable.



Lines 6 through 8 exit the script if the user presses the "Cancel" button. Line 10 uses the Zenity question dialog to ask the user if they want to display the selected OTA broadcast as full screen or not. The user's response is then filtered out by the subsequent if-then-else conditional loop in lines 11 through 15. If the user answers "Yes," then the selected programming is displayed full screen on the user's computer. If the user answers "No," then the selected programming is displayed in a window on their computer screen.

In lines 12 and 14, we start up MPlayer with the selected OTA broadcast. The channel selection previously made in line 5 (and stored in the $CHANNEL variable) is inserted into the MPlayer command. The only difference between the lines is that line 12 has the setting for full screen playback. Line 17 allows the script to exit cleanly.

Keep in mind that MPlayer is a command line program. There are some choices you can make when starting MPlayer, and these commands are where you can make them. First of all, I prefer a non-interlaced image, and the vf pp=fd command line switch does an admirable job of deinterlacing the video image, without taxing system resources too heavily. Secondly, if you prefer a GUI, then place the -gui command line switch immediately after the mplayer command, as the first command line parameter. To display the GUI, you MUST issue the -gui parameter as the first command line switch. The first image in this article uses the GUI, and that's how it was displayed.




Running MPlayer TV Player & MPlayer

When you use MPlayer TV Player to launch MPlayer, the script's window will immediately disappear. If you want to use it again, you'll have to re-launch it. To close out the MPlayer window (whether it's windowed or full screen), just press the "Escape" key.

There are some other keyboard controls for MPlayer you should know about. To raise the volume, press either the "0" (zero) or "*" key. To lower the volume, press either the "/" or "9" key. To mute the volume, press the "M" key. To go up one channel, press the "K" key. To go down one channel, press the "H" key. The "Space bar" pauses and restarts the currently playing video. The "F" key switches between full screen and windowed display modes. Press the "S" key to take a screenshot. The "O" key toggles the on-screen display.

To see a full listing of all of the other keys, check out this section of the MPlayer man pages. The keyboard controls work whether or not you're using the GUI.


Summary

Have I mentioned that MPlayer is very powerful? And, true to form, something this powerful is also very complicated. It's only easy if you're a frequent user or a student of its ways. I hope these scripts go a long way to making it easier for you to get much more use out of MPlayer. It truly is a worthy program. But it can also make a worthy opponent "if you don't know its ways with the force"

Next month, I plan to take a look at using Kaffeine to view OTA broadcasts.



Previous Page              Top              Next Page