Yaser Sakkaf
Yaser Sakkaf

Reputation: 153

While loop in bash to read a file skips first 2 characters of THIRD Line

#bin/bash
INPUT_DIR="$1"
INPUT_VIDEO="$2"
OUTPUT_PATH="$3"
SOURCE="$4"
DATE="$5"

INPUT="$INPUT_DIR/sorted_result.txt"
COUNT=1
initial=00:00:00
while IFS= read -r line; do
  OUT_DIR=$OUTPUT_PATH/$COUNT
  mkdir "$OUT_DIR"
  ffmpeg -nostdin -i $INPUT_VIDEO -vcodec h264 -vf fps=25 -ss $initial -to $line $OUT_DIR/$COUNT.avi
  ffmpeg -i $OUT_DIR/$COUNT.avi -acodec pcm_s16le -ar 16000 -ac 1 $OUT_DIR/$COUNT.wav
  python3.6 /home/Video_Audio_Chunks_1.py $OUT_DIR/$COUNT.wav
  python /home/transcribe.py  --decoder beam --cuda --source $SOURCE --date $DATE --video $OUT_DIR/$COUNT.avi --out_dir "$OUT_DIR"
  COUNT=$((COUNT + 1))
  echo "--------------------------------------------------"
  echo $initial
  echo $line
  echo "--------------------------------------------------"
  initial=$line
done < "$INPUT"

This is the code I am working on. The contents of file sorted_results.txt are as follows:

00:6:59
00:7:55
00:8:39
00:19:17
00:27:48
00:43:27

While reading the file it skips first two characters of the third line i.e. it takes it as :8:39 which results in the ffmpeg error and the script stops.

However when I only print the variables $INITIAL and $LINE, commenting the ffmpeg command the values are printed correctly i.e. same as the file contents.

I think the ffmpeg command is somehow affecting the file reading process or the variable value. BUT I CAN'T UNDERSTAND HOW?

PLEASE HELP.

Upvotes: 1

Views: 1017

Answers (1)

micha137
micha137

Reputation: 1285

Your bash read builtin command and the second ffmpeg command (for the audio) both read from STDIN, that is why they interfere with each other. You can either also specify -nostdin there or use another file descriptor (here number 3 is used) for read:

  while IFS= read -r -u 3 line; do
    ...
  done 3< "$INPUT"

Upvotes: 6

Related Questions