Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash variable changes in loop with ffmpeg

Tags:

bash

loops

ffmpeg

I wrote a skript to quickly create short preview clips from vides I recorded on timestamps that I found worth checking out later for cutting. My file with the timestamps is written like this

FILE_NAME1#MM:SS MM:SS
FILE_NAME2#MM:SS MM:SS MM:SS MM:SS

example:

MAH01728#02:47 03:34 03:44 05:00 06:08 06:55

The script looks like this:

#!/bin/bash
while read f
 do

 file=$(echo $f | cut -d"#" -f1)
 filename=${file}".MP4"
 timestamps=$(echo $f | cut -d"#" -f2)

for time in $timestamps 
 do 
  ffmpeg -ss 00:${time}.0 -i "orig/${filename}" -c copy -t 10 "preview/${file}_${time}.MP4"
 done
done < $1

The script gets half of the previews that I want and on the other the filename is messed up and ffmpeg complains that the file is not found:

orig/714.MP4: No such file or directory
orig/00:58 01:25.MP4: No such file or directory

So I modified the script for trouble shooting and just put an echo in front of the ffmpeg command - now all file names are correct. What am I missing?

ffmpeg -ss 00:01:47.0 -i orig/MAH01714.MP4 -c copy -t 10 preview/MAH01714_01:47.MP4
ffmpeg -ss 00:02:00.0 -i orig/MAH01713.MP4 -c copy -t 10 preview/MAH01713_02:00.MP4
ffmpeg -ss 00:00:58.0 -i orig/MAH01712.MP4 -c copy -t 10 preview/MAH01712_00:58.MP4
ffmpeg -ss 00:01:25.0 -i orig/MAH01712.MP4 -c copy -t 10 preview/MAH01712_01:25.MP4
like image 969
Eike Avatar asked Sep 17 '18 19:09

Eike


1 Answers

ffmpeg reads from standard input, consuming data from $1 that was intended for the read command at the top of the loop. Redirect its standard input from /dev/null:

while IFS="#" read file timestamps; do
  filename="$file.MP4"
  for time in $timestamps; do
    ffmpeg -ss 00:${time}.0 -i "orig/${filename}" \
           -c copy -t 10 "preview/${file}_${time}.MP4" < /dev/null
  done
done < "$1"

echo does not read from standard input, which is why your modification made it appear to be working correctly.

like image 113
chepner Avatar answered Oct 08 '22 21:10

chepner