用于查找目录中所有 m4b 文件的第 2 章至第 6 章(x 章和 n 章)并将其拆分为脚本

用于查找目录中所有 m4b 文件的第 2 章至第 6 章(x 章和 n 章)并将其拆分为脚本

我正在尝试使用脚本来查找目录中所有 m4b 文件的第 2 章到第 6 章(x 和 n)之间的章节。

此代码在运行时将 m4b 文件拆分成章节bash m4b-split.sh *.m4b

#!/bin/bash
# used to find and split all m4b files in directory by chapter and convert them to mp3 files
# to run type "bash m4b-split.sh *.m4b"

# Chapter #0:0: start 0.000000, end 1290.013333
# first _ _ start _ end
currentdir=$(pwd) #get current directory

#: '
while [ $# -gt 0 ]; do
    for f in *.m4b # only look for m4b files
    do
        #fn=`echo "$1" | cut -d'.' -f1` #get just the filename no extension
        fn=$(basename "$f" | cut -d'.' -f1) #get just the filename no extension
        echo "($fn)"
        splitdirname="$currentdir/final-split-$fn" #sub directory with correct names
        mkdir -p "$splitdirname" #make split directory
        echo "$splitdirname/$chapter-$fn.mp3"
        ffmpeg -i "$1" 2> tmp.txt

        while read -r first _ _ start _ end; do
            if [[ $first = Chapter ]]; then
                read -r # discard line with Metadata:
                read -r _ _ chapter

                ffmpeg -vsync 2 -i "$1" -ss "${start%?}" -to "$end" -metadata title="$chapter $fn" -metadata track="$chapter" -vn -ar 22050 -ac 1 -ab 128 -f mp3 "$splitdirname/$chapter-$fn.mp3" </dev/null
            fi
        done <tmp.txt

        rm tmp.txt
        shift
    done
done
#'  

我尝试了下面的代码,但运行时bash m4b-split.sh *.m4b我得到了m4b-split.sh:第 25 行:((:片头字幕:表达式语法错误(错误标记是“Credits”)

更新 1:

看起来我需要将章节作为数字/数字而不是仅仅是章节来获取,因为我需要比较章节编号。 我如何获取章节编号并进行比较,以便仅将它们拆分为目录中所有 m4b 文件的 2 到 6(x 和 n)?

#!/bin/bash
# use a script to find and split the chapters between 2 and 6 (x and n) of all m4b files in directory
# to run type "bash m4b-split.sh *.m4b"

# Chapter #0:0: start 0.000000, end 1290.013333
# first _ _ start _ end
currentdir=$(pwd) #get current directory

while [ $# -gt 0 ]; do
    for f in *.m4b # only look for m4b files
    do
        fn=$(basename "$f" | cut -d'.' -f1) #get just the filename no extension
        echo "($fn)"
        splitdirname="$currentdir/final-split-$fn" #sub directory with correct names
        mkdir -p "$splitdirname" #make split directory
        echo "$splitdirname/$chapter-$fn.mp3"
        ffmpeg -i "$1" 2> tmp.txt

        while read -r first _ _ start _ end; do
            if [[ $first = Chapter ]]; then
                read -r # discard line with Metadata:
                read -r _ _ chapter

                # Only process chapters 2-6
                if ((chapter >= 2 && chapter <= 6)); then
                    ffmpeg -vsync 2 -i "$1" -ss "${start%?}" -to "$end" -metadata title="$chapter $fn" -metadata track="$chapter" -vn -ar 22050 -ac 1 -ab 128 -f mp3 "$splitdirname/$chapter-$fn.mp3" </dev/null
                fi

            fi
        done <tmp.txt

        rm tmp.txt
        shift
    done
done

相关内容