/ walking bear:D / embed srt into mkv using ffmpeg ImageMagick

embed srt into mkv using ffmpeg ImageMagick

2012-10-10 posted in [程式]

following shell script to embed srt into mkv and srt2frame.c for framming srt.

#!/bin/sh

if [ $# -lt 5 ]; then
	echo $0 avfile srtfile start duration output
	exit 1
fi

AVF=${1}
SRT=${2}
START=${3}
DURATION=${4}
OUTPUT=${5}

mkdir -p ${OUTPUT}.out
mkdir -p ${OUTPUT}.txt
mkdir -p ${OUTPUT}.frames

ffmpeg -i ${AVF} -ss ${START} -t ${DURATION} \
	-vf scale=-1:360 -f image2 -y ${OUTPUT}.frames/f%d.png \
	-ss ${START} -t ${DURATION} -acodec copy -sameq \
	-y ${OUTPUT}.mka

FRAMES=`ls ${OUTPUT}.frames |wc -l`
FPS=`echo "scale=0; ${FRAMES}/${DURATION}" |bc`

srt2frame ${SRT} ${START} ${DURATION} ${FPS} ${OUTPUT}.txt

for (( i=1 ; i<=${FRAMES} ; i++ )) ; do
	echo ${FRAMES} ${FPS} ${i}
	if [ -f ${OUTPUT}.txt/${i} ] ; then
		SUBTXT=`cat ${OUTPUT}.txt/${i}`
		convert ${OUTPUT}.frames/f${i}.png \
			-gravity Center \
			-font Microsoft-JhengHei-Negreta \
			-pointsize 24 -fill black \
			-draw "text 0,122 \"${SUBTXT}\"" \
			-pointsize 24 -fill white \
			-draw "text 0,120 \"${SUBTXT}\"" \
			${OUTPUT}.out/f${i}.png
	else
		cp ${OUTPUT}.frames/f${i}.png ${OUTPUT}.out
	fi
done

ffmpeg -threads 4 -r ${FPS} -f image2 -i ${OUTPUT}.out/f%d.png \
	-i ${OUTPUT}.mka -acodec copy -sameq -y ${OUTPUT}.mkv

echo "Done!"
exit

// EOT

walking bear:DRSS feed

关于

wkliang

Clarke's Three Laws:

  • When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
  • The only way of discovering the limits of the possible is to venture a little way past them into the impossible.
  • Any sufficiently advanced technology is indistinguishable from magic.
  • 版权申明

    知识共享许可协议

    Fork me on GitHub

    Powered by

    Disqus, GitHub, Google Custom Search, Gravatar, HighlightJS, jekyll