Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

script to perform a task on each file in a directory

Posted on 2006-06-12
14
Medium Priority
?
185 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

722 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