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

问题描述

在我的大型电影收藏中,我想搜索带有 DTS 编码的主(第一)音轨以转换为杜比的电影。

我的问题是我认为的第一首曲目。我当前的 bash 脚本将列出任何包含 DTS 曲目的电影,但没有指定哪个曲目。

    libusb_device*** list=NULL;
    libusb_context *context = NULL;
    libusb_init(&context);

        unsigned char serial[200];
        int status = libusb_get_string_descriptor_ascii(device,desc.iSerialNumber,serial,200);
        if(status < 0)
        {
            printf("Index: %d Error: %d\n",status);
        }else{
            printf("Themal camera serial: %s\n",serial);
        }

之后我想运行这个命令

#!/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

或者有没有办法将所有音轨向下移动并添加新的 AAC 音轨?

###进度

感谢@llogan,我已经微调了 bash 以找到所需的文件

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

现在深入研究命令,我想我可能有一个工作命令。有人发现问题吗?

#!/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

解决方法

好的,所以我微调了脚本以分离 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