【问题标题】:bash script to select multiple file formats at once for encode processbash脚本一次选择多种文件格式进行编码过程
【发布时间】:2014-01-28 11:13:44
【问题描述】:

我下载了一个 BASH 脚本(可以使用),它可以使用 handbrake-CLI 进行一些视频转换,但目前它只允许一次从单一文件格式 avi 转换为 mkv。我希望能够让它一次转换任何类型的输入文件(avi、wmv、flv...到 mkv),而不是每次都为每种输入格式更改脚本。如何调整 bash 脚本以允许这样做?

那么我怎样才能让这个带有 input_file_type="avi" 行的 bash 脚本与 input_file_type="avi,wmv,flv,mp4"

PS:我尝试发布 bash 脚本,但是如果有人知道如何以正确的格式将 bash 脚本发布到论坛,那么格式就会变得一团糟,让我知道我会在这里发布而不是下面的链接

[http://pastebin.com/hzyxnsYY][1]

在此处粘贴代码:

#!/bin/sh

###############################################################################
#
# Script to recursively search a directory and batch convert all files of a given
# file type into another file type via HandBrake conversion.
#
# To run in your environment set the variables:
#   hbcli - Path to your HandBrakeCLI
#
#   source_dir - Starting directory for recursive search
#
#   input_file_type - Input file type to search for
#
#   output_file_type  - Output file type to convert into
#
#
# Change log:
# 2014-01-27: Initial release.  Tested on ubuntu 13.10.
#
###############################################################################

hbcli=HandBrakeCLI
source_dir="/media/rt/1tera_ext/1_Video_Stuff/1 Documentary"
#source_dir="/media/rt/1tera_ext/1_Video_Stuff/1 Nova and bbc/Carbon diamonds"



input_file_type="avi"
output_file_type="mkv"

echo "# Using HandBrakeCLI at "$hbcli
echo "# Using source directory " "$source_dir"
echo "# Converting "$input_file_type" to "$output_file_type

# Convert from one file to another
convert() {
        # The beginning part, echo "" | , is really important.  Without that, HandBrake exits the while loop.
        #echo "" | $hbcli -i "$1" -o "$2" --preset="Universal";
        echo "" | $hbcli -i "$1" -t 1 --angle 1 -c 1 -o "$2"  -f mkv  --decomb --loose-anamorphic  --modulus 2 -e x264 -q 20 --cfr -a 1,1 -E faac,copy:ac3 -6 dpl2,auto -R Auto,Auto -B 160,0 -D 0,0 --gain 0,0 --audio-fallback ffac3 --x264-profile=high  --h264-level="4.1"  --verbose=1
        #echo "" | $hbcli -i "$1" -t 1 --angle 1 -c 1 -o "$2"  -f mkv  --decomb -w 640 --loose-anamorphic  --modulus 2 -e x264 -q 20 --cfr -a 1,1 -E faac,copy:ac3 -6 dpl2,auto -R Auto,Auto -B 160,0 -D 0,0 --gain 0,0 --audio-fallback ffac3 --x264-profile=high  --h264-level="4.1"  --verbose=1

}

# Find the files and pipe the results into the read command.  The read command properly handles spaces in directories and files names.
find "$source_dir" -name *.$input_file_type | while read in_file
do
        echo "Processing…"
        echo ">Input  "$in_file

        # Replace the file type
        out_file=$(echo $in_file|sed "s/\(.*\.\)$input_file_type/\1$output_file_type/g")
        echo ">Output "$out_file

        # Convert the file
        convert "$in_file" "$out_file"

        if [ $? != 0 ]
        then
            echo "$in_file had problems" >> handbrake-errors.log
        fi

        echo ">Finished "$out_file "\n\n"
done

echo "DONE CONVERTING FILES"

【问题讨论】:

    标签: linux bash shell sh


    【解决方案1】:

    假设所有文件类型的转换命令都相同,您可以像这样使用单个find

    find "$source_dir" -type f -regex ".*\.\(avi\|wmv\|flv\|mp4\)" -print0 | while IFS= read -r -d $'\0' in_file
    do
    
    done
    

    或者,创建一个您感兴趣的文件类型数组并遍历它们:

    input_file_types=(avi wmv flv mp4)
    
    # loop over the types and convert
    for input_file_type in "${input_file_types[@]}"
    do
        find "$source_dir" -name "*.$input_file_type" -print0 | while IFS= read -r -d $'\0' in_file
        do
    
        done
    done
    

    为了正确处理包含空格和换行符的文件名,您应该使用空分隔输出。这就是 -print0read -d $'\0' 的用途。

    【讨论】:

    • 我得到一个语法错误:"(" 在行中时出现意外 input_file_types=(avi wmv flv mp4) 这些应该用引号代替吗?
    • 不,您不需要引号。您可能没有使用bash(或使用旧版本),因此不支持数组。
    【解决方案2】:

    您可以在find 车道中使用 OR(-o) 运算符。

    例如

    find "$source_dir" -name *.avi -o -name *wmv -o -name *.flv -o -name *.mp4 | while read in_file
    

    【讨论】:

      【解决方案3】:

      如果通过 bash 运行:

      #!/usr/bin/bash
      
      input_file_type="avi|wmv|flv|mp4"
      
      find "$source_dir" -type f|egrep "$input_file_type" | while read in_file
      do
              echo "Processing…"
              echo ">Input  "$in_file
      
              # Replace the file type
              out_file=$(in_file%.*}.${output_file_type}   # replace file type with different command. 
              echo ">Output "$out_file
      
              # Convert the file
              convert "$in_file" "$out_file"
      
              if [ $? != 0 ]
              then
                  echo "$in_file had problems" >> handbrake-errors.log
              fi
      
              echo ">Finished "$out_file "\n\n"
      done
      

      【讨论】:

        【解决方案4】:

        这是可能对其他人有所帮助的最终代码,对于链接我不知道如何在此处粘贴 bash 脚本而不会弄乱格式感到抱歉

        Link to the final code

        #!/bin/bash
        
        ###############################################################################
        #execute using bash mkvconv.sh
        
        # Script to recursively search a directory and batch convert all files of a given
        # file type into another file type via HandBrake conversion.
        #
        # To run in your environment set the variables:
        #   hbcli - Path to your HandBrakeCLI
        #
        #   source_dir - Starting directory for recursive search
        #
        #   input_file_types - Input file types to search for
        #
        #   output_file_type  - Output file type to convert into
        #
        #
        # Change log:
        # 2014-01-27: Initial release.  Tested on ubuntu 13.10.
        #http://stackoverflow.com/questions/21404059/bash-script-to-select-multiple-file-formats-at-once-for-encode-process/21404530#21404530
        ###############################################################################
        
        hbcli=HandBrakeCLI
        source_dir="/media/rt/1tera_ext/1_Video_Stuff/1 Nova and bbc/Carbon diamonds"
        
        
        input_file_types=(avi wmv flv mp4 webm mov mpg)
        output_file_type="mkv"
        
        echo "# Using HandBrakeCLI at "$hbcli
        echo "# Using source directory " "$source_dir"
        echo "# Converting "$input_file_types" to "$output_file_type
        
        # Convert from one file to another
        convert() {
                # The beginning part, echo "" | , is really important.  Without that, HandBrake exits the while loop.
                #echo "" | $hbcli -i "$1" -o "$2" --preset="Universal"; # dont use with preses things are left out
                echo "" | $hbcli -i "$1" -t 1 --angle 1 -c 1 -o "$2"  -f mkv  --decomb --loose-anamorphic  --modulus 2 -e x264 -q 20 --cfr -a 1,1 -E faac,copy:ac3 -6 dpl2,auto -R Auto,Auto -B 160,0 -D 0,0 --gain 0,0 --audio-fallback ffac3 --x264-profile=high  --h264-level="4.1"  --verbose=1
        
        }
        # loop over the types and convert
        for input_file_types in "${input_file_types[@]}"
        do
        
                # Find the files and pipe the results into the read command.  The read command properly handles spaces in directories and files names.
                #find "$source_dir" -name *.$input_file_type | while read in_file
                find "$source_dir" -name "*.$input_file_types" -print0 | while IFS= read -r -d $'\0' in_file
                #In order to correctly handle filenames containing whitespace and newline characters, you should use null delimited output. That's what the -print0 and read -d $'\0' is for.
                do
                        echo "Processing…"
                        echo ">Input  "$in_file
        
                        # Replace the file type
                        out_file=$(echo $in_file|sed "s/\(.*\.\)$input_file_types/\1$output_file_type/g")
                        echo ">Output "$out_file
        
                        # Convert the file
                        convert "$in_file" "$out_file"
        
                        if [ $? != 0 ]
                        then
                            echo "$in_file had problems" >> handbrake-errors.log
                        fi
        
                        echo ">Finished "$out_file "\n\n"
                done
        done
        echo "DONE CONVERTING FILES"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-03
          • 1970-01-01
          • 1970-01-01
          • 2014-12-22
          相关资源
          最近更新 更多