Archive for the ‘cool linux commands’ Category

intro to scripting in linux tutorial

Thursday, June 26th, 2008

I’m all about speeding things up. Letting the computer do what I don’t want to have to do.
I think that little things can add up.
In linux, the command line is king. You can add commands to your system by creating scripts.

A script in this case is just a text file that has code, and gets treated as a program- just as find and ls and cd are available to you, you can create as many new commands as you want.

You can use various languages to code a script to do something.
One of the most powerful aspects of living with linux is that you can add these scripts and customize the system to your liking. In fact, various people can on the same system without conflict! But that is another matter…

(more…)

cli pdfmerge

Friday, June 6th, 2008

I have a working release of pdfmerge.

My goodness! Why another pdf merging thingie!!!!

This one counts how many pages are in all docs, then compares to output, etc.. Anyhow, it’s somewhat safe.
And it’s a simple call. Instead of looking up how to use xpdf or something to merge pdfs, you can use pdfmerge via the command line. Yes, there *is* another pdfmerge out there. In fact like 2. One requires extra moolah for the ‘full’ version.. pleeezzz…
(more…)

copy part of movie

Wednesday, February 6th, 2008

I wanted to cut a chunk of footage from a video file.
Mencoder does this wonderfully, but, the command is not intuitive.

First, the example. This command goes to 10 minutes after the movie starts and takes 30 seconds after and spits out to ‘out.avi’..

mencoder ./filein.avi -ss 00:10:00 -endpos 00:00:30 -o ./out.avi

More practical example:

Fire up a terminal emulator.. open your source movie with mplayer.
You’ll need to see where in the movie you are, press ‘o’, get a pen and paper. This tells you the time elapsed.
You want to start your video selection at 00:14:24, that is, 14 minutes, 24 seconds.
And you want to stop at 00:15:24.
What you need to figure out is how much time there is betwen the start time and the stop time, in this case, one minute.

So our -ss seek/start time, will be 00:14:24
Once the movie is being copied, the end position (inside the copy movie) will be 00:01:00 (one minute).

Let’s put the command together..

mencoder ./in.avi -ss 00:14:24 -endpos 00:01:00 -o ./out.avi

Wait! Where is the stop time of 00:15:24?? RTFM, remember? endpos is telling mencoder where to stop, this is the target duration of the output movie we are making.

how to make an avi smaller

Wednesday, January 30th, 2008

Sometimes I have a divx file that is way too big to fit into a cd.
How the heck do you make an avi smaller?

I ripped a copy from dvd to an avi, and got it to under 700 megs, great. That fits in a cd. But then I had a 10 meg subtitle file! So, it didn’t fit! I needed to get the avi a little smaller.

If you look up info on mplayer, mencoder.. there’s a LOT to read.
So here’s how, in no way complete.
Just one way to solve the problem.

   mencoder in.avi -o out.avi -ovc lavc -oac copy  -lavcopts vbitrate=400:vpass=1

Now, what’s very cool about mencoder, is it gives you stats on progress..

   Pos: 101.5s   2435f ( 0%) 213.30fps Trem:  27min 628mb  A-V:0.036 [320:32]

‘Trem’ is rime remaining, and the mb count is the guess for how big it will be.
As the progress gets further along (beyond 10%), the stats will be more precise.

Downloading Images from a Camera with Linux

Tuesday, January 29th, 2008

Ingredients:

  • Linux distro (Fedora Core 4)
  • Camera USB (Kodak Easyshare LS743)
  • gphoto2

So I get a camera, and take pictures.. now what? I got linux. I thought this may be complicated.. But guess what..

Step 1

Make sure gphoto2 is installed. If not.. open shell cli prompt (whatever) and become su, then # yum -y install gphoto2, wait for it to complete..

Step 2

Make your dir to receive photos, change to it.. then # gphoto2 -P

Step 3

All your photos are downloaded to your current working dir. Now, delete the photos.. # gphoto2 -DR .. All the photos are deleted from your camera.

Done. That's it. No mouse clicking. No stupid dialogues. Wow.

how to join avi movies

Sunday, January 20th, 2008
mencoder -oac copy -ovc copy ./file1.avi ./file1.avi -o ./out.avi

That example may not place keyframes in- (you know how you use the arrow keys in mplayer to forward or rewind?)
For an example that would put in keyframes:

mencoder file1.avi file2.avi -oac copy -ovc lavc -o out.avi

For closer together keyframes:

mencoder file1.avi file2.avi -oac copy -ovc lavc -o out.avi -lavcopts keyint=5

This would change your output quality.

If you get an audio encoding error, you may have to choose an audio codec..

mencoder file1.avi file2.avi -oac pcm -ovc copy -o out.avi

If you don’t want to deal with keyframes, simply use the -forceidx option when you play the movie

mplayer -forceidx ./out.avi

What is mencoder?

mencoder is part of mplayer, you can install via yum on fedora core packages (as root)..

yum -y install mencoder

I had a lot of these to join.. so..
I made a perl script that I can use use without so much fuss.. one that I don’t have to tell it what the output file is going to be.
I put it in ~/bin/avijoin

#!/usr/bin/perl
use strict;

my $outfile = outfile();

my @arg = (qw(mencoder -oac copy -ovc copy), @ARGV, '-o',$outfile);

system(@arg) == 0 or die($?);

print "Wrote:\n$outfile";

exit;

sub outfile {
@ARGV or die('missing args');
my $outfile = $ARGV[0];
$outfile=~s/([^\/]+)\.(\w{1,5})$// or die;
my ($filename,$ext) = (uc $1,$2);
my $x=0;
$filename=~s/\W//g;
my $_outfile;

for(1){
$_outfile="$outfile/$filename$x.$ext";
last unless -e $_outfile;
$x++;
}
return $_outfile;

}

