用于递归查找和转换电影的 Bash 脚本

标签 bash ffmpeg aac dts

在我的大型电影收藏中,我想搜索带有 DTS 编码的主要(第一)音轨要转换为杜比的电影。
我的问题将是我认为的第一首轨道。我当前的 bash 脚本将列出任何包含 DTS 轨道的电影,但没有指定哪个轨道。

#!/bin/bash
# My message to create DTS list
find /home/Movies -name '*.mkv' | while read f
do
if mediainfo "$f" | grep A_DTS; then
echo $f 
fi
done
之后我想运行这个命令
ffmpeg -i $f -map 0:v -map 0:a:0 -map 0:a -map 0:s -c:v copy -c:a copy -c:s copy -c:a:0 ac3 -b:a:0 640k $f
或者有没有办法将所有音轨向下移动并添加新的 AAC 音轨?
###进步
感谢@llogan,我已经微调了 bash 以找到所需的文件。
#!/bin/bash
# My DTS conversion script
# credits to llogan

find /Mymovies -name '*.mkv' | while read f
do
 if ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of csv=p=0 "$f" | grep dts; then
 echo "$f"
fi
done
现在深入研究命令,我想我可能有一个工作命令。有人发现问题吗?
ffmpeg -i $f
      -map 0:v -c:v copy
      -map 0:a:0? -c:a:0 ac3
      -map 0:a:0? -c:a:1 copy
      -map 0:a:1? -c:a:2 copy
      -map 0:a:2? -c:a:3 copy
      -map 0:a:3? -c:a:4 copy
      -map 0:a:4? -c:a:5 copy
      -map 0:a:5? -c:a:6 copy
      -map 0:a:6? -c:a:7 copy
      -map 0:a:7? -c:a:8 copy
      -map 0:a:8? -c:a:9 copy
      -map 0:s? -c copy
      -b:a:0 640k
/tmp/output.mkv
mv $f /home/DTS_BACKUP/
mv /tmp/output.mkv $f
rm /tmp/output.mkv
所以最终结果看起来像:
#!/bin/bash
# My DTS conversion script
# credits to llogan
find /Mymovies -name '*.mkv' | while read f
do
 if ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of csv=p=0 "$f" | grep dts; then

ffmpeg -i $f
          -map 0:v -c:v copy
          -map 0:a:0? -c:a:0 ac3
          -map 0:a:0? -c:a:1 copy
          -map 0:a:1? -c:a:2 copy
          -map 0:a:2? -c:a:3 copy
          -map 0:a:3? -c:a:4 copy
          -map 0:a:4? -c:a:5 copy
          -map 0:a:5? -c:a:6 copy
          -map 0:a:6? -c:a:7 copy
          -map 0:a:7? -c:a:8 copy
          -map 0:a:8? -c:a:9 copy
          -map 0:s? -c copy
          -b:a:0 640k
/tmp/output.mkv
mv $f /home/DTS_BACKUP/
mv /tmp/output.mkv $f
rm /tmp/output.mkv

fi
done

最佳答案

好的,所以我微调了脚本以分离 dts 和 dts-hd。我得出的结论是不需要的,因为我无法将 dts-hd 解码为 e-ac3,也可以将其编码为 ac3。但我在 bash 中玩得很开心。
当前 bash :

#!/bin/bash
# My DTS conversion script
# credits to llogan

find /MyMovies -name '*.mkv' | while read f
do

function codec_profile {
ffprobe -v error -select_streams a:0 -show_entries stream=$1 -of csv=p=0 "$f"
}

#first check for audio format
if [ "$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of csv=p=0 "$f")" = "dts" ]; then
    if [ "$(codec_profile "profile")" = "DTS" ]; then
      echo "$f" >> dts.txt
      codec_profile "profile" >> dts.txt
      codec_profile "channels" >> dts.txt
      if [ "$(codec_profile "channels")" -gt 5 ]; then
        echo "check" >> dts.txt; else
        echo "stereo" >> dts.txt;
      fi
    else [ "$(ffprobe -v error -select_streams a:0 -show_entries stream=profile -of csv=p=0 "$f")" = "DTS-HD MA" ]; 
      echo "$f" >> dts-hd.txt
      codec_profile "profile" >> dts-hd.txt
      codec_profile "channels" >> dts-hd.txt    
    fi
fi
done
我检查了创建的txt文件,结果是spot op。我还测试了@llogan 给我的命令并且效果很好。
ffmpeg -i "$f" -map 0 -map -0:a -map 0:a:0 -map 0:a -c copy -c:a:0 ac3 -b:a:0 640k /tmp/output.mkv
最后要弄清楚的是如何检查退出代码并用这个命令替换文本文件的创建
想法:
ffmpeg -i "$f" -map 0 -map -0:a -map 0:a:0 -map 0:a -c copy -c:a:0 ac3 -b:a:0 640k /tmp/output.mkv
RC=$?
if [ "${RC}" -ne "0" ]; then
    # list error in txt file and move on to next
else
# mv output file to overwrite original file
fi

关于用于递归查找和转换电影的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65617649/

相关文章:

audio - 如果缺少片段,如何播放m4a/aac音频文件?

linux - 在 awk 命令中使用 pstree

FFMPEG (libx264) "height not divisible by 2"

c - 终端背景颜色并不总是使用 "\033[0m"正确重置

android - 为 android 构建 ffmpeg 时出错

opencv - 用 FFMPEG 将视频与标量相乘

android - Android 可以播放 ADTS AAC 文件吗?

android - 在 Android 上流式传输 AAC

linux - 如何在特定条件下更改特定字段的值

macos - 我如何使用 bash 和 xmllint 解析 adobe xml swidtag