sh 使用HandbrakeCLI递归转换所有文件并保存到新文件夹。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 使用HandbrakeCLI递归转换所有文件并保存到新文件夹。相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#
# Change this to specify a different handbrake preset. You can list them by running: "HandBrakeCLI --preset-list"
#
#set -ux
set +e
PRESET="Very Fast 720p30" # or Fast 720p30 or Fast 1080p30
if [ -z "$1" ] ; then
    TRANSCODEDIR="."
else
    TRANSCODEDIR="$1"
fi
BURN_SUBS=${2:-"false"}

abs_basefolder=`realpath "$TRANSCODEDIR"`
folder_base=`dirname "$abs_basefolder"`
folder_name=`basename "$abs_basefolder"`
output_basedirectory="$folder_base/$folder_name-converted"
mkdir -p "$output_basedirectory"


function recursive_convert {
    find "$1" -type d -print0 |
    while IFS= read -rd '' directory_to_convert; do 
        echo "Converting files in $directory_to_convert"; 
        output_directory=$output_basedirectory
        if [ "$directory_to_convert" != "$1" ]; then           
            abs_subfolder=`realpath "$directory_to_convert"`            
            rel_subfolder=${abs_subfolder#$abs_basefolder/}            
            #Create output directory
            output_subdirectory="$output_basedirectory/$rel_subfolder"
            mkdir -p "$output_subdirectory"             
            output_directory=$output_subdirectory       
        fi       
        echo "Converted files will be saved in $output_directory"; 
        # TODO only find video files
        # *.mkv *.webm *.flv *.avi *.mov *.wmv *.mp4 *.mp4$ *.m4v *.flv
        find "$directory_to_convert" -maxdepth 1 -mindepth 1 -type f -print0 | 
        while IFS= read -r -d '' file_to_convert; do
            file_to_convert_filename=$(basename "$file_to_convert")
            if [[ ${file_to_convert_filename: -4} == ".mp4" ]] || [[ ${file_to_convert_filename: -4} == ".mkv" ]]; then
                converted_filepath="$output_directory/${file_to_convert_filename%.*}.m4v"
                subtitle_srt="$abs_basefolder/${file_to_convert_filename%.*}.srt" 
                # If the file doesn't exist then convert
                if [ ! -e "$converted_filepath" ]; then
                    
                    if [ -e "$subtitle_srt" ] && [ "$BURN_SUBS" = "true" ]; then
                        echo "Converting $file_to_convert with hardcoded subs"
                        HandBrakeCLI --input "$file_to_convert" --output "$converted_filepath" --preset="$PRESET" --srt-file "$subtitle_srt" --srt-codeset UTF-8 --srt-burn </dev/null
                    else
                        echo "Converting $file_to_convert"
                        HandBrakeCLI --input "$file_to_convert" --output "$converted_filepath" --preset="$PRESET" </dev/null
                    fi
                else
                    echo "$converted_filepath already exists"
                fi
            fi
        done
    done
}

recursive_convert "$abs_basefolder"

以上是关于sh 使用HandbrakeCLI递归转换所有文件并保存到新文件夹。的主要内容,如果未能解决你的问题,请参考以下文章

sh 将m4a文件递归转换为mp3

sh 递归删除所有文件的尾部空格

sh 递归删除目录中的所有node_modules文件夹

sh 在Mac上递归删除所有node_modules文件夹

sh RECURSIVELY Bash将所有html转换为markdown文件(使用Pandoc)

sh Bash脚本使用Applescript将目录中的所有html文件转换为docx文件。它还将标题和普通字体转换为