Wednesday, January 28, 2015

Video capture with the Hauppauge USB-Live2

I'm a bit behind the times and have only just started converting old analogue video tapes to digital format.

I chose the Hauppauge USB-Live2 to perform the analogue to digital conversion, and have been very happy with the results.

The command I used for the capture was:

mythffmpeg -y -loglevel debug -f alsa -i plughw:1,0 -f v4l2 -s pal -pix_fmt yuyv422 -i /dev/video0 -b:v 2048k myfile.nut

on a Mythbuntu 14.04 system, although it should be easily adaptable to other installations, e.g. using ffmpeg instead of mythffmpeg.

The components of the command that may need to be changed:

The Audio input device (plughw:1,0): On my system, executing:

> arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: Intel [HDA Intel], device 0: AD1981 Analog [AD1981 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Cx231xxAudio [Cx231xx Audio], device 0: Cx231xx Audio [Conexant cx231xx Capture]
  Subdevices: 1/1

  Subdevice #0: subdevice #0

Shows that the Live2 (which uses the Cx231xx drivers) is card 1, subdevice 0.  And:

> arecord -L
[... removed irrelevant entries ...]
default:CARD=Cx231xxAudio
    Cx231xx Audio, Conexant cx231xx Capture
    Default Audio Device
sysdefault:CARD=Cx231xxAudio
    Cx231xx Audio, Conexant cx231xx Capture
    Default Audio Device
dmix:CARD=Cx231xxAudio,DEV=0
    Cx231xx Audio, Conexant cx231xx Capture
    Direct sample mixing device
dsnoop:CARD=Cx231xxAudio,DEV=0
    Cx231xx Audio, Conexant cx231xx Capture
    Direct sample snooping device
hw:CARD=Cx231xxAudio,DEV=0
    Cx231xx Audio, Conexant cx231xx Capture
    Direct hardware device without any conversions
plughw:CARD=Cx231xxAudio,DEV=0
    Cx231xx Audio, Conexant cx231xx Capture
    Hardware device with all software conversions

shows that plughw has all the format conversions.  Putting the two together gives plughw:1,0.  I think that plughw:CARD=Cx231xxAudio,DEV=0 will also work, but haven't tested it.

The video device (/dev/video0) can usually be guessed by looking at the output of ls /dev/video*.  If there's only one video device, that's it, if there are two, the first is probably the built in webcam.  If you really want to be sure, look in /var/log/syslog at the time you plugged the Live2 in:

Jan 28 14:30:49 myth kernel: [ 5319.874560] cx231xx #0: cx231xx #0/0: registered device video0 [v4l2]
Jan 28 14:30:49 myth kernel: [ 5319.874652] cx231xx #0: cx231xx #0/0: registered device vbi0
Jan 28 14:30:49 myth kernel: [ 5319.874656] cx231xx #0: V4L2 device registered as video0 and vbi0

The PC I used for transfer doesn't have a webcam, so it is video0, as confirmed in the syslog extract above.

-b:v 2048k specifies the bitrate to record at.  2048k is probably overkill, but I figured that I won't have another chance to capture the tapes, so a little extra disk space isn't worth worrying about.

After capturing the video I trimmed the blank video at the start and end, and fixed the timestamps using:

mythffmpeg -ss -t -fflags +genpts -i myfile.nut -vcodec copy -acodec copy myfile.mkv

Where x is the start offset in seconds, and y is the length of the video in seconds.

Some articles I've read have discussed capturing video simply using:

cat /dev/video0 > file.mpg

this won't work for the Live2 as it is a simple frame-grabber, and doesn't have onboard compression.

The only hiccup in the whole process is that the Live2 more reliable if it is plugged in to a USB port that is using the ehci stack.  Newer computers that support USB3 and use the xhci stack will have problems.  If you want to check, once again looking at /var/log/syslog:

Jan 28 14:30:45 myth kernel: [ 5316.076069] usb 1-6: new high-speed USB device number 3 using ehci-pci
Jan 28 14:30:45 myth kernel: [ 5316.210847] usb 1-6: New USB device found, idVendor=2040, idProduct=c200
Jan 28 14:30:45 myth kernel: [ 5316.210856] usb 1-6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Jan 28 14:30:45 myth kernel: [ 5316.210860] usb 1-6: Product: Hauppauge Device
Jan 28 14:30:45 myth kernel: [ 5316.210863] usb 1-6: Manufacturer: Hauppauge

From the first line above we can see that in this case the Live2 is using a ehci port.

[EDIT 11 APRIL 2015] Kernel 4.0-rc7 includes patches which largely resolve the issues with USB3 / xHCI ports and Intel xHCI controllers.  More specifically, the problem was with Intel xHCI controllers and isochronous USB devices (i.e. video and audio capture devices).  It looks like there are still some issues, however the current hypothesis is that they are issues to do with the cx231xx driver that are triggered by the differences between the eHCI and xHCI drivers.  Depite these issues, if you have an Intel xHCI controller, cx231xx devices are now usable on USB3 ports, and other video / audio capture devices may well work without any problems.  If you're interested in the details, please see: http://www.gossamer-threads.com/lists/linux/kernel/2126922

Many thanks to Devin Heitmueller, Mathias Nyman and Lu Baolu for their assistance it getting the Live2 working for me.

[EDIT 25 Apr 2015] More good news: The patch has been back-ported to 3.19.5, 3.14.39 and 3.10.75.

[EDIT 20 Jun 2015] More good news: Ubuntu kernel 3.19.0-21 (and a couple of earlier version, I think) are based on mainline 3.19.8, which include a few more fixes than mentioned above.

Sunday, January 18, 2015

Guake and Fish

With many thanks to Alan Pope via the Ubuntu UK Podcast I've switched my terminal environment from the standard gnome-terminal with bash to Guake and fish.

Guake suits my workflow as I like to have applications in separate workspaces and switch between the terminal and application.  Guake, which gets its name and style of operation from early first-person-shooter games such as Quake (except it's for Gnome, get it? :-)) is easily accessible via a hotkey - F12 by default - and will always display in the current workspace.

To learn more, see:

* https://github.com/Guake/guake - the official repository
* http://dalibornasevic.com/posts/48-guake-terminal-for-super-productivity - provides some useful configuration tips

To install and automatically start at logon in Ubuntu 14.04:

sudo apt-get install guake
ln -s /usr/local/share/applications/guake.desktop ~/.config/autostart/guake.desktop


Fish - the Friendly Interactive Shell for the 1990's is a step up on bash for two simple reasons:

1. Searching and running previous commands is much more convenient than in bash.  Once can simply type in a part of the command, any part, press the up arrow and step back through commands that contained the search string.

2. While typing a command fish will offer to auto-complete it with what it thinks you want, based on command history and the current directory contents.  You can 1) ignore the offer and type your command in full, 2) press tab to auto-complete in the same manner as bash, or 3) press the right arrow to accept the suggested command, and then Enter to execute the command.

