commit f3456e1a4ec00031c0cc9ca4e9e01ea1f57c7cb1 Author: sin <sin@2f30.org> Date: Thu, 20 Jun 2013 12:46:54 +0100 Initial commit Diffstat:
A | ytconv | | | 96 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 96 insertions(+), 0 deletions(-)
diff --git a/ytconv b/ytconv @@ -0,0 +1,96 @@ +#!/bin/bash + +if [[ $# -lt 2 ]]; then + echo "usage: ytconv [options] [<image>] <audio 1> [<audio 2> [.. <audio N>]]" + echo -e "\nOptions:" + echo -e "\t-t\t\tproduce only out.txt" + exit 1 +fi + +if [[ "$1" != "-t" ]]; then + identify "$1" > /dev/null 2>&1 || { + echo "ERROR: \`$1' is not an image file." + exit 1 + } + + TMP_DIR=$(mktemp -d /tmp/tmp.ytconv.$$.XXXXXX) + TMP_IMG=${TMP_DIR}/image.png + TMP_ALIST=${TMP_DIR}/audio_list + + echo "Converting image .." + IMG="$1" + shift 1 # remove image from args + convert "${IMG}" ${TMP_IMG} + mogrify -resize 1920x1080 ${TMP_IMG} + # ffmpeg does not accept images with odd width or height + IW=$(identify -format '%w' ${TMP_IMG}) + IH=$(identify -format '%h' ${TMP_IMG}) + if [[ $(( $IW % 2 )) -ne 0 ]]; then + IW=$(( $IW - 1 )) + mogrify -crop "${IW}x${IH}+0+0" ${TMP_IMG} + fi + if [[ $(( $IH % 2 )) -ne 0 ]]; then + IH=$(( $IH - 1 )) + mogrify -crop "${IW}x${IH}+0+0" ${TMP_IMG} + fi + + for x in "$@"; do + FILE_PATH=$(realpath "$x") + # because ffmpeg doesn't have a way to escape all special characters we + # create a symbolic link of the file that contains only alfanumeric characters + LN_FILE=$(mktemp ${TMP_DIR}/XXXXXX.${x##*.}) + ln -fs "$FILE_PATH" "$LN_FILE" + echo file \'"$LN_FILE"\' >> "$TMP_ALIST" + done + + (ffmpeg -codecs 2> /dev/null | grep libfdk_aac > /dev/null) && aac_codec="libfdk_aac" + [[ -z "$aac_codec" ]] && (ffmpeg -codecs 2> /dev/null | grep libfaac > /dev/null) && aac_codec="libfaac" + [[ -z "$aac_codec" ]] && aac_codec="aac" + + ffmpeg -loop 1 -i ${TMP_IMG} -f concat -i ${TMP_ALIST} -shortest -pix_fmt yuvj420p -c:a ${aac_codec} -b:a 512k -ar 96k -c:v libx264 -strict -2 out.mp4 + + ret=$? + rm -rf ${TMP_DIR} + [[ $ret -ne 0 ]] && exit $ret +else + shift 1 # remove -t from args + identify "$1" > /dev/null 2>&1 && shift 1 # remove image from args +fi + +rm -f out.txt +H=0 +M=0 +S=0 +CS=0 + +for x in "$@"; do + nm=$(echo "$x" | sed -e 's/\([:digit:]*\) \?[-. ] \?/\1. /') + printf "[%02d:%02d:%02d] %s\n" $H $M $S "${nm%.*}" >> out.txt + + t=$(ffmpeg -i "$x" 2>&1 | grep 'Duration:' | tr ',' ' ' | awk '{print $2}') + t=$(echo $t | tr ':.' ' ') + t=${t// 0/ } + t=( ${t#0*} ) + + H=$(( ${t[0]} + $H )) + M=$(( ${t[1]} + $M )) + S=$(( ${t[2]} + $S )) + CS=$(( ${t[3]} + $CS )) + + if [[ $CS -ge 100 ]]; then + S=$(( $S + 1 )) + CS=$(( $CS - 100 )) + fi + + if [[ $S -ge 60 ]]; then + M=$(( $M + 1 )) + S=$(( $S - 60 )) + fi + + if [[ $M -ge 60 ]]; then + H=$(( $H + 1 )) + M=$(( $M - 60 )) + fi +done + +exit 0