Solved

script to perform a task on each file in a directory

Posted on 2006-06-12
14
182 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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