Brain Dump

A place to store my random thoughts and anything else I might find useful.

Posts Tagged ‘linux’

Transcoding Video for Android

Posted by mzanfardino on September 17, 2013

Contents

1. Overview
2. Solution
3. Summary

Overview

Android devices have a limited number of video and audio codecs that are supported natively. I have a collection of videos that I’d like to be able to watch on my Nexus 10, but simply uploading the files does not always work. Many of the files are encoded in mkv or avi containers with mp3 or ac3 audio encoding. These will not play with the native video player and attempts to install a full-featured player such as vlc have been frustrating. The solution is to transcode the video to mp4 encoding with aac audio encoding. Note: there are other compatible video and audio codecs such as 3gpp, but I have found that mp4 and aac are most straight-forward codecs to transcode.
top

Solution

Transcoding can be performed with a number of different tools and a number of different approaches may well be valid. I am documenting here the solution that works for me. I have been using ffmpeg for years to transcode, so naturally this is the tool I turned to. Assuming all the appropriate libraries have been installed, you can use ffmpeg to transcode from mkv or avi with a few simple switches.

The first issue I wanted to address was the size of the files. A number of my files are in excess of 1GB. This is due to the frame-size of the videos: some as large as 1280×720. I clearly don’t need that high resolution for playback on my Nexus 10, so reducing the frame-size to something more manageable has the desired effect of reducing the file size. I prefer video scaled to 720xW (where W is relative to the original scale dimension). Why this particular frame-size? It’s high-enough resolution to maintain a decent quality video; it’s small enough that most videos will down-size to this frame-size (rather than forcing an up-size which can be ugly); and maintains the correct aspect ratio. You can of course substitute any frame-size you like.

The second issue I need to address is audio codecs. Many of the videos I have are encoded using ac3 which is not supported. I chose aac as my audio codec as this is supported by the native video player and it will preserve 5.1 if the source is encoded as such.

The following is the general form of the command I use:

$ ffmpeg -i foo.mkv -c:v libx264 -preset slow -crf 22 -vf scale=[1920,1280,720]:-1 -c:a aac -strict -2 foo.mp4

I wanted to transcode both avi and mkv in one pass. I also wanted to do more than one file at a time, so I constructed the following command to transcode a block of files:

$ for f in *.{avi,mkv}; do ffmpeg -i ${f} -c:v libx264 -preset slow -crf 22 -vf scale=[1920,1280,720]:-1 -c:a aac -strict -2 ${f/${f: -3}/mp4}; done;

NOTE: in the above the ‘scale’ parameter should be only one of the three suggested values. By setting the width to -1 the resulting video will be scaled correctly. One caveat: in some instances the width value returned from -1 will be an odd number which will cause ffmpeg to throw an error. In that case I mere adjust the width to the next highest even integer and rerun the command providing the calculated width rather than the -1.

There are a number of consideration when performs transcoding that I have not touched on. I have tried to address the most common situations I’ve run into relating to: video and audio codec as well as maintaining aspect ratio.

The final step is to transfer the newly transcoded videos to my Nexus. This was done with a simple adb push command (no need to root access).
top

Summary

Android is a wonderful platform with a number of fantastic features. However, given the limited set of video and audio codecs supported, it’s necessary to convert your videos you might otherwise be able to “just play” on your PC. The process I’ve outlined is just one way to achieve this goal. With linux and adb it can be a very easy process to convert your videos to a playable format (while reducing the file size) and move them to your favorite android device.
top

Posted in android, ffmpeg, linux, transcode, video | Tagged: , , , , | Leave a Comment »

Replacing default desktop manager with qingy on Ubuntu intrepid

Posted by mzanfardino on January 30, 2009

I often find that I want to login to a text console as opposed to my GUI. I use Ubuntu 8.10 (intrepid ibex) with gnome which uses gdm desktop manager by default. I’ve found in the past while working with gentoo that I can replace the GUI login provided by kdm, gdm, etc. with qingy which uses framebuffer video support to provide a nice splash screen without the overhead of a full-blown GUI.

