Sunday, December 17, 2006

Put Youtube video to ipod using ffmpeg

Put Any YouTube Video On Your iPod is a good source for expanding the video library on the wonderful video iPod. A Beginner's Guide to Video for the IPod also introduced a number of commercial or free programs that can convert videos for the iPod.

However, I have developed a fondness for ffmpeg through my previous effort of making movies from still images. In addition, I found out that a lot of the existing video authoring programs are actually derivatives of ffmpeg. With a little googling around, I found this page - Converting video to play on your iPod with FFmpeg. After messing around different options, I finally settle with this command line:

I download flash videos using firefox videodownloader extension. All the downloaded video dimensions are 320x240, so no more fiddling is needed. It seems that people often misinterpret the -maxrate option. The correct meaning of it is maximum bitrate tolerance, so the output bitrate is usually dictated by the input bitrate since we don't want too much deviation.

If you are a fetish Java user, try this code to pool all the videos into a folder and convert them all at onece


import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
/**
* Convert the flv video files into ipod video files using ffmpeg.
* Usage:
* java itube [source_path] [prefix] [number_of_files]
*
* @author yctan
*
*/
public class itube {
public static class AlphabeticComparator implements Comparator {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
return s1.toLowerCase().compareTo(s2.toLowerCase());
}
}
public static void main(String[] args) {
File path;
String[] list;
if(args.length == 0)
path = new File(".");
else
path = new File(args[0]);
list = path.list();
if(list == null) {
System.err.println("File not found.");
System.exit(1);
}
Arrays.sort(list, new AlphabeticComparator());

String prefix = null;
if(args.length > 1) prefix = args[1];

int nfiles = list.length;
if(args.length == 3)
nfiles = Integer.parseInt(args[2]);

try {
PrintWriter logfile = new PrintWriter(new BufferedWriter(new FileWriter("itube.log")));
for(int i = 0; i < nfiles; i++) {
int dot = list[i].lastIndexOf('.');
if(dot==-1 || !list[i].substring(dot+1,list[i].length()).equals("flv")) continue;
String cmd = "ffmpeg -i " + path.getPath() + '/' + list[i];
cmd += " -f mp4 -maxrate 1000 -b 300 -qmin 3 -qmax 5";
cmd += " -bufsize 4096 -g 300 -vcodec mpeg4 -acodec aac ";
if(prefix!=null) cmd += prefix + '-';
cmd += list[i].substring(0, list[i].lastIndexOf('.'));
cmd += ".mp4";
// System.out.println(cmd);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
logfile.println("Converting " + list[i] + " ... ");
while((line = br.readLine()) != null)
logfile.println(line);
logfile.println("Done.");
int exitVal = proc.waitFor();
logfile.println("Process exitValue: " + exitVal);
}
logfile.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Wednesday, December 06, 2006

Convert RMVB to iPod Video

As I started my quest to put RMVB videos to my newly acquired video ipod, I realized that ffmpeg simply does not have the codecs to decode this RMVB format. I kept googling the potential candidates but most of the solutions I found are non-free/commercial software. Finally, I took a chance with mencoder and had some success. It's not a smooth sail using mencoder. I tried it first on my 64 bit AMD linux machine, and none of the realvideo codecs worked. Therefore, I have to do it on my windows machine :-( Followings are the steps to successfully converting the RMVB:
  1. Download MPlayer 1.0rc1 Windows and grab "windows-all-20061022.zip" from codecs directory.
  2. Unzip both of them into the same directory. Let's say, C:\mplayer.
  3. Assume you want to convert "Cars.rmvb" and it is in "C:\Documents and Settings\All Users\Documents\". Open Notepad to type the following textbox as a single line and save the file as "encode.bat".
  4. Double click "encode.bat" to execute it. "Cars.mp4" will be created. Use iTune or gtkpod (on your linux machine) to transfer the mp4 into ipod :-)

"encode.bat"

The first thing we should keep in mind is the video ipod limitation. From ipod specs, we can immediately learn that the allowed video formats are H.264 and MPEG-4, and the allowed audio codec is AAC. For MPEG-4, the pixel limit is 640x480 and the max. frame rate is 30 fps. As a result, "vcodec=mpeg4" and "acodec=aac" are used.

Of course, we also need to understand the command line of mencoder to successfully convert every given RMVB file. The listed command line in "encode.bat" is very similar to Encoding to Sony PSP video format from the mencoder documentation. We'd better have this documentation ready to explain every parameter that can be used.

Another crucial element is understanding the video source - video bit rate, fps, and aspect ratio. Read video info using either Real Player or Real Alternative. For example, the source Cars.rmvb has these properties

  • Avg Stream Bit Rate: 996.9 Kbps
  • Dimensions: 800x452
  • Encoded Frame Rate: 24.0 fps
Therefore, I use
  • vbitrate=800 to sacrifice a little bit quality to control the output file size, I certainly don't think there is any benefit to use bit rate larger than 996.9 Kbps.
  • -ofps 24.0 to match the output frame rate. Otherwise, mencoder will keep complaining skipped frames.
  • -vf scale=640:362 to keep the correct aspect ratio and shrink the pixel size (remember the ipod limitation).

Monday, December 04, 2006

Black Forest Delight


How-to: Bake devil's food cake mix. Dissolve cherry gelatin and pour into cake layers. Mix sour cream and powdered sugar and stir in whip cream. Spread whipped topping mixture and cherries on the first cake layer. Place second cake layer on the first cake layer. Frost with remaining whipped topping mixture. Drizzle chocolate on the top. Garnish with cherries. Yummy.

Monday, June 26, 2006

Saturday, December 31, 2005

Make Mouse Scroll Wheel Work


I just made my PS/2 mouse scroll wheel work again in SUSE Linux 10.0. Who would have thought the correct protocol name should be imps/2? The protocol change does not take effect until the system is restarted, so the testing immediately after the switch is meaningless. Despite all the wonderful features in the latest SUSE Linux version, there are still some quirks I have picked up so far.
  1. My 16X DVD-ROM drive is treated as a CD-ROM drive such that no DVD can be read. I need to painfully do a switcheroo with a Plextor DVD drive during the initial system setup.
  2. By switching the USB and PS/2 mice after the initial system setup, I lost the correct setup for the PS/2 mouse. Hence I endured the annoyance of the loss of scroll wheel function until I figured out what protocol I need to choose.
  3. A sudden loss of OpenGL function due to some incompatible system packages (although I never figured out the real cause). The solution is to re-install the Nvidia video driver.
  4. Eclipse (3.1 and 3.2) will somehow become unstable and finally cannot start on the 64 bit JVM provided by SUSE, but it manages to work under the latest JVM 1.5.0 update 6 directly from Sun.

Monday, November 28, 2005

Give Me a Break

There are a number of things are newsworthy.

Michael Scofield and his team are going to break out of Fox River State Penitentiary tonight :-)

On November 18, my new computer ceased to work after being assembled merely three weeks earlier. Without understanding the true cause of the breakdown, I placed the blame on the mother board - Epox 9NDA3J nForce3. I regretted the premature disposal of the packaging boxes because by doing so I cannot refund the defected items from NewEgg.com. Desperately I bought another mother board - Gigabyte Uli 1689 from NewEgg, and it should arrive today. The temperature rises to a near-term peak today. And I will be assembling my new computer tonight ...

My last blog - trip to Yellowstone - is heavily spammed in addition to my mailbox. I don't exactly know how the spammers benefit from commenting my blog entry and placing a link back to their web sites. Are they trying to raise the Google ranking of their own web sites?

XBox 360 started selling on November 22, which is right before the Thanksgiving shopping craze. But rumor has it - PS3 will be sold with Linux pre-installed. Very sweet. With all the hype on HD gaming, yesterday I installed, DOSBox, a MS-DOS emulation program such that my Mom can play Lode Runner, a pre-historic yet extremely fun game. And that's what a brilliant video game should have, an innovative gameplay instead of only pretty high resolution.

Skiing at Winter Park