Solved

script to perform a task on each file in a directory

Posted on 2006-06-12
14
179 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:kailee
  • 7
  • 6
14 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 16884054
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16884242
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
0
 

Author Comment

by:kailee
ID: 16885568
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>;'

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 16889064
> .. `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
0
 

Author Comment

by:kailee
ID: 16891777
ahoffmann,
now the thumbs are named '.wmv'
they're jpgs.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 16892775
ls *.wmv|sed -e 's/\(.*\)\(\..*\)/\1 \2/'| awk '{print "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}' |sh

# assuming Gnu sed
0
 

Author Comment

by:kailee
ID: 16965973
sorry for the delay, I get 'undetermined string'.

OZO, gimme a hand!!!!!
0
 

Author Comment

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

Author Comment

by:kailee
ID: 16966047
oh, I got it, missing close-quotes at the end.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16966571
does my suggestion work?
0
 

Author Comment

by:kailee
ID: 16966608
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.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16966680
> .. 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
0
 

Author Comment

by:kailee
ID: 16966847
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?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16966872
> 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
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question