Skip to content

Instantly share code, notes, and snippets.

@Soolthub
Forked from BenjaminPoncet/ffmpeg-wrapper
Created July 1, 2022 07:03
Show Gist options
  • Select an option

  • Save Soolthub/39744079036f02476f5320d699fea318 to your computer and use it in GitHub Desktop.

Select an option

Save Soolthub/39744079036f02476f5320d699fea318 to your computer and use it in GitHub Desktop.

Revisions

  1. @BenjaminPoncet BenjaminPoncet revised this gist Feb 27, 2020. 1 changed file with 308 additions and 62 deletions.
    370 changes: 308 additions & 62 deletions ffmpeg-wrapper
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,35 @@
    #!/bin/bash

    rev="12"

    _log(){
    echo "$(date '+%Y-%m-%d %H:%M:%S') - ${streamhash} - $1" >> /tmp/ffmpeg.log
    echo "$(date '+%Y-%m-%d %H:%M:%S') - ${streamid} - $1" >> /tmp/ffmpeg.log
    }

    _log_para(){
    echo "$1" | fold -w 120 | sed "s/^.*$/$(date '+%Y-%m-%d %H:%M:%S') - ${streamhash} - = &/" >> /tmp/ffmpeg.log
    echo "$1" | fold -w 120 | sed "s/^.*$/$(date '+%Y-%m-%d %H:%M:%S') - ${streamid} - = &/" >> /tmp/ffmpeg.log
    }

    _term(){
    rm /tmp/ffmpeg-${streamhash}.stderr
    rm /tmp/ffmpeg-${streamid}.stderr
    _log "*** KILLCHILD ***"
    kill -TERM "$childpid" 2>/dev/null
    }

    trap _term SIGTERM

    arch=`uname -a | sed 's/.*synology_//' | cut -d '_' -f 1`
    nas=`uname -a | sed 's/.*synology_//' | cut -d '_' -f 2`
    pid=$$
    paramvs=$@
    stream="${@: -1}"
    streamid="FFM$pid"
    bin1=/var/packages/ffmpeg/target/bin/ffmpeg
    bin2=/var/packages/VideoStation/target/bin/ffmpeg.orig
    args=()

    vcodec="KO"

    while [[ $# -gt 0 ]]
    do
    case "$1" in
    @@ -26,94 +38,328 @@ case "$1" in
    movie="$1"
    args+=("-i" "$1")
    ;;
    -hwaccel)
    shift
    hwaccel="$1"
    args+=("-hwaccel" "$1")
    ;;
    -scodec)
    shift
    scodec="$1"
    args+=("-scodec" "$1")
    ;;
    -f)
    shift
    fcodec="$1"
    args+=("-f" "$1")
    ;;
    -map)
    shift
    args+=("-map" "$1")
    idmap=`echo $1 | cut -d : -f 2`
    if [ "$vcodec" = "KO" ]; then
    vcodec=`/var/packages/ffmpeg/target/bin/ffprobe -v error -select_streams $idmap -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$movie" | head -n 1`
    vcodecprofile=`/var/packages/ffmpeg/target/bin/ffprobe -v error -select_streams $idmap -show_entries stream=profile -of default=noprint_wrappers=1:nokey=1 "$movie" | head -n 1`
    else
    acodec=`/var/packages/ffmpeg/target/bin/ffprobe -v error -select_streams $idmap -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$movie" | head -n 1`
    fi
    ;;
    *)
    args+=("$1")
    stream="$1"
    ;;
    esac
    shift
    done

    streamhash=`echo $stream | cut -d / -f 5 | cut -d _ -f 2`
    bin=/var/packages/ffmpeg/target/bin/ffmpeg
    _log "*** PROCESS START REV $rev DS$nas ($arch) PID $pid ***"

    if echo $stream | grep "/tmp/SYNOVIDEO_ffmpeg_.*\.srt"; then
    streamhash=`echo $stream | cut -d _ -f 3 | sed -e 's/.srt//g'`
    bin=/var/packages/VideoStation/target/bin/ffmpeg.orig
    fi

    _log "*** PROCESS START ***"
    streamdir=`dirname "$stream"`
    device=`cat ${streamdir}/video_metadata | jq -r '.device'`
    _log "DEVICE = $device"
    _log "MOVIE = $movie"
    _log "FFMPEG = $bin"
    _log_para "$paramvs"

    set -- "${args[@]}"

    argsnew=()
    args1sv=()
    args2sv=()
    args1vs=()
    args2vs=()

    while [[ $# -gt 0 ]]
    do
    case "$1" in
    -ss)
    shift
    argsnew+=("-ss" "$1")
    args1sv+=("-ss" "$1")
    args1sv+=("-noaccurate_seek")
    args1vs+=("-ss" "$1")
    args1vs+=("-noaccurate_seek")
    args2sv+=("-analyzeduration" "10000000")
    args2vs+=("-analyzeduration" "10000000")
    ;;
    -i)
    shift
    argsnew+=("-i" "$1")
    args1sv+=("-i" "$1")
    args2sv+=("-i" "pipe:0" "-map" "0")
    args1vs+=("-i" "$1")
    args2vs+=("-i" "pipe:0" "-map" "0")
    ;;
    -vf)
    shift
    if [ "$hwaccel" = "vaapi" ] && [ "$vcodecprofile" = "Main 10" ]; then
    scale_w=`echo "${1}" | sed -e 's/.*=w=//g' | sed -e 's/:h=.*//g'`
    scale_h=`echo "${1}" | sed -e 's/.*:h=//g'`
    if let ${scale_w} AND let ${scale_h}; then
    argsnew+=("-vf" "scale_vaapi=w=${scale_w}:h=${scale_h}:format=nv12,hwupload,setsar=sar=1")
    else
    argsnew+=("-vf" "scale_vaapi=format=nv12,hwupload,setsar=sar=1")
    fi
    else
    argsnew+=("-vf" "$1")
    fi
    args2sv+=("-vf" "$1")
    args1vs+=("-vf" "$1")
    ;;
    -vcodec)
    shift
    argsnew+=("-vcodec" "$1")
    args1sv+=("-vcodec" "copy")
    args2sv+=("-vcodec" "$1")
    args1vs+=("-vcodec" "$1")
    args2vs+=("-vcodec" "copy")
    ;;
    -acodec)
    shift
    if [ "$1" = "libfaac" ]; then
    argsnew+=("-acodec" "aac")
    args1sv+=("-acodec" "aac")
    args2vs+=("-acodec" "aac")
    else
    argsnew+=("-acodec" "$1")
    args1sv+=("-acodec" "$1")
    args2vs+=("-acodec" "$1")
    fi
    args2sv+=("-acodec" "copy")
    args1vs+=("-acodec" "copy")
    ;;
    -ab)
    shift
    argsnew+=("-ab" "$1")
    args1sv+=("-ab" "$1")
    args2vs+=("-ab" "$1")
    ;;
    -ac)
    shift
    argsnew+=("-ac" "$1")
    args1sv+=("-ac" "$1")
    args2vs+=("-ac" "$1")
    ;;
    -f)
    shift
    argsnew+=("-f" "$1")
    args1sv+=("-f" "mpegts")
    args2sv+=("-f" "$1")
    args1vs+=("-f" "mpegts")
    args2vs+=("-f" "$1")
    ;;
    -segment_format)
    shift
    argsnew+=("-segment_format" "$1")
    args2vs+=("-segment_format" "$1")
    args2sv+=("-segment_format" "$1")
    ;;
    -segment_list_type)
    shift
    argsnew+=("-segment_list_type" "$1")
    args2vs+=("-segment_list_type" "$1")
    args2sv+=("-segment_list_type" "$1")
    ;;
    -hls_seek_time)
    shift
    argsnew+=("-hls_seek_time" "$1")
    args2vs+=("-hls_seek_time" "$1")
    args2sv+=("-hls_seek_time" "$1")
    ;;
    -segment_time)
    shift
    argsnew+=("-segment_time" "$1")
    args2vs+=("-segment_time" "$1")
    args2sv+=("-segment_time" "$1")
    ;;
    -segment_time_delta)
    shift
    argsnew+=("-segment_time_delta" "$1")
    args2vs+=("-segment_time_delta" "$1")
    args2sv+=("-segment_time_delta" "$1")
    ;;
    -segment_start_number)
    shift
    argsnew+=("-segment_start_number" "$1")
    args2vs+=("-segment_start_number" "$1")
    args2sv+=("-segment_start_number" "$1")
    ;;
    -individual_header_trailer)
    shift
    argsnew+=("-individual_header_trailer" "$1")
    args2vs+=("-individual_header_trailer" "$1")
    args2sv+=("-individual_header_trailer" "$1")
    ;;
    -avoid_negative_ts)
    shift
    argsnew+=("-avoid_negative_ts" "$1")
    args2vs+=("-avoid_negative_ts" "$1")
    args2sv+=("-avoid_negative_ts" "$1")
    ;;
    -break_non_keyframes)
    shift
    argsnew+=("-break_non_keyframes" "$1")
    args2vs+=("-break_non_keyframes" "$1")
    args2sv+=("-break_non_keyframes" "$1")
    ;;
    -max_muxing_queue_size)
    shift
    args2vs+=("-max_muxing_queue_size" "$1")
    args2sv+=("-max_muxing_queue_size" "$1")
    ;;
    -map)
    shift
    argsnew+=("-map" "$1")
    args1sv+=("-map" "$1")
    args1vs+=("-map" "$1")
    ;;
    *)
    argsnew+=("$1")
    if [ "$stream" = "$1" ]; then
    args1sv+=("-bufsize" "1024k" "pipe:1")
    args2sv+=("$1")
    args1vs+=("-bufsize" "1024k" "pipe:1")
    args2vs+=("$1")
    else
    args2sv+=("$1")
    args1vs+=("$1")
    fi
    ;;
    esac
    shift
    done

    sed -i -e "s/{\"PID\":${pid},\"hardware_transcode\":true,/{\"PID\":${pid},\"hardware_transcode\":false,/" /tmp/VideoStation/enabled

    startexectime=`date +%s`

    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &
    childpid=$!
    if [ "$scodec" = "subrip" ]; then

    _log "CHILDPID = $childpid"
    _log "FFMPEG = $bin2"
    _log "CODEC = $scodec"
    _log "PARAMVS ="
    _log_para "$paramvs"

    $bin2 "${args[@]}" &> /tmp/ffmpeg-${streamid}.stderr &

    wait $childpid
    elif [ "$fcodec" = "mjpeg" ]; then

    _log "FFMPEG = $bin2"
    _log "CODEC = $fcodec"
    _log "PARAMVS ="
    _log_para "$paramvs"

    $bin2 "${args[@]}" &> /tmp/ffmpeg-${streamid}.stderr &

    else

    _log "VCODEC = $vcodec ($vcodecprofile)"
    _log "ACODEC = $acodec"
    _log "PARAMVS ="
    _log_para "$paramvs"
    _log "MODE = WRAP"
    _log "FFMPEG = $bin1"
    _log "PARAMWP ="
    param1=${argsnew[@]}
    _log_para "$param1"

    $bin1 "${argsnew[@]}" &> /tmp/ffmpeg-${streamid}.stderr &

    stopexectime=`date +%s`
    if test $((stopexectime-startexectime)) -lt 3; then
    _log "STDERR ="
    _log_para "`tail -n 10 /tmp/ffmpeg-${streamhash}.stderr`"
    fi

    _log "*** CHILD END ***"
    childpid=$!
    _log "CHILDPID = $childpid"
    wait $childpid

    if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then

    _log "*** RW MODE ***"

    args=()
    while [[ $# -gt 0 ]]
    do
    case "$1" in
    -vf)
    shift
    scale_w=`echo "${1}" | sed -e 's/.*=w=//g' | sed -e 's/:h=.*//g'`
    scale_h=`echo "${1}" | sed -e 's/.*:h=//g'`
    if let ${scale_w} AND let ${scale_h}; then
    args+=("-vf" "scale_vaapi=w=${scale_w}:h=${scale_h}:format=nv12,hwupload,setsar=sar=1")
    else
    args+=("-vf" "scale_vaapi=format=nv12,hwupload,setsar=sar=1")
    fi
    ;;
    *)
    args+=("$1")
    ;;
    esac
    shift
    done
    set -- "${args[@]}"
    paramrw=$@

    _log "FFMPEG = $bin"
    _log_para "$paramrw"
    if grep "Conversion failed!" /tmp/ffmpeg-${streamid}.stderr || grep "Error opening filters!" /tmp/ffmpeg-${streamid}.stderr || grep "Unrecognized option" /tmp/ffmpeg-${streamid}.stderr || grep "Invalid data found when processing input" /tmp/ffmpeg-${streamid}.stderr; then

    _log "*** CHILD END ***"
    startexectime=`date +%s`

    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &
    _log "STDOUT ="
    _log_para "`tail -n 15 /tmp/ffmpeg-${streamid}.stderr`"
    _log "MODE = PIPE SV"
    _log "FFMPEG1 = $bin1"
    _log "FFMPEG2 = $bin2"
    _log "PARAM1 ="
    param1=${args1sv[@]}
    _log_para "$param1"
    _log "PARAM2 ="
    param2=${args2sv[@]}
    _log_para "$param2"

    $bin1 "${args1sv[@]}" | $bin2 "${args2sv[@]}" &> /tmp/ffmpeg-${streamid}.stderr &

    childpid=$!

    _log "CHILDPID = $childpid"
    wait $childpid

    fi

    if grep "Conversion failed!" /tmp/ffmpeg-${streamid}.stderr || grep "Error opening filters!" /tmp/ffmpeg-${streamid}.stderr || grep "Unrecognized option" /tmp/ffmpeg-${streamid}.stderr || grep "Invalid data found when processing input" /tmp/ffmpeg-${streamid}.stderr; then

    _log "*** CHILD END ***"
    startexectime=`date +%s`
    _log "STDOUT ="
    _log_para "`tail -n 15 /tmp/ffmpeg-${streamid}.stderr`"
    _log "MODE = PIPE VS"
    _log "FFMPEG1 = $bin2"
    _log "FFMPEG2 = $bin1"
    _log "PARAM1 ="
    param1=${args1vs[@]}
    _log_para "$param1"
    _log "PARAM2 ="
    param2=${args2vs[@]}
    _log_para "$param2"

    $bin2 "${args1vs[@]}" | $bin1 "${args2vs[@]}" &> /tmp/ffmpeg-${streamid}.stderr &

    childpid=$!
    _log "CHILDPID = $childpid"
    wait $childpid

    stopexectime=`date +%s`
    if test $((stopexectime-startexectime)) -lt 3; then
    _log "STDERR ="
    _log_para "`tail -n 10 /tmp/ffmpeg-${streamhash}.stderr`"
    fi

    fi

    if grep "Conversion failed!" /tmp/ffmpeg-${streamid}.stderr || grep "Error opening filters!" /tmp/ffmpeg-${streamid}.stderr || grep "Unrecognized option" /tmp/ffmpeg-${streamid}.stderr || grep "Invalid data found when processing input" /tmp/ffmpeg-${streamid}.stderr; then

    _log "*** CHILD END ***"
    startexectime=`date +%s`
    _log "STDOUT ="
    _log_para "`tail -n 15 /tmp/ffmpeg-${streamid}.stderr`"
    _log "MODE = ORIG"
    _log "FFMPEG = $bin2"

    $bin2 "${args[@]}" &> /tmp/ffmpeg-${streamid}.stderr &

    childpid=$!
    _log "CHILDPID = $childpid"
    wait $childpid

    fi

    stopexectime=`date +%s`
    if test $((stopexectime-startexectime)) -lt 10; then
    _log "STDOUT ="
    _log_para "`tail -n 15 /tmp/ffmpeg-${streamid}.stderr`"
    fi

    _log "*** CHILD END ***"
    _log "*** PROCESS END ***"

    rm /tmp/ffmpeg-${streamhash}.stderr
    rm /tmp/ffmpeg-${streamid}.stderr
  2. @BenjaminPoncet BenjaminPoncet renamed this gist Dec 31, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @BenjaminPoncet BenjaminPoncet renamed this gist Dec 29, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @BenjaminPoncet BenjaminPoncet revised this gist Dec 28, 2019. 1 changed file with 49 additions and 20 deletions.
    69 changes: 49 additions & 20 deletions ffmpeg
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,16 @@
    #!/bin/bash

    _log(){
    echo "$(date '+%Y-%m-%d %H:%M:%S') - ${streamhash} - $1" >> /tmp/ffmpeg.log
    }

    _log_para(){
    echo "$1" | fold -w 120 | sed "s/^.*$/$(date '+%Y-%m-%d %H:%M:%S') - ${streamhash} - = &/" >> /tmp/ffmpeg.log
    }

    _term(){
    rm /tmp/ffmpeg-${streamhash}.stderr
    echo "---KILLCHILD---" >> /tmp/ffmpeg.log
    echo >> /tmp/ffmpeg.log
    _log "*** KILLCHILD ***"
    kill -TERM "$childpid" 2>/dev/null
    }

    @@ -18,41 +25,51 @@ case "$1" in
    shift
    movie="$1"
    args+=("-i" "$1")
    ;;
    ;;
    *)
    args+=("$1")
    stream="$1"
    ;;
    ;;
    esac
    shift
    done

    streamhash=`echo $stream | cut -d / -f 5`
    streamhash=`echo $stream | cut -d / -f 5 | cut -d _ -f 2`
    bin=/var/packages/ffmpeg/target/bin/ffmpeg

    if echo $stream | grep "/tmp/SYNOVIDEO_ffmpeg_.*\.srt"; then
    streamhash=`echo $stream | cut -d _ -f 3 | sed -e 's/.srt//g'`
    bin=/var/packages/VideoStation/target/bin/ffmpeg.orig
    fi

    echo "MOVIE = $movie" >> /tmp/ffmpeg.log
    echo "HASH = $streamhash" >> /tmp/ffmpeg.log
    echo "PARAM_VS = $paramvs" >> /tmp/ffmpeg.log
    _log "*** PROCESS START ***"
    _log "MOVIE = $movie"
    _log "FFMPEG = $bin"
    _log_para "$paramvs"

    set -- "${args[@]}"
    #echo "PARAM_RW = $@" >> /tmp/ffmpeg.log
    echo "FFMPEG = $bin" >> /tmp/ffmpeg.log

    startexectime=`date +%s`

    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &
    childpid=$!

    echo "CHILDPID = $childpid" >> /tmp/ffmpeg.log
    _log "CHILDPID = $childpid"

    wait $childpid
    echo "---CHILD END---" >> /tmp/ffmpeg.log

    stopexectime=`date +%s`
    if test $((stopexectime-startexectime)) -lt 3; then
    _log "STDERR ="
    _log_para "`tail -n 10 /tmp/ffmpeg-${streamhash}.stderr`"
    fi

    _log "*** CHILD END ***"

    if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    echo "ERROR = Conversion failed!" >> /tmp/ffmpeg.log

    _log "*** RW MODE ***"

    args=()
    while [[ $# -gt 0 ]]
    do
    @@ -74,17 +91,29 @@ if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    shift
    done
    set -- "${args[@]}"
    echo "PARAM_RW = $@" >> /tmp/ffmpeg.log
    echo "FFMPEG = $bin" >> /tmp/ffmpeg.log
    paramrw=$@

    _log "FFMPEG = $bin"
    _log_para "$paramrw"

    startexectime=`date +%s`

    $bin "$@" &
    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &
    childpid=$!

    echo "CHILDPID = $childpid" >> /tmp/ffmpeg.log
    _log "CHILDPID = $childpid"

    wait $childpid
    echo "---CHILD END---" >> /tmp/ffmpeg.log

    stopexectime=`date +%s`
    if test $((stopexectime-startexectime)) -lt 3; then
    _log "STDERR ="
    _log_para "`tail -n 10 /tmp/ffmpeg-${streamhash}.stderr`"
    fi

    _log "*** CHILD END ***"
    fi
    echo "---PROCESS END---" >> /tmp/ffmpeg.log
    echo >> /tmp/ffmpeg.log

    _log "*** PROCESS END ***"

    rm /tmp/ffmpeg-${streamhash}.stderr
  5. @BenjaminPoncet BenjaminPoncet revised this gist Dec 27, 2019. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions ffmpeg
    Original file line number Diff line number Diff line change
    @@ -18,11 +18,11 @@ case "$1" in
    shift
    movie="$1"
    args+=("-i" "$1")
    ;;
    ;;
    *)
    args+=("$1")
    stream="$1"
    ;;
    ;;
    esac
    shift
    done
    @@ -59,8 +59,13 @@ if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    case "$1" in
    -vf)
    shift
    vf=`echo "${1}" | sed -e 's/format=nv12.*scale_vaapi/scale_vaapi/g' -e 's/$/:format=nv12,hwupload,setsar=sar=1/'`
    args+=("-vf" "$vf")
    scale_w=`echo "${1}" | sed -e 's/.*=w=//g' | sed -e 's/:h=.*//g'`
    scale_h=`echo "${1}" | sed -e 's/.*:h=//g'`
    if let ${scale_w} AND let ${scale_h}; then
    args+=("-vf" "scale_vaapi=w=${scale_w}:h=${scale_h}:format=nv12,hwupload,setsar=sar=1")
    else
    args+=("-vf" "scale_vaapi=format=nv12,hwupload,setsar=sar=1")
    fi
    ;;
    *)
    args+=("$1")
  6. @BenjaminPoncet BenjaminPoncet revised this gist Dec 26, 2019. 1 changed file with 13 additions and 16 deletions.
    29 changes: 13 additions & 16 deletions ffmpeg
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@

    _term(){
    rm /tmp/ffmpeg-${streamhash}.stderr
    echo "---KILLCHILD---" >> /tmp/ffmpeg.log
    echo >> /tmp/ffmpeg.log
    kill -TERM "$childpid" 2>/dev/null
    }

    @@ -16,19 +18,17 @@ case "$1" in
    shift
    movie="$1"
    args+=("-i" "$1")
    ;;
    ;;
    *)
    args+=("$1")
    stream="$1"
    ;;
    ;;
    esac
    shift
    done

    if echo $stream | grep "/tmp/VideoStation/HLS/"; then
    streamhash=`echo $stream | cut -d / -f 5`
    bin=/var/packages/ffmpeg/target/bin/ffmpeg
    fi
    streamhash=`echo $stream | cut -d / -f 5`
    bin=/var/packages/ffmpeg/target/bin/ffmpeg

    if echo $stream | grep "/tmp/SYNOVIDEO_ffmpeg_.*\.srt"; then
    streamhash=`echo $stream | cut -d _ -f 3 | sed -e 's/.srt//g'`
    @@ -40,32 +40,27 @@ echo "HASH = $streamhash" >> /tmp/ffmpeg.log
    echo "PARAM_VS = $paramvs" >> /tmp/ffmpeg.log

    set -- "${args[@]}"
    echo "PARAM_RW = $@" >> /tmp/ffmpeg.log
    #echo "PARAM_RW = $@" >> /tmp/ffmpeg.log
    echo "FFMPEG = $bin" >> /tmp/ffmpeg.log

    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &
    childpid=$!

    echo "CHILDPID = $childpid" >> /tmp/ffmpeg.log
    echo >> /tmp/ffmpeg.log

    wait $childpid
    echo "---CHILD END---" >> /tmp/ffmpeg.log

    if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    echo "ERROR = Conversion failed!" >> /tmp/ffmpeg.log
    args=()
    while [[ $# -gt 0 ]]
    do
    case "$1" in
    -vprofile)
    shift
    args+=("-x264profile" "$1")
    ;;
    -vf)
    shift
    scale_w=`echo "${1}" | sed -e 's/.*=w=//g' | sed -e 's/:h=.*//g'`
    scale_h=`echo "${1}" | sed -e 's/.*:h=//g'`
    args+=("-vf" "scale_vaapi=w=${scale_w}:h=${scale_h}:format=nv12")
    vf=`echo "${1}" | sed -e 's/format=nv12.*scale_vaapi/scale_vaapi/g' -e 's/$/:format=nv12,hwupload,setsar=sar=1/'`
    args+=("-vf" "$vf")
    ;;
    *)
    args+=("$1")
    @@ -81,8 +76,10 @@ if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    childpid=$!

    echo "CHILDPID = $childpid" >> /tmp/ffmpeg.log
    echo >> /tmp/ffmpeg.log

    wait $childpid
    echo "---CHILD END---" >> /tmp/ffmpeg.log
    fi
    echo "---PROCESS END---" >> /tmp/ffmpeg.log
    echo >> /tmp/ffmpeg.log
    rm /tmp/ffmpeg-${streamhash}.stderr
  7. @BenjaminPoncet BenjaminPoncet revised this gist Dec 26, 2019. No changes.
  8. @BenjaminPoncet BenjaminPoncet revised this gist Dec 26, 2019. No changes.
  9. @BenjaminPoncet BenjaminPoncet revised this gist Dec 24, 2019. 1 changed file with 27 additions and 45 deletions.
    72 changes: 27 additions & 45 deletions ffmpeg
    Original file line number Diff line number Diff line change
    @@ -1,46 +1,21 @@
    #!/bin/bash

    _term(){
    rm /tmp/ffmpeg-${streamhash}.stderr
    kill -TERM "$childpid" 2>/dev/null
    }

    trap _term SIGTERM

    paramvs=$@
    args=()
    while [[ $# -gt 0 ]]
    do
    case "$1" in
    #-hls_seek_time)
    #shift
    #hls_seek_time="$1"
    #;;
    -i)
    shift
    movie="$1"
    args+=("-i" "$1")
    ;;
    -vaapi_device)
    shift
    args+=("-vaapi_device" "$1")
    ;;
    -hwaccel)
    shift
    args+=("-hwaccel" "$1")
    ;;
    -hwaccel_output_format)
    shift
    args+=("-hwaccel_output_format" "$1")
    ;;
    -vcodec)
    shift
    args+=("-vcodec" "$1")
    #args+=("-vcodec" "libx264")
    ;;
    -vf)
    shift
    #args+=("-vf" "scale_vaapi=w=320:h=240:format=nv12")
    args+=("-vf" "$1")
    #args+=("-filter:v" "scale=720:-1")
    ;;
    -vb)
    shift
    args+=("-vb" "$1")
    #args+=("-preset" "ultrafast")
    ;;
    *)
    args+=("$1")
    @@ -50,34 +25,42 @@ esac
    shift
    done

    streamhash=`echo $stream | cut -d / -f 5`
    if echo $stream | grep "/tmp/VideoStation/HLS/"; then
    streamhash=`echo $stream | cut -d / -f 5`
    bin=/var/packages/ffmpeg/target/bin/ffmpeg
    fi

    if echo $stream | grep "/tmp/SYNOVIDEO_ffmpeg_.*\.srt"; then
    streamhash=`echo $stream | cut -d _ -f 3 | sed -e 's/.srt//g'`
    bin=/var/packages/VideoStation/target/bin/ffmpeg.orig
    fi

    echo "MOVIE = $movie" >> /tmp/ffmpeg.log
    echo "STREAM = $streamhash" >> /tmp/ffmpeg.log
    echo "HASH = $streamhash" >> /tmp/ffmpeg.log
    echo "PARAM_VS = $paramvs" >> /tmp/ffmpeg.log

    #args+=("-hls_seek_time" "$hls_seek_time")
    bin=/var/packages/ffmpeg/target/bin/ffmpeg

    set -- "${args[@]}"
    echo "PARAM_RW = $@" >> /tmp/ffmpeg.log
    echo "FFMPEG = $bin" >> /tmp/ffmpeg.log

    pkill -F /tmp/ffmpeg-${streamhash}.lastpid

    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stdout &
    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &
    childpid=$!

    echo $childpid > /tmp/ffmpeg-${streamhash}.lastpid
    echo "CHILDPID = $childpid" >> /tmp/ffmpeg.log
    echo >> /tmp/ffmpeg.log

    wait $childpid

    if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stdout; then
    if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    echo "ERROR = Conversion failed!" >> /tmp/ffmpeg.log
    args=()
    while [[ $# -gt 0 ]]
    do
    case "$1" in
    -vprofile)
    shift
    args+=("-x264profile" "$1")
    ;;
    -vf)
    shift
    scale_w=`echo "${1}" | sed -e 's/.*=w=//g' | sed -e 's/:h=.*//g'`
    @@ -97,10 +80,9 @@ if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stdout; then
    $bin "$@" &
    childpid=$!

    echo $childpid > /tmp/ffmpeg-${streamhash}.lastpid
    echo "CHILDPID = $childpid" >> /tmp/ffmpeg.log
    echo >> /tmp/ffmpeg.log

    wait $childpid
    fi

    echo >> /tmp/ffmpeg.log
    rm /tmp/ffmpeg-${streamhash}.stderr
  10. @BenjaminPoncet BenjaminPoncet revised this gist Dec 23, 2019. 1 changed file with 31 additions and 28 deletions.
    59 changes: 31 additions & 28 deletions ffmpeg
    Original file line number Diff line number Diff line change
    @@ -1,78 +1,79 @@
    #!/bin/bash

    echo "$@" >> /tmp/ffmpeg.log

    paramvs=$@
    args=()
    while [[ $# -gt 0 ]]
    do
    case "$1" in
    #-hls_seek_time)
    # shift
    # hls_seek_time="$1"
    #;;
    #shift
    #hls_seek_time="$1"
    #;;
    -i)
    shift
    movie="$1"
    args+=("-i" "$1")
    ;;
    ;;
    -vaapi_device)
    shift
    args+=("-vaapi_device" "$1")
    ;;
    ;;
    -hwaccel)
    shift
    args+=("-hwaccel" "$1")
    ;;
    ;;
    -hwaccel_output_format)
    shift
    args+=("-hwaccel_output_format" "$1")
    ;;
    ;;
    -vcodec)
    shift
    args+=("-vcodec" "$1")
    #args+=("-vcodec" "libx264")
    ;;
    ;;
    -vf)
    shift
    #args+=("-vf" "scale_vaapi=w=320:h=240:format=nv12")
    args+=("-vf" "$1")
    #args+=("-filter:v" "scale=720:-1")
    ;;
    ;;
    -vb)
    shift
    args+=("-vb" "$1")
    #args+=("-preset" "ultrafast")
    ;;
    ;;
    *)
    args+=("$1")
    stream="$1"
    ;;
    ;;
    esac
    shift
    done

    streamhash=`echo $stream | cut -d / -f 5`
    echo "MOVIE = $movie" >> /tmp/ffmpeg.log
    echo "STREAM = $streamhash" >> /tmp/ffmpeg.log
    echo "MOVIE = $movie" >> /tmp/ffmpeg.log
    echo "STREAM = $streamhash" >> /tmp/ffmpeg.log
    echo "PARAM_VS = $paramvs" >> /tmp/ffmpeg.log

    #args+=("-hls_seek_time" "$hls_seek_time")
    bin=/var/packages/ffmpeg/target/bin/ffmpeg

    set -- "${args[@]}"
    echo $bin >> /tmp/ffmpeg.log
    echo "$@" >> /tmp/ffmpeg.log
    echo "PARAM_RW = $@" >> /tmp/ffmpeg.log
    echo "FFMPEG = $bin" >> /tmp/ffmpeg.log

    pkill -F /tmp/ffmpeg-${streamhash}.lastpid

    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &
    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stdout &
    childpid=$!

    echo $! > /tmp/ffmpeg-${streamhash}.lastpid
    echo $! >> /tmp/ffmpeg.log
    echo $childpid > /tmp/ffmpeg-${streamhash}.lastpid
    echo "CHILDPID = $childpid" >> /tmp/ffmpeg.log

    sleep 2
    wait $childpid

    if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    echo "Conversion failed!" >> /tmp/ffmpeg.log
    if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stdout; then
    echo "ERROR = Conversion failed!" >> /tmp/ffmpeg.log
    args=()
    while [[ $# -gt 0 ]]
    do
    @@ -85,19 +86,21 @@ if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    ;;
    *)
    args+=("$1")
    stream="$1"
    ;;
    esac
    shift
    done
    set -- "${args[@]}"
    echo $bin >> /tmp/ffmpeg.log
    echo "$@" >> /tmp/ffmpeg.log
    echo "PARAM_RW = $@" >> /tmp/ffmpeg.log
    echo "FFMPEG = $bin" >> /tmp/ffmpeg.log

    $bin "$@" &
    childpid=$!

    echo $! > /tmp/ffmpeg-${streamhash}.lastpid
    echo $! >> /tmp/ffmpeg.log
    echo $childpid > /tmp/ffmpeg-${streamhash}.lastpid
    echo "CHILDPID = $childpid" >> /tmp/ffmpeg.log

    wait $childpid
    fi

    echo >> /tmp/ffmpeg.log
  11. @BenjaminPoncet BenjaminPoncet revised this gist Dec 23, 2019. 1 changed file with 28 additions and 5 deletions.
    33 changes: 28 additions & 5 deletions ffmpeg
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@ while [[ $# -gt 0 ]]
    do
    case "$1" in
    #-hls_seek_time)
    #shift
    #hls_seek_time="$1"
    # shift
    # hls_seek_time="$1"
    #;;
    -i)
    shift
    @@ -34,10 +34,8 @@ case "$1" in
    ;;
    -vf)
    shift
    scale_w=`echo "${1}" | sed -e 's/.*=w=//g' | sed -e 's/:h=.*//g'`
    scale_h=`echo "${1}" | sed -e 's/.*:h=//g'`
    #args+=("-vf" "scale_vaapi=w=320:h=240:format=nv12")
    args+=("-vf" "scale_vaapi=w=${scale_w}:h=${scale_h}:format=nv12")
    args+=("-vf" "$1")
    #args+=("-filter:v" "scale=720:-1")
    ;;
    -vb)
    @@ -75,6 +73,31 @@ sleep 2

    if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    echo "Conversion failed!" >> /tmp/ffmpeg.log
    args=()
    while [[ $# -gt 0 ]]
    do
    case "$1" in
    -vf)
    shift
    scale_w=`echo "${1}" | sed -e 's/.*=w=//g' | sed -e 's/:h=.*//g'`
    scale_h=`echo "${1}" | sed -e 's/.*:h=//g'`
    args+=("-vf" "scale_vaapi=w=${scale_w}:h=${scale_h}:format=nv12")
    ;;
    *)
    args+=("$1")
    stream="$1"
    ;;
    esac
    shift
    done
    set -- "${args[@]}"
    echo $bin >> /tmp/ffmpeg.log
    echo "$@" >> /tmp/ffmpeg.log

    $bin "$@" &

    echo $! > /tmp/ffmpeg-${streamhash}.lastpid
    echo $! >> /tmp/ffmpeg.log
    fi

    echo >> /tmp/ffmpeg.log
  12. @BenjaminPoncet BenjaminPoncet created this gist Dec 22, 2019.
    80 changes: 80 additions & 0 deletions ffmpeg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    #!/bin/bash

    echo "$@" >> /tmp/ffmpeg.log

    args=()
    while [[ $# -gt 0 ]]
    do
    case "$1" in
    #-hls_seek_time)
    #shift
    #hls_seek_time="$1"
    #;;
    -i)
    shift
    movie="$1"
    args+=("-i" "$1")
    ;;
    -vaapi_device)
    shift
    args+=("-vaapi_device" "$1")
    ;;
    -hwaccel)
    shift
    args+=("-hwaccel" "$1")
    ;;
    -hwaccel_output_format)
    shift
    args+=("-hwaccel_output_format" "$1")
    ;;
    -vcodec)
    shift
    args+=("-vcodec" "$1")
    #args+=("-vcodec" "libx264")
    ;;
    -vf)
    shift
    scale_w=`echo "${1}" | sed -e 's/.*=w=//g' | sed -e 's/:h=.*//g'`
    scale_h=`echo "${1}" | sed -e 's/.*:h=//g'`
    #args+=("-vf" "scale_vaapi=w=320:h=240:format=nv12")
    args+=("-vf" "scale_vaapi=w=${scale_w}:h=${scale_h}:format=nv12")
    #args+=("-filter:v" "scale=720:-1")
    ;;
    -vb)
    shift
    args+=("-vb" "$1")
    #args+=("-preset" "ultrafast")
    ;;
    *)
    args+=("$1")
    stream="$1"
    ;;
    esac
    shift
    done

    streamhash=`echo $stream | cut -d / -f 5`
    echo "MOVIE = $movie" >> /tmp/ffmpeg.log
    echo "STREAM = $streamhash" >> /tmp/ffmpeg.log

    #args+=("-hls_seek_time" "$hls_seek_time")
    bin=/var/packages/ffmpeg/target/bin/ffmpeg

    set -- "${args[@]}"
    echo $bin >> /tmp/ffmpeg.log
    echo "$@" >> /tmp/ffmpeg.log

    pkill -F /tmp/ffmpeg-${streamhash}.lastpid

    $bin "$@" 2> /tmp/ffmpeg-${streamhash}.stderr &

    echo $! > /tmp/ffmpeg-${streamhash}.lastpid
    echo $! >> /tmp/ffmpeg.log

    sleep 2

    if grep "Conversion failed!" /tmp/ffmpeg-${streamhash}.stderr; then
    echo "Conversion failed!" >> /tmp/ffmpeg.log
    fi

    echo >> /tmp/ffmpeg.log