在bash中的while循环中读取文件会跳过THIRD行的前2个字符 [英] While loop in bash to read a file skips first 2 characters of THIRD Line

查看:42
本文介绍了在bash中的while循环中读取文件会跳过THIRD行的前2个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我正在处理的代码.文件sorted_results.txt的内容如下:

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

在读取文件时,它会跳过第三行的前两个字符,即将其作为:8:39 ,这会导致ffmpeg错误,并且脚本会停止.

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.

但是,当我仅打印变量$ INITIAL和$ LINE时,请注释 ffmpeg 命令,以正确打印值,即与文件内容相同.

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.

我认为ffmpeg命令以某种方式影响文件读取过程或变量值.但是我无法理解如何?

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

请帮助.

推荐答案

您的bash 读取内置命令和第二个ffmpeg命令(用于音频)都从STDIN读取,这就是它们相互干扰的原因.您也可以在此处指定 -nostdin 或使用另一个文件描述符(此处使用数字3)进行读取:

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"

这篇关于在bash中的while循环中读取文件会跳过THIRD行的前2个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