#!/bin/bash


for file in `ls $1`
do
        echo "--------------------------------------------------------------"
        echo $file
        num=${file:0-5:1}
        num1=`expr $num + 1`
        num1=${num1:0-1:1}
        file=$1"/"$file
        file1=${file%$num.mp4}"$num1.mp4" # 更改视频名称
        # read -p "ss: " ss
        # read -p "to: " to
        `ffmpeg -i $file >1.log 2>&1`
        msg=`cat 1.log | grep Duration | grep -oE ":.*,"`
        h=`expr ${msg:2:2} + 0`
        m=`expr ${msg:5:2} + 0`
        s=`expr ${msg:8:2} + 0`
        time=$(($h*3600+$m*60+$s)) # 将转换为秒数
        if (($h > 0))&& (($h < 2)) # 判断h的值来对视频进行裁剪
        then
            time=$(($time*9/10))
        elif (($h > 1))&& (($h < 3))
        then
            time=$(($time*7/8))
        elif (($h > 2))
        then
            time=$(($time*4/5))
        else
            time=$(($time*19/20))
        fi
        # echo $time
        h=`printf "%02d" $(($time/3600))`
        time=$(($time%3600))
        m=`printf "%02d" $(($time/60))`
        s=`printf "%02d" $(($time%60))`
        to="$h:$m:$s"
        `ffmpeg -t $to -i $file -c copy $file1` # 裁剪视频
        `rm $file`
        # read -p "rm ${file}? [y/n]: " input
        # case $input in
        #         y|Y|"")
        #                 `rm $file`
        #                 ;;
        #         n|N)
        #                 echo "Nothing"
        #                 ;;
        #         *)
        #                 echo "ERR!!!"
        #                 exit 1
        #                 ;;
        # esac
done
