Tuesday, May 20, 2014

Super-simple way to tune HdHomerun tuners and record their output

Rev 7/4/22 (see Notes)


This article describes a simple, dirt-cheap system for using a basic HDHomerun tuner to record OTA broadcasts "on the fly" in their native format to a PC running Ubuntu or one of its derivatives. (I prefer Kubuntu for my PVR, partly because, as of this writing, using Kubuntu 22.04, the KDEnlive video editor can be installed on it with a download of only about 57MB, which is small for a video editor with its capabilities.) I've used this system for years without feeling that it needs to be improved. It consists of a set of shell scripts which can be launched by clicking on them, and thus act as buttons, each of which sets a particular HDHR tuner (0,1, etc.) of an HDHR unit to a particular channel and subchannel ("program"), then requests a name for the .ts file to be used for storing the program, which ends up in the directory which conains the scripts. When a script is launched, the corresponding tuner-section (0,1, etc) is set to the corresponding channel and subchannel, and a terminal window appears and requests a name for a file to contain the tuner's output. Then, when a name is entered and Enter is pressed, the tuner's output is stored in the designated file, until the terminal is closed. I've created a directory for the shell scripts for each tuner-section (0,1, etc), and the files produced by the corresponding tuner are stored in the same directory.

Another name for "shell script" is "bash script," where BASH is an interpreter which sequentially converts each command in the script to a lower-level language and sends it to the OS. The first line in a bash script is always "#!/bin/bash," which declares it to be a bash script and launches the bash interpreter. To run the script, it's necessary to give it execute-permission (right-click on the file, select Properties, then Permissions, and then the execute-permission box), which can also be done with the 'chmod 700 <path/filename>' command. It's also sometimes necessary to tell the computer how to run the script, such as by telling it to open it with bash and to run it in a terminal (this is done once, after which any bash script would be launched by simply clicking on it).

Here's an example script, named 4-1_KVOA.sh (avoid spaces in file names, in case they end up getting used in commands). It's for recording Tucson station KVOA [1] with HDHomerun s/n 1038702F Tuner 0:

#!/bin/bash
hdhomerun_config 1038702F set /tuner0/channel auto:23
hdhomerun_config 1038702F set /tuner0/program 1
echo "Enter desired name for ts file"
read filename
hdhomerun_config 1038702F save /tuner0 $filename.ts

"Echo" just displays the specified prompt on the command line, "read" assigns whatever is subsequently entered to the variable "filename," and $filename is the value of the variable 'filename.' The HDHomerun commands can be found in hdhomerun-config "manpage," or by entering "hdhomerun-config -h" after installing hdhomerun-config. You'll also need hdhomerun-config-gui to gather information for the scripts. As far as I've been able to determine, the GUI is available only for Ubuntu and its derivatives.  

I had some trouble finding a way to get the scripts to run in Kubuntu 22.04, so I'll describe my solution: Right click on the file, select "open with other application," then in the box which appears, enter "bash," and enable it to run in the terminal. If that works, you would do it again and make it the default method, although you might not want all shell scripts to run in the terminal, in which case you would create an exception for each script which you don't want to run in a terminal. Then, you can simply click on the file and select "open" in the box which appears ("execute" didn't work for me). A terminal will appear with the prompt "Enter desired name for ts file," and you would enter the name and hit Enter, and the recording process should begin.

Naturally, the HDHomerun "config" software (hdhomerun-config) would have to be installed, which in Linux is done through the package manager if you have a direct 4G or better internet connection. (If you don't have such an internet connection, see AnAptOfflineBlog.blogspot.com.) I doubt that you'll have to update the package index to install just HDHR - you could check the relevant Packages site, such as Ubuntu Packages, to see if there have been any revisions to the HDHR packages or any dependencies since you performed an update.

You might also have to set up the ethernet connection with the connection-management utility, which is typically found in the system-settings section of the main menu. If your tuner is connected directly to your computer, instead of through a network, all you have to do is to turn the tuner on and to set the connection's IPv4/Method option to "Link-local only," and the connection should then set itself up automatically. Then you could launch the hdhomerun-config-gui, scan for channels, and press View in the GUI to view them on VLC.

Notes

Rev 7/4/22 - Rewrote the entire mess, and added some information I've learned since first writing it.

[1] KVOA is virtual channel 4.1, which broadcasts on Tucson physical channel 23, Program 1. Note that the virtual subchannel's number isn't always the program-number, as you probably notice when scanning through the channels and examining the subchannels.

Monday, May 12, 2014

Easy way to grab frames from .ts files in Linux

The VLC player will play .ts files obtained from TV broadcasts (obtained as indicated below, although I now recommend formatting Personal Video Recorder/PVR hdd's with the Ext4 format). However, VLC doesn't do such a good job of stepping through .ts files frame by frame, as you will find if you try it. 

So, I tried OpenShot, but I couldn't find a way to get it to extract frames at full resolution, and it eventually played a dirty trick by replacing itself with an error message. 

OpenShot's dirty trick backfired, because as a result I went on to discover that VLC does an excellent job of stepping through .ts files which have been converted to an mp4 format by Handbrake, with essentially zero effort and little if any loss of resolution. The conversion can take a long time, but you can convert just the sections of interest, one at a time, by specifying the range for each conversion, which you can obtain by "sliding" through the .ts file with the VLC player.

VLC doesn't have a button to step backwards, but you can use the slider to "rewind" the video a bit if you overshoot, and then step forward with the single-step button. Once you find the frame you want, you can save it with the snapshot button.