尝试更改 ffmpeg 脚本的文件扩展名

Trying to change extension of filename on an ffmpeg script

(第一次在这里发帖提问)

所以我正在寻找一个 ffmmpeg 脚本来自动将我的文件编码为 VP9。 我遇到的问题是当我尝试删除扩展名并添加一个新扩展名时。

例如 演示.mp4

应该改为 Demo.webm

我是 运行 这个 Ubuntu-16.04(服务器非 GI 版本) 我已经尝试了几种不同的方法来完成此操作(使用 google 和 Whosebug 上的其他帖子)但我似乎无法使其工作

这是我不断收到的错误..

第 31 行:Demo.mp4+.vp9:语法错误:无效算术运算符(错误标记为“.mp4+.vp9”)

我还评论了(在下面的代码中)语法错误指向的地方..

#!/bin/bash

# Welcome Message

clear
printf "====================================\n"
printf "FFMPEG Encoder\n"
printf "(Using HDR-4k Profile)\n"
printf "====================================\n\n"
printf " Loading Files in Current Directory...\n\n"
sleep 3s


# Variables

i=1
ext=".webm"
vadd=4000000
vsub=2000000


# Iterate through files in current directory

for j in *.{mp4,mkv};
do
echo "$i.$j"
file[i]=$j
i=$(( i + 1 ))
done


# Select File & Bitrate

printf "Enter file number\n"
read fselect
printf "${file[$fselect]}: Selected for encoding\n\n"

printf "Enter Average Bitrate (Eg: 8000000)\n\n"
read bselect


# ***THIS IS WHERE THE PROBLEM IS***
# Prepare output file, strip trailing extension (eg .mkv) and add .webm

ftemp1="${file[$fselect]}"
ftemp2="${ftemp1::-4}"
fout="$(($ftemp2+$ext))"
printf "Output file will be: $fout"
printf "Preparing to encode..."
sleep 5s


# Encode with User-Defined Parameters

ffmpeg -y -report -i ${file[$fselect]} -b:v $bselect -speed 4 -pass 1 \
  -pix_fmt yuv420p10le \
  -color_primaries 9 -color_trc 16 -colorspace 9 -color_range 1 \
  -maxrate "$(($bselect+$vadd))" -minrate "$(($bselect-$vsub))" \
  -profile:v 2 -vcodec libvpx-vp9 -f webm /dev/null && \
ffmpeg -y -report -i ${file[$fselect]} -b:v $bselect -pass 2 \
  -pix_fmt yuv420p10le \
  -color_primaries 9 -color_trc 16 -colorspace 9 -color_range 1 \
  -maxrate "$(($bselect+$vadd))" -minrate "$(($bselect-$vsub))" \
  -profile:v 2 -vcodec libvpx-vp9 \
$fout

我确信有一种更简洁的方法可以做到这一点 - 但我并不期待这方面的帮助:P

我怀疑我在尝试添加两种不同类型的变量?但我以为我将它们定义为字符串..我可能错了

请帮忙...哈哈

您正在尝试进行算术微积分 ($((...)))。但是你只需要连接两个字符串:

fout="$ftemp2$ext"

顺便说一句,您可以将此转换简化为一行三行:

fout="${file[$fselect]/%.mp4/$ext}"

这是一个正则表达式,其中 .mp4 字符串末尾(% 符号)被 $ext.

的内容替换