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

荒凉一梦 提交于 2021-02-08 08:14:11

问题


#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.


回答1:


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"


来源:https://stackoverflow.com/questions/51146890/while-loop-in-bash-to-read-a-file-skips-first-2-characters-of-third-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!