I've been scratching my head over this for some time now; maybe one of you can help me.
I am trying to extract video frames from certain positions in a video. For that purpose, the locations are stored in a text file in the format 'hh:mm:ss' with one entry per line. A bash script reads those lines from the file and executes ffmpeg for each line.
The problem is that the input read from the text file changes depending on whether ffmpeg runs 'properly' or not. Without executing ffmpeg, running a different command such as ls, running ffmpeg with invalid settings (so that it immediately exits), everything works as indented. With ffmpeg, the file read from the time stamp file is invalid.
My best guess is that ffmpeg somehow messes with the environment the bash script runs in, however, OS design teaches us that this should be impossible.
Here is a minimal example and its output:
Script extract.sh
:
OUTDIR=samples
for f in test.txt; do # in reality a proper filter such as "f in video-*.txt"
BASE=${f%%.txt}
VIDEO=${BASE}.mkv
cd $OUTDIR
while read ss; do
echo "ffmpeg -i ../${VIDEO} -vsync 0 -ss ${ss} -t 00:00:01 '${BASE}-${ss}-%02d.png'"
(ffmpeg -i ../invalid.mkv -vsync 0 -ss ${ss} -t 00:00:01 "${BASE}-${ss}-%02d.png" 2>&1) >> ${BASE}.log
#(ffmpeg -i ../${VIDEO} -vsync 0 -ss ${ss} -t 00:00:01 "${BASE}-${ss}-%02d.png" 2>&1) >> ${BASE}.log
done < ../${f}
cd ..
done
Timestamp file test.txt
:
00:00:42
00:01:20
00:02:20
00:04:20
00:05:56
00:06:40
The first ffmpeg command in the script is an incorrect command (invalid video file). If activated, the input is read correctly from test.txt
:
$ ./extract.sh
ffmpeg -i ../test.mkv -vsync 0 -ss 00:00:30 -t 00:00:01 'test-00:00:30-%02d.png'
ffmpeg -i ../test.mkv -vsync 0 -ss 00:00:42 -t 00:00:01 'test-00:00:42-%02d.png'
ffmpeg -i ../test.mkv -vsync 0 -ss 00:01:20 -t 00:00:01 'test-00:01:20-%02d.png'
ffmpeg -i ../test.mkv -vsync 0 -ss 00:02:20 -t 00:00:01 'test-00:02:20-%02d.png'
ffmpeg -i ../test.mkv -vsync 0 -ss 00:04:20 -t 00:00:01 'test-00:04:20-%02d.png'
ffmpeg -i ../test.mkv -vsync 0 -ss 00:05:56 -t 00:00:01 'test-00:05:56-%02d.png'
ffmpeg -i ../test.mkv -vsync 0 -ss 00:06:40 -t 00:00:01 'test-00:06:40-%02d.png'
After disabling the first (incorrect) ffmpeg comand and enabling the second (correct) one, the timestamps read from test.txt
are incorrect:
$ ./extract.sh
ffmpeg -i ../test.mkv -vsync 0 -ss 00:00:30 -t 00:00:01 'test-00:00:30-%02d.png'
ffmpeg -i ../test.mkv -vsync 0 -ss -t 00:00:01 'test--%02d.png'
ffmpeg -i ../test.mkv -vsync 0 -ss 00:01:20 -t 00:00:01 'test-00:01:20-%02d.png'
ffmpeg -i ../test.mkv -vsync 0 -ss 05:56 -t 00:00:01 'test-05:56-%02d.png'
Any ideas?
ls -ld samples
. Specifically, is it a symbolic link? – Chris Davies Jul 22 '22 at 12:52