并行和转码视频的问题

时间:2014-02-24 17:16:15

标签: bash transcoding

嘿伙计们,即使set -x没有给我任何明显的信息,我也会收到一个奇怪的错误。看起来我的cmds实际上并没有被并行处理,或者至少这是假设。下面的脚本应该执行以下操作

  • 找到所有内容
  • 将它们全部推过开关以确保不会重写相同的格式
  • 将它们重新编码为html5视频格式(webm ogg和mp4,通过h264)
  • 移动这些文件

有人可以简单地运行此代码并让我知道发生了什么吗?我确信这很简单,但是它正在逃避我...

#!/bin/bash
# Video transcoder

# CPR : Jd Daniel
# MOD : 2014-02-20 @ 10:51:25
# VER : Beta 1

# proto: cd drop ; types=(asf asx avi flv m4v mov mp4 mpg rm swf vob wmv); for i in "${types[@]}"; do touch "video.${i}"; done;

inp="drop"
out="export"

## test for reqs of exit
for requires in ffmpeg parallel; do
    hash $requires 2>/dev/null || { 
        echo >&2 "I require $requires to run but it's not installed.  Aborting."; exit 1; 
    }
done


## dectypes
declare -r fpath=$( cd "$(dirname "$0")" ; pwd -P )
declare -r types="asf\|asx\|avi\|flv\|m4v\|mov\|mp4\|mpg\|ogg\|rm\|swf\|vob\|webm\|wmv"
declare -r allow=( mp4 ogg webm )
##

    for dir in "$inp" "$out"; do
        [ -d "$fpath/$dir" ] || { mkdir -p "$fpath/$dir"; } # create if not available
    done

        [ -z "$(ls $fpath/$inp)" ] && { "$fpath is empty, finished..."; exit 0; } # exit when empty


    ## start video conversion
    cd $fpath ; for video in $(find . -type f -iregex ".*\(${types}\)" -printf '%P\0 '); do

        skip=0 #reset skip flag 
        name=$(echo $video |awk -NF '.' '{print $1}')
        exts=$(echo $video |awk -NF '.' '{print $2}')

        # assign predetermined pos
        for (( i = 0; i < ${#allow[@]}; i++ )); do
           if [ "${allow[$i]}" = "${exts}" ]; then
               skip=$(($i + 1));
           fi
        done

        cmds=() # empty
        [[ $skip = 1 ]] || { cmds+=("'$video' -b 1500k -vcodec libx264   -vpre   slow      -vpre baseline -g 30          '$name.mp4' ") ; }
        [[ $skip = 2 ]] || { cmds+=("'$video' -b 1500k -vcodec libvpx    -acodec libvorbis -ab   160000   -f webm -g 30  '$name.webm'") ; }
        [[ $skip = 3 ]] || { cmds+=("'$video' -b 1500k -vcodec libtheora -acodec libvorbis -ab   160000   -g 30          '$name.ogg' ") ; }

        echo "${cmds[@]}" | parallel --gnu -j10 ffmpeg -i $fpath/$inp/{/}\;

        for ext in "${allow[@]}"; do
            [ -a "${fpath}/${inp}/${name}.{ext}" ] && mv "${fpath}/${inp}/${name}.{ext}" "${fpath}/${out}/"
        done

    done

可以在transcoder找到指向回购的链接。请随意在你自己的项目上使用!!!!谁知道它如何派上用场,但我很乐意看到它分享;)

1 个答案:

答案 0 :(得分:2)

结束于此,不再使用并行但仍然执行后台任务

#!/bin/bash
# Video transcoder

# CPR : Jd Daniel :: Ehime-ken
# MOD : 2014-02-24 @ 16:34:29
# VER : Beta 3


inp="drop"
out="export"

clear # set -x

## test for reqs of exit
for requires in ffmpeg parallel; do
    hash $requires 2>/dev/null || {
        echo >&2 "I require $requires to run but it's not installed.  Aborting."; exit 1;
    }
done


## dectypes
declare -r fpath=$( cd "$(dirname "$0")" ; pwd -P )
declare -r types="asf\|asx\|avi\|flv\|m4v\|mkv\|mov\|mp4\|mpg\|ogg\|rm\|swf\|vob\|webm\|wmv"
declare -r allow=( mp4 ogg webm )
##

    for dir in "$inp" "$out"; do
        [ -d "$fpath/$dir" ] || { mkdir -p "$fpath/$dir"; } # create if not available
    done

    [ -z "$(ls $fpath/$inp)" ] && { "$fpath is empty, finished..."; exit 0; } # exit when empty


    ## start video conversion
    cd $fpath ; while IFS= read -r video; do

        skip=0 #reset skip flag
        name="${video%.*}"
        exts="${video##*.}"

        # assign predetermined pos
        for (( i = 0; i < ${#allow[@]}; i++ )); do
           if [ "${allow[$i]}" = "${exts}" ]; then
               skip=$(($i + 1));
           fi
        done


        pids=() # pid track if we want to use it later for something
        [[ $skip = 1 ]] || { ffmpeg -y -i "$inp/$video" -vb 1500k -vcodec libx264 -vpre slow -vpre baseline -g 30 "$out/${name}.mp4" 2> /dev/null & } # \n pid+="$! " # if we're going to do post proc kill work later
        [[ $skip = 2 ]] || { ffmpeg -y -i "$inp/$video" -vb 1500k -vcodec libvpx -acodec libvorbis -ab 160000 -f webm -g 30 "$out/${name}.webm" 2> /dev/null & }
        [[ $skip = 3 ]] || { ffmpeg -y -i "$inp/$video" -vb 1500k -vcodec libtheora -acodec libvorbis -ab 160000 -g 30 "$out/${name}.ogg" 2> /dev/null & }


        # move out of path
        for ext in "${allow[@]}"; do
            [ -a "$inp/$name.$ext" ] && cp -f "$inp/$name.$ext" "$out/" &
        done

        for job in `jobs -p`; do
            echo "Job: $job"
            wait $job || let "FAIL+=1"
        done

        [ "0" == "$FAIL" ] && echo "YAY!" || echo "FAIL! ($FAIL)"

    done <<< $(find "$fpath/$inp" -type f -iregex ".*\(${types}\)" -printf '%P\0 ')