Saturday, February 28, 2015

Arch Linux and Arduino with your favorite IDE

You can edit your Arduino code from any IDE and compile it / upload it without using the Arduino IDE. To do that, the ino tool comes to the rescue.

If you are using Arch Linux you might be interested to know I uploaded a package named ino-1.6-git on AUR. It has been patched to work with the latest version of the Arduino SDK.

Installation

yaourt ino-1.6-git
sudo gpasswd -a $USER tty
sudo gpasswd -a $USER uucp
sudo gpasswd -a $USER lock

Log out and in again, or reboot.

Quick Start:

cd my-arduino-project
ino init
$EDITOR src/sketch.ino
ino build
ino upload

Happy Arduino-ing!

Saturday, February 21, 2015

Remove audio from a video on Linux

If you are working on a computer vision project, there is a great chance that you have been recording videos with sound, which you have no use of. To save space and processing power, you can strip the audio from the video just like this:

avconv -i input_video.mp4 -an output_video.mp4

(Replace avconv with ffmpeg if necessary.)

Just so you know, there is something dishonest with avconv as they make you believe ffmpeg is dead or obsolete. In fact it's nothing like that. avconv is a fork of ffmpeg and has become the default on Ubuntu. So if you are reading to use a filter that isn't present in avconv, then you need to download the latest version of ffmpeg from source or from a PPA. Read more on this subject here.

Rotate a video on Linux

When you record videos on your smartphone, the video itself is not rotated. The video recorder simply adds a tag telling the player how to rotate the video.
That's why mediainfo reports "Rotation: 180°" or other angles on videos recorded from my Samsung Galaxy S4.

Unfortunately unlike the video player on your phone, the vast majority of video players on desktop computers and TVs don't support this tag.

If the video is upside down:

avconv -i original_video.mp4 -vf "vflip,hflip" output_video.mp4
or
avconv -i original_video.mp4 -vf "transpose=2,transpose=2" output_video.mp4

(You can replace avconv with ffmpeg if avconv is not found on your system.)

Note that the first version is slightly faster (2-5 %).

If the video was recorded in portrait mode:

if the top of the image is on the left of the screen, it needs a +90° rotation:

avconv -i original_video.mp4 -vf "transpose=1" output_video.mp4

if the top of the image is on the right of the screen, it needs a -90° or +270° rotation:

avconv -i original_video.mp4 -vf "transpose=2" output_video.mp4

Sunday, February 15, 2015

Improving FabulaTech USB over Network

I've been looking for a solution to share a USB device over the network for years. Unfortunately, as of today, no viable opensource solution exists. usbip is supposed to be that solution, but it's broken and I never managed to make it work on Ubuntu, despite the program being available in universe. The installation on Windows is a very tedious process, even more complicated IMO than the procedure below.

There are however commercial solutions. One of them is Fabulatech USB over Network. It will work on Windows, Mac and Linux.

On Linux, there is no graphical user interface, and they don't provide anything that would make our lives easier. Only a server program and a client program that can be invoked from the command-line.
That's why we'll have to write a daemon and a script to automatically share some devices after they are plugged in.

Step 0 : Unzip the Linux tarball somewhere

Let's say this somewhere is /opt/ftusbnet.

Step 1: Start the daemon (ftusbnetd) at boot

Install supervisord: sudo apt-get install supervisor

Write a configuration file for supervisor by editing /etc/supervisor/conf.d/ftusbnetd.conf:

[program:ftusbnetd]
command=/opt/ftusbnet/sbin/ftusbnetd
autostart=true
autorestart=unexpected

Restart supervisord: sudo service supervisor restart
(You can use supervisorctl if you don't want to restart all the running services.)

If you have never used supervisor before, you really should. Regular init scripts are still useful, but most of the time supervisor does the job much better, with only two lines.

UPDATE: supervisor 3.0b2-1 distributed with Ubuntu 14.04.1 LTS has a little bug. It won't start, after complaining that /var/log/supervisor doesn't exist.
You can either change the line childlogdir=/var/log/supervisor in /etc/supervisor/supervisord.conf, or create the directory before starting supervisord by modifying /etc/init.d/supervisor and adding the line mkdir /var/log/supervisor after case "$1" in start)

Step 2: Run a script when a USB device is connected

Read the previous blog post.
Let's assume the "add" script is /opt/ftusbnet/bin/udev-connect.

Usually, to share a device you would issue the ftusbnetctl command like this:
ftusbnetctl share 1234, where 1234 is an ID generated by ftusbnet and that even changes each time you plug the device.
It would be nice if we could tell udev to run a script only after ftusbnetctl "sees" the USB device. Unfortunately we can't do that. The device won't be accessible until udev has run all the scripts we told him to run. We can't just "sleep" or start a background process. 

The only workaround I found is to run the script 1 to 60 seconds after the USB device is processed by udev. So yes: You might have to wait up to a minute before the device is shared. 
For this we will use the at utility. Unfortunately, at is an old UNIX tool and it only works with minutes, days, and weeks. Tell me if you find a better solution!

/opt/ftusbnet/bin/udev-connect contains this:

#!/bin/sh
echo "/opt/ftusbnet/bin/scansnap-connect" | at 'now + 1 minute'

In my case I am connecting a device that is shown as "ScanSnap" when executing ftusbnetctl list.
Make sure the script is executable.

Step 3: (Cont'd)

Here is the content of /opt/ftusbnet/bin/scansnap-connect:

#!/bin/sh

ftusbclient=/opt/ftusbnet/bin/ftusbnetctl

while ! $ftusbclient list | grep ScanSnap
do  
    sleep 1
done
sleep 1

ftid=$( ${ftusbclient} list | grep ScanSnap | awk '{print $1;}' )
logger -t ftusbnet-udev "Sharing device ID $ftid"
$ftusbclient share $ftid

Make sure the script is executable.

The before-last line will print a message in the syslog. If all you never see the line or the ID is truncated, you have a problem.

Ubuntu: run a script when a USB device is plugged in

There's a lot of stuff you can do with udev. One of them is running a script when a USB device is plugged in.

It's as simple as:

1. figure out the vendor id and product id with lsusb
2. edit /etc/udev/rules.d/85-something.rules:

ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="04c5", ATTR{idProduct}=="11a2", RUN+="/absolute/path/to/your/script/that/will/be/executed/as/root"

3. Write the script mentioned above and make sure it's executable.
4. Restart udev: service udev restart.

You can do other things with udev such as managing /dev/ and the permissions on the device.
However you should not use udev to mount USB disks, there are other tools for that.

To run a script when detaching a USB device, replace ACTION=="add" with ACTION=="remove". You can put several lines in the same ".rules" file.


Two scripts for the Cisco VPN Client

I recently wrote a post about using openconnect instead of the Cisco VPN Client on Linux. If you really want to use the Cisco VPN Client but miss the handy vpnc and vpnd commands, this article is just for you.
It uses the expect tool from Don Libes, who wrote it in 1990 when he was working for the U.S. government. Therefore, Expect is in the public domain.

Before executing the commands and modifying the script, you should run /opt/cisco/anyconnect/bin/vpn to understand what is going on. The script tries to mimic a user waiting for the console to print things, and then type on the keyboard.

Here's your vpnc script:



And here is vpnd:

Friday, February 13, 2015

Speed up Windows updates

Apparently there's something you can do to speed up Windows updates:

1. Open a command-line prompt with elevated privileges (Win + S, search "prompt", right-click, Run as administrator)
2. sfc /scannow

The Windows System File Checker tool will detect corrupt system files, and some people have experienced a noticeable speed up of the Windows updates.