Sunday, November 24, 2013

Linux Video Capture using an HD-PVR (model# 1212)

Hello there fellow tech nerds. How has everyone been doing lately? I have been thrilled with life lately and I hope you as well.

Todays post is going to cover how to capture hd video (720p) from a component video source onto your Linux computer so that you can edit it with video editing software. I do this to capture my Xbox 360 gameplay. I use a microphone and audacity to record commentary separately. I then edit the video files captured from the HD-PVR and the audio commentary together using Kdenlive, then render it and upload the videos to YouTube. I do this to help others with certain parts of a game or just provide general tips about a particular game. So let's get right into it.

First you'll obviously need an Hauppauge HD-PVR but it has to be model number 1212 (the wiki states that model# 1445 is also supported but I don't see that as a model# on the Hauppauge Support page), the newer ones (HD-PVR2) don't have linux drivers so there is currently no way to capture from them. Luckily if you're using a recent Linux distribution the driver that allows this to work is built right into the Linux kernel. Any kernel 2.6.30 or above and the driver is included within the kernel. I am running Xubuntu 12.04.3 with kernel 3.7.0-030700-generic. You may possibly need a Windows computer to upgrade the firmware on the HD-PVR which is done by running a Windows executable file from Hauppauge's Support Page which installs the driver but it also updates the firmware within the HD-PVR. The latest firmware is preferred, it's all explained HERE.

Step 1 (determine current firmware on your HD-PVR)
Open a terminal window and plug in your HD-PVR preferably into a USB2.0 port (I don't think USB1.1 is fast enough. I had issues using a USB3.0 port in which it would stop recording by itself so try to avoid if you can or try it out and see for yourself), type in
Mine returns the following information
[6307.550581] usb 1-3: new high-speed USB device number 13 using ehci_hcd
[ 6307.727776] usb 1-3: New USB device found, idVendor=2040, idProduct=4903
[ 6307.727779] usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 6307.727781] usb 1-3: Product: Hauppauge HD PVR
[ 6307.727783] usb 1-3: Manufacturer: AMBA
[ 6307.727784] usb 1-3: SerialNumber: 00A49D0D
[ 6307.755504] hdpvr 1-3:1.0: firmware version 0x1e dated Mar  7 2012 08:25:15
[ 6307.755507] hdpvr 1-3:1.0: untested firmware, the driver might not work.
[ 6307.869496] hdpvr 1-3:1.0: magic request returned 8
[ 6307.878237] hdpvr 1-3:1.0: config call request for value 0x1700 returned 1
[ 6307.887215] hdpvr 1-3:1.0: config call request for value 0x1500 returned 1
[ 6307.922052] hdpvr 1-3:1.0: config call request for value 0x1200 returned 1
[ 6307.931548] hdpvr 1-3:1.0: config call request for value 0x1300 returned 1
[ 6307.940780] hdpvr 1-3:1.0: config call request for value 0x2900 returned 1
[ 6307.950518] hdpvr 1-3:1.0: config call request for value 0x2a00 returned 1
[ 6307.958510] hdpvr 1-3:1.0: config call request for value 0x2b00 returned 1
[ 6307.967748] hdpvr 1-3:1.0: config call request for value 0x2c00 returned 1
[ 6307.977615] hdpvr 1-3:1.0: config call request for value 0x2d00 returned 1
[ 6307.996731] hdpvr 1-3:1.0: control request returned 4
[ 6307.997589] hdpvr 1-3:1.0: no valid video signal or device init failed
[ 6308.007835] hdpvr 1-3:1.0: control request returned 1
[ 6308.025559] hdpvr 1-3:1.0: control request returned 1
[ 6308.025561] hdpvr 1-3:1.0: allocating 64 buffers
[ 6308.052476] hdpvr 1-3:1.0: device now attached to video1
[ 6308.052495] usbcore: registered new interface driver hdpvr
The hdpvr module should have autoloaded once you plugged in your HD-PVR, if it didn't we can load it manually by issuing
sudo modprobe hdpvr
Nothing will appear to happen if all went well but to verify it's loaded you can issue
lsmod | grep hdpvr
That should return the following info
hdpvr                  32700  0
v4l2_common            21458  2 s2253,hdpvr
videodev              130085  5 s2253,hdpvr,uvcvideo,videobuf2_core,v4l2_common
If it didn't you'll have to figure out what's wrong with the hdpvr module within the kernel you're using and this tutorial won't cover that sorry.
So according to my dmesg output and the HD-PVR MythTV wiki I am running the latest firmware already. This is a good thing. If you're not running a firmware dated Mar 7 2012 then I suggest updating the HD-PVR firmware because it can fix various issues as well as fix color and saturation issues with previous firmwares. I am not sure this would work within a Virtual Machine so if you don't have a Windows computer see if you have any friends or family that would allow you to use it. You would just need to bring the HD-PVR with you, along with it's USB cable and if they had internet you could download the latest driver that linked to earlier, hook up the HD-PVR and run the .exe driver installer and it will update the HD-PVR with the latest firmware. If you can't for whatever reason update the firmware it's ok, you can still move forward with the tutorial just note that if issues arise it may be related to the old firmware on the HD-PVR. NOTE: Certain kernels (kernel lower than 3.3) and firmware combinations may also result in color and saturation issues as well, those issues can be read about HERE.

Step 2 (setting some sane defaults for the HD-PVR)
The way we set the module (a module is equivalent to a driver in Windows) defaults at least within Ubuntu is by creating a *.conf file and storing it within /etc/modprobe.d/. The *.conf file will be read by the kernel when it loads the hdpvr module. The settings that can be set are all listed HERE. Create the *.conf file
gksudo leafpad /etc/modprobe.d/hdpvr.conf
You can name it whatever you want but I named it hdpvr.conf so that I know what the conf file is for. I used leafpad but you can use whatever GUI text editor that's installed on your system. I run Xubuntu so the default editor is leafpad, yours may be gedit if you run straight Ubuntu.
within the file paste the following
options hdpvr hdpvr_debug=1 video_nr=1 default_video_input=0 default_audio_input=0
I enabled the least log info possible while still showing some debug info (useful for troubleshooting). Since I have a webcam always connected I made the HD-PVR video device node 1 (will show up as /dev/video1). I want to capture the component input (versus composite-yellow, white, red) and finally I want to capture the audio from the rear RCA ports NOT the front ones. You can set those settings to match your system and situation. In order for the settings to take effect without having to reboot your machine you can issues the following to reload the hdpvr module which will then use your settings.
sudo rmmod hdpvr
That will unload the module
sudo modprobe hdpvr
That will load it again using the settings from the *.conf file you created.

Step 3 (capture some video)
Unfortunately for now there is no nice little GUI (Graphical User Interface) application that captures the HD-PVR video stream although I am in talks with the developer of SimpleScreenRecorder to see if he can incorporate capturing the HD-PVR with his application. So for now it's a pretty basic command in which we have to use the terminal. For this to work your user that you log in as needs to be part of the video group. I'm sure you can google how to achieve this as it's not hard but this tutorial won't cover that. So open a terminal window, then we want to change directories to where ever you want the recording to be saved. Ensure there is an ample space free within this folder since capturing HD video takes up a lot of space. It's around 500 megabytes (that's .5 gigabyte) of space for around a 10 minute video. That's done with
cd /home/username/Videos/
You obviously need to change the username to be YOUR username and ensure the full path is writable by you. So I have a Videos directory located in my home directory which I have write permissions to. Normally any folder within your entire home directory is writable by you. The command to capture the video is
cat /dev/video1 > video.ts
The Linux cat command concatenates and lists files. Basically we're using cat to spit out whatever is at /dev/video1 which in this case will be the video stream from the HD-PVR. The little forward pointing arrow ">" is the linux command called redirect. It redirects input somewhere else. So in this case it's taking the video stream from the cat command and redirecting it into a file called video.ts. That's pretty much it, whenever you want to stop recording you simply hit the control key plus the letter c (ctrl+c) which tells it to cancel the command.

Congrats, you now have a video file called video.ts which contains whatever was being displayed from your video device. In regards to the video and audio specifications the HD-PVR can record up to 1080i. Personally I prefer 720p over 1080i since the p means it's progressive which can lead to less flickering. It boils down to what resolution you're inputting into the HD-PVR, it has a passthrough, meaning whatever you're inputting in the HD-PVR that same signal will get passed out to a TV to view. At that same time the video is encoded within the device using h264 codec for the video and aac codec for the audio. The encoding information for the HD-PVR can be read about HERE, it's all in the 2nd and 3rd paragraph if you're interested in learning about the bitrate, resolutions, codec etc etc. So in my example I play my Xbox 360 as 720p so the video.ts file contains an hd video which is HD quality (720p). Now go and create some masterpiece videos and showcase them on YouTube for the world to view.

Here's a sample of my work all done using Xubuntu Linux

-Ubu out

Post a Comment