Solved

script to perform a task on each file in a directory

Posted on 2006-06-12
14
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using a variable to name output in powershell script 2 43
Do Psexec queries install files on remote computers 6 87
Logon script fails 23 60
IDE for Python 5 64
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

740 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