2

I am trying perform a batch operation to extract specific frames from multiple video files and save as a PNG, using a bash script. I hope to do this by using ffmpeg within a bash script, supplemented by a csv file that contains the name of the input video, the specific frame number to be extracted from the input video, and the output name of the PNG file.

The video files and the csv file would all be placed within the same folder. The script can also be placed in there if necessary.

My csv - called "select.csv" - currently takes the following format (input,output,frame):

mad0.m4v,mad0_out1,9950
mad0.m4v,mad0_out2,4500
mad1.m4v,mad1_out1,3200

My current script - called "frame.sh" - takes the following form:

#!/bin/bash
OLDIFS=$IFS
IFS=“,”
SDIR=/Users/myuser/Desktop/f-input/

cd $SDIR;
while read input output frame
do
   echo "$input"
   echo "$output"
   echo "$frame"
   input1=$input
   output1=$output
   frame1=$frame
   ffmpeg -i "$input1" -vf select='eq(n\,'"$frame1"')' -vsync 0 
"$output1".png
done < $1
IFS=$OLDIFS

This should allow me to run ./frame.sh select.csv to then process all relevant files in the "f-input" folder on my desktop and extract the specified frames.

I ended up echoing the variables read from the csv so that they could actually be used as variables and looped in the ffmpeg command because carrying out the ffmpeg command using $input, $frame and $output directly after the read operation only ever completed the process on the first line of the csv, without progressing further.

Essentially I would like the following to actually loop through each csv entry, instead of only the first line:

#!/bin/bash
OLDIFS=$IFS
IFS=“,”
SDIR=/Users/myuser/Desktop/f-input/

cd $SDIR;
while read input output frame
do
   ffmpeg -i "$input" -vf select='eq(n\,'"$frame"')' -vsync 0 "$output".png
done < $1
IFS=$OLDIFS

Any and all advice appreciated!

Many thanks

1
  • agree with dbl-quote solution. You're on your way, keep going!. Check you code at shellcheck.net to speed up you dev cycles. Good luck! Commented Sep 27, 2018 at 18:11

2 Answers 2

1

Replace IFS=“,” with IFS=",".

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - I hadn't picked up on the speech marks. However, with that change I am still not able to loop through each line and I can only parse the first line in the csv. Any other ideas?
Append </dev/nullto end of your ffmpeg line.
Cyrus - thank you for your answer. Completing that task has helped no end. For those that want to understand - if I understand correctly - adding </dev/null allows the process to perform it's next action without affecting existing files/permissions or anything else (outputting to nothing) and the process can continue to it's next stage.
0

I wrote a similar script that reads csv and process movies by ffmpeg. It works well on the first line of the csv but fails after the second lines.

I found ffmpeg in a loop seems to affect the "read" command and trim the first character of the lines after the second line.

So I ended up with adding extra "garbage" column on the left-most side of the csv and let ffmpeg trim it.

my csv is like:

101,movie1.mp4
102,movie2.mp4
103,movie3.mp4
...

and the (simplified) script is like:

while IFS="," read id movie; do
  ffmpeg -v quiet -s 1280x720 -i "$movie" "$id-$movie" </dev/null
done

it generates "101-movie1.mp4" for the first line of the csv just like I expect but after the second line it generates "02-movie1.mp4" "03-movie3.mp4" and so force because ffmpeg (seems to have) trimmed the first character of the lines.

I added a garbage column on the 1st column like this

x,101,movie1.mp4
x,102,movie2.mp4
x,103,movie3.mp4

and fix the script:

while IFS="," read garbage id movie; do
  ffmpeg -v quiet -s 1280x720 -i "$movie" "$id-$movie" </dev/null
done

this worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.