【问题标题】:Bash Script to get info from ffprobe从 ffprobe 获取信息的 Bash 脚本
【发布时间】:2017-03-26 02:15:56
【问题描述】:

我正在尝试创建一个脚本来处理视频,但我希望从传入的文件中获取 bit_rate、宽度和高度信息,以便更好地调整输出。当我一次处理一个文件时,该脚本有效,但是当我突然将其放入循环时,我没有得到任何信息。

所以这行得通:

#!/bin/bash
eval $(ffprobe -v quiet -show_format -of flat=s=_ -show_entries stream=height,width,nb_frames,duration,codec_name input.mp4);
width=${streams_stream_0_width};
height=${streams_stream_0_height};
bitrate=$((${format_bit_rate}/1000));
echo $width,$height,$bitrate;

find ./ -type f -regex ".*\.\(mkv\|mp4\|wmv\|flv\|webm\|mov\|avi\)" -print0 | xargs -0 /root/newbatch.sh 执行时不会这样做

for i; do
eval '$(ffprobe -v quiet -show_format -of flat=s=_ -show_entries stream=height,width,nb_frames,duration,codec_name $i)';
width=${streams_stream_0_width};
height=${streams_stream_0_height};
bitrate=${format_bit_rate};
kbitrate=$((bitrate/1000));
echo $i,$width,$height,$kbitrate;

done

循环中bitrate 的数学运算也出现错误,但即使我将其注释掉,我仍然没有得到任何结果。由于它一次工作一个,我假设问题是 bash 脚本,与 ffmpeg / ffprobe 无关。

话虽如此,我可以这样做:

echo $i,$width,$height,$bitrate;

然后回来

./file1.mkv,,,
./file2.mkv,,,
./file3.mkv,,,
./file4.mkv,,,

所以它确实得到了一些信息,但它丢失了 eval 语句中的信息。

【问题讨论】:

  • 为什么需要知道输入的比特率?使用shellcheck.net 检查您的脚本。如果您仍然有问题,请显示您清理过的脚本。
  • 我正在对几千个视频进行编码,其中一些视频的分辨率和比特率比其他视频低得多,但是因为我想使用 nvenc_hvec,它的算法选择合适的比特率比libx265,我必须为每个文件指定它。我正在考虑使用一些if, else 逻辑来为输出选择合适的速率,这样我就不会意外地将传入的 400kbps 文件编码为 1,200kbps。
  • 我使用了 shellcheck.net,虽然我很欣赏这个推荐,但没有发现任何值得注意的地方,这个网站对我来说是新的。
  • 一般来说,我会很犹豫以这种方式使用eval。你基本上在你的 shell 中运行了数千个命令,这些命令是ffprobe 的输出结果。我认为一个快速的 Python 脚本或者 sedawk 会更适合这项任务。
  • 忽略我之前的评论,如果您删除 eval 命令的参数周围的单引号是否有效?

标签: bash ffmpeg


【解决方案1】:

我发现问题在于$i 变量中带有空格的文件。

此代码将所有命令组合到一个 shell 命令中,该命令在当前文件夹和所有子目录中搜索所有视频文件,循环使用一些选项来设置不同分辨率的比特率设置以及其他一些东西来修饰它。

#!/bin/bash
IFS=$'\n'   

# Reset
Color_Off='\033[0m'       # Text Reset

# Regular Colors
Black='\033[0;30m'        # Black
Red='\033[0;31m'          # Red
Green='\033[0;32m'        # Green
Yellow='\033[0;33m'       # Yellow
Blue='\033[0;34m'         # Blue
Purple='\033[0;35m'       # Purple
Cyan='\033[0;36m'         # Cyan
White='\033[0;37m'        # White

# Bold
BBlack='\033[1;30m'       # Black
BRed='\033[1;31m'         # Red
BGreen='\033[1;32m'       # Green
BYellow='\033[1;33m'      # Yellow
BBlue='\033[1;34m'        # Blue
BPurple='\033[1;35m'      # Purple
BCyan='\033[1;36m'        # Cyan
BWhite='\033[1;37m'       # White

# Underline
UBlack='\033[4;30m'       # Black
URed='\033[4;31m'         # Red
UGreen='\033[4;32m'       # Green
UYellow='\033[4;33m'      # Yellow
UBlue='\033[4;34m'        # Blue
UPurple='\033[4;35m'      # Purple
UCyan='\033[4;36m'        # Cyan
UWhite='\033[4;37m'       # White

# Background
On_Black='\033[40m'       # Black
On_Red='\033[41m'         # Red


VideoFiles=$(find ./ -maxdepth 10 -regex ".*\.\(mkv\|mp4\|wmv\|flv\|webm\|mov\|avi\|m4v\)")  

for i in $VideoFiles  
do  
  filename=$(basename "$i");
  extension="${filename##*.}";
  filename="${filename%.*}";
echo -e "${UGreen}############################################################################################################################################################################${Color_Off}\n";
echo -e "${Color_Off}Getting info of ${BCyan}$i${Color_Off}";

    eval $(ffprobe -v quiet -show_format -of flat=s=_ -show_entries stream=height,width,nb_frames,duration,codec_name -sexagesimal "$i");
    width=${streams_stream_0_width};
    height=${streams_stream_0_height};
    bitrate=${format_bit_rate};
    kbitrate=$((bitrate/1000));
    duration=${format_duration};
    #duration=$((durationSec/60));
    d=$(dirname "$i")
echo -e "${Color_Off}Duration = ${URed}$duration ${Color_Off}, Height/Width = ${URed}$height/$width ${Color_Off} Bitrate =${URed} $bitrate${Color_Off}\n";
echo -e "${UGreen}############################################################################################################################################################################${Color_Off}";
mkdir $d/Processed;

if ((1<= $height &&  $height<=400))
then    
    desired="200k";
    min="100k";
    max="800k";
    echo -e "This is a ${UPurple}LOW Quality${Color_Off} File\n.";

elif ((401<= $height &&  $height<=660))
then
    desired="500k";
    min="200k";
    max="1000k";
    echo -e "This is a ${UPurple}480p${Color_Off} File\n";
elif ((661<= $height &&  $height<=890))
then
    desired="800k";
    min="250k";
    max="1300k";
    echo -e "This is a ${UPurple}720p${Color_Off} File\n";

elif ((891<= $height &&  $height<=1200))
then
    desired="1200k";
    min="350k";
    max="2300k";
    echo -e "This is a ${UPurple}1080p${Color_Off} File\n";
else
    desired="1500k";
    min="550k";
    max="2700k";
    echo -e "This is a ${UPurple}HQ${Color_Off} File\n";
fi
    ffmpeg -n -i "$i" -c:v hevc_nvenc -hide_banner -preset fast -x265-params keyint=33:min-keyint=33 -crf 30 -b:v $desired -minrate $min -maxrate $max -bufsize 25M -c:a copy "$d/X265_$filename.mp4"
mv $i $d/Processed;
done  

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    相关资源
    最近更新 更多