gpt4 book ai didi

Bash - 从 ffmpeg 获取时差 - 音频和视频合并

转载 作者:行者123 更新时间:2023-12-04 22:59:49 26 4
gpt4 key购买 nike

我正在使用 ffmpeg 构建一个应用程序。其中我需要减去两个文件的开始时间(只有两个文件作为输入)。以下是我的代码。

#!/bin/bash
mkvarray=()
mkvarray_name=()
for video_name in *.mkv; do
output=$(ffprobe -v error -show_entries format=start_time -of default=noprint_wrappers=1:nokey=1 "$video_name")
mkvarray+=($output)
mkvarray_name+=("$video_name")
done

arraylength=${#mkvarray[@]}
for (( i=0; i<${arraylength}; i++ ));
do
echo ${mkvarray_name[$i-1]} "------> " ${mkvarray[$i-1]}
done

max_number=0
if [[ ${mkvarray[0]} < ${mkvarray[1]} ]]; then
echo "first-------------"

max_number = ${mkvarray[0]} - ${mkvarray[1]}
echo "first " $max_number
else
echo "second-------------"
max_number = ${mkvarray[0]} - ${mkvarray[1]}
echo "second " $max_number
fi

echo $max_number

输出

$ ./ffmpeg_updated.bash RTda01986c816052106c00f417387a83ff.mkv ------> 2.040000 cc.mkv ------> 2.040000 RT4bafb05e1b36885c75d7d67a16f5e3b2.mkv ------> 4.086000 first------------- ./ffmpeg_updated.bash: line 24: max_number: command not found first 0 0



来自 this我明白,浮点运算是不可能的。那么有什么建议可以从这个开始。我对 bash 很陌生。或者有没有其他方法可以在 ffmpeg 中完成任务?

最佳答案

更新
可以在以下链接中找到更好的答案
Merge Conference Video and Audio call output using hstack ffmpeg

如链接问题中所述。无法直接通过 bash 进行浮点运算。我们必须使用 bc。
更新的代码如下。

VIDEO MERGER


    #!/bin/bash
echo "VIDEO MERGER"
echo "------------"
echo "VIDEO MERGER: started"
mkvarray=()
mkvarray_name=()
#ASSIGN VIDEO START TIME TO VARIBALES
for video_name in *.mkv; do
start_time=$(ffprobe -v error -show_entries format=start_time -of default=noprint_wrappers=1:nokey=1 "$video_name")
mkvarray+=($start_time)
mkvarray_name+=("$video_name")
done
#PRINT START TIME OF VIDEO FILES (FOR DEBUG)
#arraylength=${#mkvarray[@]}
#for (( i=0; i<${arraylength}; i++ ));
#do
# echo $i ${mkvarray_name[$i]} "------> " ${mkvarray[$i]}
#done

#echo "----->" ${mkvarray_name[0]} ${mkvarray[0]}
#echo "----->" ${mkvarray_name[1]} ${mkvarray[1]}

#GETTING THE START TIME
RESULT=$(echo "scale=5; (${mkvarray[0]} - ${mkvarray[1]})" | tr -d $'\r' | bc -l)
updated_result=0
#check which file is
if (( $(echo "$RESULT < 0" |bc -l) )); then
updated_result=$(echo "$RESULT * -1" |bc -l)
else
updated_result=$RESULT
fi

#IDENTIFY THE FIRST FILE AND SECOND FILE IN ORDER OF START TIME
if (( $(echo "(${mkvarray[0]} > ${mkvarray[1]})" | tr -d $'\r' |bc -l) )); then #do
second_file=${mkvarray_name[0]}
first_file=${mkvarray_name[1]}
echo "FIRST FILE : " ${mkvarray_name[1]} ", TIME :" ${mkvarray[1]}
echo "SECOND FILE : " ${mkvarray_name[0]} ", TIME :" ${mkvarray[0]}
else
second_file=${mkvarray_name[1]}
first_file=${mkvarray_name[0]}
echo "FIRST FILE : " ${mkvarray_name[0]} ", TIME :" ${mkvarray[0]}
echo "SECOND FILE : " ${mkvarray_name[1]} ", TIME :" ${mkvarray[1]}
fi
echo "TIME DIFERENCE :" $updated_result

ffmpeg -i $first_file -itsoffset $updated_result -i $second_file -filter_complex "[0]pad=iw*2:ih[int]; [int][1]overlay=W/2:0[vid]" -map [vid] -c:v libvpx -crf 23 -quality good -cpu-used 3 -an output_video.webm
echo "VIDEO MERGER: finished"

 AUDIO MERGER

#!/bin/bash
echo "AUDIO MERGER"
echo "------------"
echo "AUDIO MERGER : started"
mkvarray=()
mkvarray_name=()
#ASSIGN VIDEO START TIME TO VARIBALES
for video_name in *.mka; do
start_time=$(ffprobe -v error -show_entries format=start_time -of default=noprint_wrappers=1:nokey=1 "$video_name")
mkvarray+=($start_time)
mkvarray_name+=("$video_name")
done
#PRINT START TIME OF VIDEO FILES (FOR DEBUG)
#arraylength=${#mkvarray[@]}
#for (( i=0; i<${arraylength}; i++ ));
#do
# echo $i ${mkvarray_name[$i]} "------> " ${mkvarray[$i]}
#done

#echo "----->" ${mkvarray_name[0]} ${mkvarray[0]}
#echo "----->" ${mkvarray_name[1]} ${mkvarray[1]}

#GETTING THE START TIME
RESULT=$(echo "scale=5; (${mkvarray[0]} - ${mkvarray[1]})" | tr -d $'\r' | bc -l)
updated_result=0
#check which file is
if (( $(echo "$RESULT < 0" |bc -l) )); then
updated_result=$(echo "$RESULT * -1 * 1000" |bc -l)
else
updated_result=$(echo "$RESULT * 1000" |bc -l)
fi

#IDENTIFY THE FIRST FILE AND SECOND FILE IN ORDER OF START TIME
if (( $(echo "(${mkvarray[0]} > ${mkvarray[1]})" | tr -d $'\r' |bc -l) )); then #do
second_file=${mkvarray_name[0]}
first_file=${mkvarray_name[1]}
echo "FIRST FILE : " ${mkvarray_name[1]} ", TIME :" ${mkvarray[1]}
echo "SECOND FILE : " ${mkvarray_name[0]} ", TIME :" ${mkvarray[0]}
else
second_file=${mkvarray_name[1]}
first_file=${mkvarray_name[0]}
echo "FIRST FILE : " ${mkvarray_name[0]} ", TIME :" ${mkvarray[0]}
echo "SECOND FILE : " ${mkvarray_name[1]} ", TIME :" ${mkvarray[1]}
fi
echo "TIME DIFERENCE :" $updated_result

ffmpeg -i $first_file -i $second_file -filter_complex "[1]adelay="$updated_result"|"$updated_result"[d];[0][d]amix=inputs=2" -strict -2 output_audio.webm
echo "AUDIO MERGER : finished"

MERGE AUDIO & VIDEO

#!/bin/bash
echo "AUDIO/VIDEO OUTPUT MERGER"
echo "-------------------------"
echo "AUDIO/VIDEO OUTPUT MERGER : started merging audio and video"
ffmpeg -i output_video.webm -i output_audio.webm -c:v copy -c:a copy full.webm
echo "AUDIO/VIDEO OUTPUT MERGER : finished merging audio and video"

关于Bash - 从 ffmpeg 获取时差 - 音频和视频合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46528298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com