ytconv

yt video creator
git clone git@git.2f30.org/ytconv.git
Log | Files | Refs

ytconv (2721B)


      1 #!/bin/bash
      2 
      3 if [[ $# -lt 2 ]]; then
      4     echo "usage: ytconv [options] [<image>] <audio 1> [<audio 2> [.. <audio N>]]"
      5     echo -e "\nOptions:"
      6     echo -e "\t-t\t\tproduce only out.txt"
      7     exit 1
      8 fi
      9 
     10 if [[ "$1" != "-t" ]]; then
     11     identify "$1" > /dev/null 2>&1 || {
     12         echo "ERROR: \`$1' is not an image file."
     13         exit 1
     14     }
     15 
     16     TMP_DIR=$(mktemp -d /tmp/tmp.ytconv.$$.XXXXXX)
     17     TMP_IMG=${TMP_DIR}/image.png
     18     TMP_ALIST=${TMP_DIR}/audio_list
     19 
     20     echo "Converting image .."
     21     IMG="$1"
     22     shift 1 # remove image from args
     23     convert "${IMG}" ${TMP_IMG}
     24     mogrify -resize 1920x1080 ${TMP_IMG}
     25     # ffmpeg does not accept images with odd width or height
     26     IW=$(identify -format '%w' ${TMP_IMG})
     27     IH=$(identify -format '%h' ${TMP_IMG})
     28     if [[ $(( $IW % 2 )) -ne 0 ]]; then
     29         IW=$(( $IW - 1 ))
     30         mogrify -crop "${IW}x${IH}+0+0" ${TMP_IMG}
     31     fi
     32     if [[ $(( $IH % 2 )) -ne 0 ]]; then
     33         IH=$(( $IH - 1 ))
     34         mogrify -crop "${IW}x${IH}+0+0" ${TMP_IMG}
     35     fi
     36 
     37     for x in "$@"; do
     38         FILE_PATH=$(readlink -f "$x")
     39         # because ffmpeg doesn't have a way to escape all special characters we
     40         # create a symbolic link of the file that contains only alfanumeric characters
     41         LN_FILE=$(mktemp ${TMP_DIR}/XXXXXX)
     42         LN_FILE="$LN_FILE.${x##*.}"
     43         ln -fs "$FILE_PATH" "$LN_FILE"
     44         echo file \'"$LN_FILE"\' >> "$TMP_ALIST"
     45     done
     46 
     47     (ffmpeg -codecs 2> /dev/null | grep libfdk_aac > /dev/null) && aac_codec="libfdk_aac"
     48     [[ -z "$aac_codec" ]] && (ffmpeg -codecs 2> /dev/null | grep libfaac > /dev/null) && aac_codec="libfaac"
     49     [[ -z "$aac_codec" ]] && aac_codec="aac"
     50 
     51     ffmpeg -loop 1 -i ${TMP_IMG} -f concat -safe 0 -i ${TMP_ALIST} -shortest -pix_fmt yuvj420p -c:a ${aac_codec} -b:a 512k -ar 96k -c:v libx264 -strict -2 out.mp4
     52 
     53     ret=$?
     54     rm -rf ${TMP_DIR}
     55     [[ $ret -ne 0 ]] && exit $ret
     56 else
     57     shift 1 # remove -t from args
     58     identify "$1" > /dev/null 2>&1 && shift 1 # remove image from args
     59 fi
     60 
     61 rm -f out.txt
     62 H=0
     63 M=0
     64 S=0
     65 CS=0
     66 
     67 for x in "$@"; do
     68     nm=$(echo "$x" | sed -e 's/\([[:digit:]]*\) \?[-. ] \?/\1. /')
     69     printf "[%02d:%02d:%02d] %s\n" $H $M $S "${nm%.*}" >> out.txt
     70 
     71     t=$(ffmpeg -i "$x" 2>&1 | grep 'Duration:' | tr ',' ' ' | awk '{print $2}')
     72     t=$(echo $t | tr ':.' ' ')
     73     t=${t// 0/ }
     74     t=( ${t#0*} )
     75 
     76     H=$(( ${t[0]} + $H ))
     77     M=$(( ${t[1]} + $M ))
     78     S=$(( ${t[2]} + $S ))
     79     CS=$(( ${t[3]} + $CS ))
     80 
     81     if [[ $CS -ge 100 ]]; then
     82         S=$(( $S + 1 ))
     83         CS=$(( $CS - 100 ))
     84     fi
     85 
     86     if [[ $S -ge 60 ]]; then
     87         M=$(( $M + 1 ))
     88         S=$(( $S - 60 ))
     89     fi
     90 
     91     if [[ $M -ge 60 ]]; then
     92         H=$(( $H + 1 ))
     93         M=$(( $M - 60 ))
     94     fi
     95 done
     96 
     97 exit 0