views:

1146

answers:

3

Hello all,

I have this Shell script and I've managed to muck it up and I was hoping I could be corrected and put on the right path and hopefully add a few things that I am not competent enough to do myself. I have put what I want do as comments in the Shell script below.

#!/bin/bash
#Get all files from dir "videos" and send to processLine function one at a time
cd /home/test/videos/
for file in `dir -d *` ; do
processLine -f $file
done

processLine(){
# I was hoping to have a further for loop that would loop 4 times and change the $ext
#variable to avi, mpg, wmv and mov
#For loop, execute a command on each file
for i in 1 2 3 4 5 6 7 8 9 10
do
START=$(date +%s.%N)
echo "$line"
#The saved file in done dir should have filename as $file + START.
eval "ffmpeg -i $file -ar 44100 /home/test/videos/done/$fileSTART.$ext" > /dev/null 2>&1

END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)

echo "$line, $START, $END, $DIFF" >> file.csv 2>&1
echo "It took $DIFF seconds"
echo $line
done
}

Basic idea of the script is to: Get all files from dir and run an ffmpeg command on them and see how long it takes. I am trying to collect some stats

Thank you for any help

Update

Making use of Juliano's script and swapping for loops 2 and 3. I have managed to get this output below:

.
.
.
/home/test/videos/done 8 mov took 0.012 seconds
/home/test/videos/done 9 mov took 0.012 seconds
/home/test/videos/video1236104961.flv 0 avi took 0.446 seconds

It pauses there.

A: 

1) Where is $line defined?

2) Do you ever use $i?

3) You can loop over extensions via

for ext in avi mov mpg wmv; do
    ffmpg ...
done

4) You can do basic arithmetic in bash with double parens. So $(($x-$y)) instead of piping to bc

eduffy
+2  A: 

Many things are wrong.

  1. Don't use dir or ls in for loops.
  2. Why eval? What you expected to get?
  3. You use $line without defining it.
  4. Don't use bc to do math, since bash is already equipped to do it.
  5. Don't use date to measure time, bash already provides a command for that.
  6. What is "-f" passed to processLine() ?

Another try, fixing some issues:

#!/bin/bash
TIMEFORMAT=%6R
for file in /home/test/videos/* ; do
  if [ ! -f "$file" ]; then
    continue   # anything that is not a regular file
  fi
  for ext in avi mpg wmv mov; do
    for (( i = 0; i < 10; i++ )); do
      base="${file##*/}"
      elapsed=$({ time ffmpeg -i "$file" -ar 44100 -y "${file%/*}/done/${base%.*}-$i.$ext" &>/dev/null; } 2>&1)
      echo "$file $ext $i took $elapsed seconds"
    done
  done
done
Juliano
It would be nice to keep the function to run ffmpeg. time is a bash built-in and works on functions too. Looking at that ffmpeg command, I bet it won't survive the first round of maintenance. :)
Ken Fox
Man that is complicated! :) - I have uncommented everything and ran it. From the output, it worked perfectly. But I am guessing, its not actually executing the ffmpeg commands right?
Abs
Uncommented what? Note that there is nothing commented in my example. It is just the SO syntax highlighter that doesn't understand bash code. It runs ffmpeg, using the time builtin to measure its execution time.
Juliano
Ah I see - sorry my mistake. I have run shell script but for some reason it stops after doing the ffmpeg command for one file in the videos dir. Also I have swapped for loops 2 and 3. After I swapped the for loops around it only managed to execute one conversion. Updated Question.
Abs
Do I have to press enter for the shell script to continue? As I press enter it continues conversion.
Abs
Ok, I missed two problems: 1. the "done" subdirectory is in the same directory, and was being processed, fixed that. 2. ffmpeg stops and asks if you can overwrite the file if the output has the same name (that is why the script was pausing). Given unique filenames and forced ovewrite. Try now.
Juliano
A: 

I smell a bash pitfall.

$ touch aaa
$ touch "bbb ccc"
$ ls -1
aaa
bbb ccc
$ for file in `dir -d *`; do echo $file; done
aaa
bbb\
ccc
Anonymous