The version that is included in Ubuntu 14.10, lacks two key features that I use: vi style editing and shared history across sessions.

Fortunately, the release currently in development addresses both of these.  To install the nightly build on Ubuntu:

sudo add-apt-repository ppa:fish-shell/nightly-master
sudo apt-get update
sudo apt-get install fish


This, of course, comes with the usual disclaimer that it is still in development and you are likely to encounter bugs.  I haven't had any problems yet, and figure that if something does go drastically wrong I can boot a USB stick and set my shell back to bash until the problem resolves itself.

To learn about fish, simply type help at the prompt and a browser window will be opened.

To edit your configuration, type fish_config and once again a browser window will be opened that will allow you to set colours (as the official web page says, you'll have an astonishing 256 colors available for use), modify the prompt, view functions, variables, command history, key bindings and abbreviations.

To enable vi mode and shared history place the following in your config file (~/.config/fish/config.fish):

fish_vi_mode
function fish_prompt
    history --merge  # This enables shared command history across sessions
    fish_vi_prompt
end


Vi mode editing is still a work in progress, and is missing a few features, e.g. replace mode, however, if like me, you only use it for editing particularly long commands it will probably be more than adequate.

You can also get a poor man's replace (r) function by adding the following:

bind -m insert r delete-char force-repaint


This simply deletes the character under the cursor and puts the editor into insert mode.  Saves a keystroke sometimes.

Useful links:

* http://fishshell.com/ - the official website
* https://github.com/fish-shell/fish-shell/wiki/Nightly-builds - nightly builds

Enjoy!