With the introduction of upstart as a replacement for inittab in ubuntu there are different steps to making qingy work and the qingy (and other) sites suggest. First, install qingy (this part hasn’t changed!). I installed from repos, but you can download and install from the qingy home page (http://qingy.sourceforge.net/). the repos will place the quingy binary in /usr/sbin. Next, edit /etc/event.d/tty1. Comment out the statement # exec /sbin/getty 38400 tty1 and add a new statement exec /usr/sbin/qingy tty1. This will alter tty1 from loading getty to qingy. NOTE: Many how-to’s suggest replacing getty with qingy for all but one tty. I do not feel this is necessary, as I’m merely using qingy as a replacement for gdm and I only need one instance of this.

Next, disable your desktop manager. I’ve stated that I use gdm so if you are using kde or xdm (or something else entirely) you may have to figure out the best way to do this. The easiest way to disable gdm is by using bum (boot-up manager GUI). This will likely have to be installed using your favorite installation method. I use sudo aptitude install bum from the command line. Once running, locate GNOME Display Manager and un-check it. If you click apply the GUI session will end! You have been warned!

At this point you should be able to boot your machine and find that you have a nifty splash screen with options to start different sessions, among them will be text-console.

Note: you will likely want to change some of the default configuration for qingy. For instance, by default qingy will launch your GUI session in the open tty (tty1 in this case). If, like me, you are used to seeing the GUI on tt7, you have to change /etc/qingy/settings x_server_tty = unused_tty. There are many other settings that can be adjusted in the setting files as well.

Also, you can find a nice selection of themes for qingy on freshmeat (http://themes.freshmeat.net)

*** HEADS UP ***
Now, this is where things got a little frustrating for me. When I logged into a gnome session, I discovered that my audio was not initializing. I could not playback audio or work with audio in any way. I did not get any specific errors: dmesg, /var/log/syslog and /var/log/messages did not reveal any obvious errors. I tested audio from a text-console and found that I could playback mp3 files using mpg321 or ximp3 (both console based mp3 players), but only if I run these apps as sudo.

This gave me the vital clue that helped to solve the problem with gnome. It turns out that my user account was not a member of the audio group that is installed when ubuntu was installed. It’s apparently not necessary when you use gdm as your desktop manager. By adding my user to the audio group I was able to playback audio from text-console without the need of sudo *and* when I logged into my gnome session audio was working!

In addition to audio not working, nm-applet (the panel NetworkManager icon control) is also not working.  This item took a little more effort to resolve, as it’s not a simple matter of adding a user account to a group.  The way I resolved this might not be the correct method, so please do your own research! However, I found that if I modified a number of files in /etc/dbus-1/system.d/ I could make it work.  Specifically I modified the following files:

  1. /etc/dbus-1/system.d/ConsoleKit.conf
  2. /etc/dbus-1/system.d/NetworkManager.conf
  3. /etc/dbus-1/system.d/nm-applet.conf
  4. /etc/dbus-1/system.d/nm-avahi-autoipd.conf
  5. /etc/dbus-1/system.d/ nm-dhcp-client.conf
  6. /etv/dbus-1/system.d/nm-dispatcher.conf
  7. /etc/dbus-1/system.d/ nm-system-settings.conf

For each of these files I copied the policy user section “root” and changed the user to my user account.

TO REPEAT: This may *not* be the correct way to do this, but in the end it worked.

Another issue I had was with the use of encfs which I used for directory encryption. I found that until I added my user account to the fuse group I was unable to mount my encrypted directories.

I suspect that there are other issues as well with this approach.

If you have any suggestions or comments regarding this method vs. some other method, please post here or provide a link to the suggestion.

Posted in linux, ubuntu | Tagged: , , , , , , , | 7 Comments »

Installing gentoo with root encryption: notes

Posted by mzanfardino on October 23, 2008

Note: This is compiled from several sources (which I will site at the end) and my own experience. This example assumes the following partition table:

/dev/sda1 /boot
/dev/sda2 swap
/dev/sda3 /
  • Follow Gentoo Handbook through 4.d. FUTURE: Add notes regarding flushing drive with /dev/urandom before encrypting.
  • Be sure to emerge cryptsetup. (NOTE: this should not be necessary as gentoo 2008.0 includes luks support).
  • Load necessary modules:
    modprobe dm-crypt # required
    modprobe dm-mod # required
    modprobe serpent # optional
    modprobe blowfish # optional
    modprobe aes # optional but required for aes-cbc-essiv
  • Encrypt the swap partition and mount it. NOTE: Encytping with random passkey – does not require passkey to mount:
    cryptsetup -c aes-cbc-essiv:sha256 -d /dev/urandom create swap /dev/sda2
    mkswap /dev/mapper/swap
    swapon /dev/mapper/swap
  • Encrypt the root partition. NOTE: This example assumes a keyword/passphrase. FUTURE: Add examples for keyfile encryption:
    cryptsetup -y --cipher serpent-cbc-essiv:sha256 --key-size 256 luksFormat /dev/sda3
  • Open newly encrypted partition:
    cryptsetup openLuks /dev/sda3 root
  • Create the file system on the logical root. This example assumes ext3. FUTURE: Add notes concerning the user of alternate file system:
    mke2fs -j -m 1 -O dir_index,filetype,sparse_super /dev/mapper/root -L root
  • Create the file system for /boot. This example assumes ext2 (as you needn’t journal a boot partition). FUTURE: Add notes concerning the use of alternate file system:
    mke2fs /dev/sda1 -L boot
  • Mount the file system for use during the remainder of install:
    mount /dev/mapper/root /mnt/gentoo
  • Return to the Gentoo Handbook from step 5 to 7.d (genkernel)
  • Build the kernel with genkernel
    cd /usr/src/linux
    genkernel --luks --menuconfig all

    The key here is that you configure your kernel for ramdisk support and luks support FUTURE: add details.
  • Continue with Gentoo Handbook from 7.e till 8.a
  • Modify /etc/fstab to mount root from /dev/mapper/root. fstab will look as follows:
    /dev/sda1 /boot ext2 defaults,noatime 1 2
    /dev/mapper/swap none swap sw 0 0
    /dev/mapper/root / ext3 noatime 0 1
  • Modify /etc/conf.d/dmcrypt to mount swap during boot:
    swap=swap
    source='/dev/sda2'
  • Continue with Gentoo Handbook from 8.b till 10.b.
  • Configure GRUB to use genkernel:
    title Gentoo Linux 2.6.25-r8 (genkernel)
    root (hd0,0)
    kernel /boot/kernel-genkernel-x86-2.6.25-gentoo-r8 root=/dev/ram0 init=/linuxrc ramdisk=8192 crypt_root=/dev/sda3 vga=791
    initrd /boot/initramfs-genkernel-x86-2.6.25-gentoo-r8

    NOTE:kernel name may be different,be sure to use the correct initramfs and kernel.

Unbelievably that’s it! After days and days of trying to create my own initramfs and writing my own linuxrc files it really comes down to passing using genkernel to build the kernel and initramfs with luks support by passing genkernel –luks! (well, and the rest).

More to come later.

Citation:
http://gentoo-wiki.com/SECURITY_System_Encryption_DM-Crypt_with_LUKS#Preparing_the_disk
http://gentoo-wiki.com/SECURITY_Encrypting_Root_Filesystem_with_DM-Crypt_with_LUKS#Using_initramfs_and_busybox
http://gentoo-wiki.com/SECURITY_Encrypting_Root_Filesystem_with_DM-Crypt
http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=1&chap=1

Here is a handy one for when you have to chroot into your system because you omitted something and it doesn’t boot: http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=1&chap=6

Posted in gentoo, linux | Tagged: , , , | 2 Comments »

Access MySQL with mysql-query-browser over ssh tunnel

Posted by mzanfardino on October 23, 2008

Problem:
Your mysql database lives on a server that has all but a handful of ports blocked. Specifically you can’t use mysql-query-broswer to connect directly as port 3306 is blocked.

Solution:
Tunnel traffic through an ssh connection and forward the traffic from some other port (assuming you have mysql running on your localhost on port 3306) to port 3306.

How?

Simple: Open a new shell and create the tunnel on the local machine to the remote server as follows:

ssh -L $localport:127.0.0.1:3306 -l $remoteuserid $remoteserveraddress

  • $localport can be any available port, include 3306, but only if mysql is *not* running on the local machine.
  • $remoteuserid is the user id used to connect to the remote server.
  • $remoteserveraddress is the address of the remote server.

Once the tunnel is established, leave the shell open. You might get timed out depending on your server configuration, so you might need to have some application running on the remote server to maintain the connection.

Now launch mysql-query-browser, set the Server Hostname to 127.0.0.1 and the port to $localport. That’s it!

Posted in linux | Tagged: , , , , | 1 Comment »

Linux: How to kill and logout users

Posted by mzanfardino on September 23, 2008

Task: How to halt/stop a user

# skill -STOP -u <user>

You muse be root to stop other users. For debian/ubuntu you should precede this command with sudo.

Task: How to resume already halted user

# skill -CONT -u <user>

Task: How to kill and logout user

# skill -KILL -u <userid>

Task: Kill and logout all users

The ultimate command to kill and logout all users:
# skill -KILL -v /dev/pts/*

Posted in linux, ubuntu | Tagged: , , | 1 Comment »