![Bash 脚本的问题](https://linux22.com/image/1178545/Bash%20%E8%84%9A%E6%9C%AC%E7%9A%84%E9%97%AE%E9%A2%98.png)
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
me= $( ffmpeg -i ${LINE} 2>&1 | grep Duration | sed 's/Duration: \(.*\), start/\1/g' > uber.txt )
echo "${me}"
done < me.txt
我想编写一个脚本来检查所有 .mp4 文件(位于 me.txt 中)的持续时间。它确实有效,但我不知道为什么它不显示持续时间,它只打印空行
答案1
0 - 我做了一些非常接近你想要的事情。
看 :https://askubuntu.com/a/1163082/77093
1 - 删除文件名中的空格
IFS=$'\n'
while read -r LINE || [ -n "${LINE}" ]; do
# ...
done < me.txt
2 - 你最好使用mediainfo
程序以毫秒为单位输出视频文件的持续时间。
sudo apt install mediainfo
然后
IFS=$'\n'
while read -r Line || [ -n "${Line}" ]; do
# Test if file is video
buff=$(file -N -i "${Line}" | grep -E 'video')
if [ ! -z "$buff" ]
then
MediaDuration=$(mediainfo --Output='General;%Duration%' "${Line}")
# Want total duration of listed files?
TotalDuration=$((TotalDuration + MediaDuration))
# Want total number of video files?
NbMedia=$((NbMedia + 1))
fi
done < me.txt
# Format Duration: milliseconds to H:M:S
Seconds=$((TotalDuration / 1000))
FormattedDuration=$(printf '%02dh:%02dm:%02ds\n' $(($Seconds/3600)) $(($Seconds%3600/60)) $(($Seconds%60)))
# Build report
ReportText="${NbFiles} File"
test $NbFiles -gt 1 && ReportText="${ReportText}s"
ReportText="${ReportText} selected\n"
test $NbMedia -gt 0 && ReportText="${ReportText}${NbMedia} Media file" || ReportText="${ReportText}No media file"
test $NbMedia -gt 1 && ReportText="${ReportText}s"
test $NbMedia -gt 0 && ReportText="${ReportText}\nTotal duration: ${FormattedDuration}"