Make sure to chmod 0755 ~/bin/avijoin

rename

Wednesday, January 16th, 2008

I want to go over linux rename command very quickly. It’s a simple command but I used to find it tricky and unintuitive.

The man rename output examples of foo and bar are freaking cryptic.
So I want to show some real world examples of rename

Let’s do a directory listing…

[leo@localhost rename_example_dir]$ ls -l
total 1872
-rw-rw-r-- 1 leo leo 845805 2008-01-16 20:36 P1010591.JPG
-rw-rw-r-- 1 leo leo 914240 2008-01-16 20:36 P1010624.JPG
-rw-rw-r-- 1 leo leo  38167 2008-01-16 20:35 Picture 11.jpg
-rw-rw-r-- 1 leo leo  29726 2008-01-16 20:35 Picture 16.jpg
-rw-rw-r-- 1 leo leo  25545 2008-01-16 20:35 Picture 21.jpg
-rw-rw-r-- 1 leo leo  23886 2008-01-16 20:35 Picture 22.jpg
-rw-rw-r-- 1 leo leo  14736 2008-01-16 20:35 Picture 3.jpg

Great. Now, let’s change those filenames..

[leo@localhost rename_example_dir]$ rename 'Picture ' 'PICTURE_' ./Pictu*
[leo@localhost rename_example_dir]$ ls -l
total 1872
-rw-rw-r-- 1 leo leo 845805 2008-01-16 20:36 P1010591.JPG
-rw-rw-r-- 1 leo leo 914240 2008-01-16 20:36 P1010624.JPG
-rw-rw-r-- 1 leo leo  38167 2008-01-16 20:35 PICTURE_11.jpg
-rw-rw-r-- 1 leo leo  29726 2008-01-16 20:35 PICTURE_16.jpg
-rw-rw-r-- 1 leo leo  25545 2008-01-16 20:35 PICTURE_21.jpg
-rw-rw-r-- 1 leo leo  23886 2008-01-16 20:35 PICTURE_22.jpg
-rw-rw-r-- 1 leo leo  14736 2008-01-16 20:35 PICTURE_3.jpg

Ok, how about we match up the file extensions too..

[leo@localhost rename_example_dir]$ rename JPG jpg ./*
[leo@localhost rename_example_dir]$ ls -l
total 1872
-rw-rw-r-- 1 leo leo 845805 2008-01-16 20:36 P1010591.jpg
-rw-rw-r-- 1 leo leo 914240 2008-01-16 20:36 P1010624.jpg
-rw-rw-r-- 1 leo leo  38167 2008-01-16 20:35 PICTURE_11.jpg
-rw-rw-r-- 1 leo leo  29726 2008-01-16 20:35 PICTURE_16.jpg
-rw-rw-r-- 1 leo leo  25545 2008-01-16 20:35 PICTURE_21.jpg
-rw-rw-r-- 1 leo leo  23886 2008-01-16 20:35 PICTURE_22.jpg
-rw-rw-r-- 1 leo leo  14736 2008-01-16 20:35 PICTURE_3.jpg

Now let’s talk about what the heck happened.
The command is..

rename $takeout $putin $towhatfiles

First argument is what you do not want there, second argument is what you want to replace it with, third argument is a list of files, you can use wildcards and all the other freaky unix filematch operators.

how to deskew an image

Monday, January 29th, 2007

OVERVIEW

First thing you need is a way to figure out what the skew angle is.

unpaper 0.2

I tried using ‘unpaper’ v 0.2

But for some reason no matter how much I fumbled with the settings.. I could not get it to detect a skew.

pagetools 0.1

The next thing I tried was something called ‘pagetools’
http://sourceforge.net/projects/pagetools/
The current version for that is 0.1 (as of this writing).
You can see them on http://sourceforge.net/projects/pagetools/

This version has no real installer per se, you unzip, untar, and then
where you extracted you run ‘make’.
The following is an example of how you would go about doing this.
This may fail on your machine- use your head and don’t dispair.
(as root..)

	cd ~/
	mkdir tmp
	cd tmp
	wget http://downloads.sourceforge.net/pagetools/pagetools-0.1.tar.gz?use_mirror=internap
	gunzip pagetools-0.1.tar.gz
	tar -xvf pagetools-0.1.tar
	make

And likely get an error about missing pbm.h
WHY! Because this is provided by another package.
I did a yum search

	yum provides pbm.h

And got some results. Then chose what to install..

	yum install netpbm-devel

Great. Then I did a ‘make clean’ to wipe my previous make try..

	make clean

Now run the make again..

	make

And.. and?? What happenned??
What happened is if you look in pbm_findskew/pbm_findskew , this binary is what
was compiled for you.

Try the sucker out.

Imagine you have a skewed.png image..
(assuming you habe imagemagick installed on your system)

	convert skewed.png skewed.pbm
	./pbm_findskew/pbm_findskew skewed.pbm

Output is something like ‘0.839234′
Great. Now.. Use that value to fix your original

	mogrify -rotate "-0.839234" skewed.png

Why minus - ? Because pbm_findskew tells you how many degrees counter clockqise you must
rotate to get it straight.

Check it out.

	eog skewed.png

Conclusion

You could script this together pretty easy with perl/bash.
I was doing this originally to prep stuff for gocr. But.. I think the quality of the rotated
image is not as good for ocr reaing as the original!! Weird- but makes sense.