Link to home
Start Free TrialLog in
Avatar of kailee
kailee

asked on

script to perform a task on each file in a directory

Hello,
I need a script that runs:
mplayer kickboxing01.wmv -frames 1 -vo jpeg quality=90:outdir=./ -ao null
rm 00000001.jpg
convert 00000002.jpg -resize 50% -crop 140x120+20 th_kickboxing01.jpg


on each file in a directory.

So, instead of 'kickboxing01.wmv' it would be all <name>.wmv files in a given directory.
and each converted jpg would have its respective th_<name>.jpg

Currently, I run the script in the directory of the wmv files, due to complications in mplayer writing out the jpg.


Thanks,

Gordie
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

You could do this with a perl script

@wmv_files = <*.wmv>;
foreach $file (@wmv_files)
{
     $base_name = ($file =~ /(.*)\.wmv/);
     system("mplayer kickboxing01.wmv -frames 1 -vo jpeg quality=90:outdir=./ -ao null");
     system("rm 00000001.jpg");
     system("convert 00000002.jpg -resize 50% -crop 140x120+20 th_$base_name\.jpg");
}
You may need to add the full path to your mplayer and convert programs
find . -type f -name \*.wmv -exec echo "mplayer {} -frames 1 -vo jpeg quality=90:outdir=./ -ao null; rm 00000001.jpg; convert 00000002.jpg -resize 50% -crop 140x120+20 th_{}" \; |sh

# for testing simply omit the final |sh
Avatar of kailee
kailee

ASKER

ahoffmann,
your solution gets:
convert: unable to open image `th_./061007.wmv': No such file or directory.

teraplane,
your solution gets:
./thumb2: line 1: syntax error near unexpected token `;'
./thumb2: line 1: `@wmv_files = <*.wmv>;'

> .. `th_./061007.wmv': No such file or directory.
damn, missed that find returns paths

> on each file in a directory.
ls *.wmv| awk '{print "mplayer "$1" -frames 1 -vo jpeg quality=90:outdir=./ -ao null; rm 00000001.jpg; convert 00000002.jpg -resize 50% -crop 140x120+20 th_"$1}' |sh
Avatar of kailee

ASKER

ahoffmann,
now the thumbs are named '.wmv'
they're jpgs.
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kailee

ASKER

sorry for the delay, I get 'undetermined string'.

OZO, gimme a hand!!!!!
Avatar of kailee

ASKER

I ran
ls *.mpg|sed -e 's/\(.*\)\(\..*\)/\1 \2/'
and get the file listing with a space before '.mpg'
Avatar of kailee

ASKER

oh, I got it, missing close-quotes at the end.

does my suggestion work?
Avatar of kailee

ASKER

Yes.
Thanks.
If I put
mplayer "$1$2" -frames 1 -vo jpeg quality=90:outdir=./ -ao null; rm 00000001.jpg; convert 00000002.jpg -resize 50% -crop 140x120+20 th_"$1".jpg

in a file, like make_jp.pl, would the vars be passed into this file as it ran?  That would make this very flexible.

> .. would the vars be passed into this file as it ran?
yes, assuming your script is named "myscript", then start it like

myscript par1 par2
Avatar of kailee

ASKER

I mean something like,
ls *.wmv|sed -e 's/\(.*\)\(\..*\)/\1 \2/'| awk '{print "gen_process.script"}' |sh

Oh, I think you mean that myscript would contain
ls $par1|sed -e 's/\(.*\)\(\..*\)/\1 \2/'| awk '{print "par2$"}' |sh

where par2 would be gen_process.script

am I correct?
> am I correct?
sound like you a bit confused, or am I with your intend

1. parameters in shell script are referenced by $1, $2 and so on ...
2. you can assign the parameters to variables in shell, like
     par1="$1"
   and then use it like:
      echo "first parameter was $par1"
3. $1, $2, etc have different meaning in awk
4. $1 etc. in awk are not related to the calling shell anyhow
5. passing shell variables to awk is difficult, you either need a proper quoting or pass the variables as values of the input stream, or use gawk with -v option