Newer
Older
webtls / mov2html5
@HIROSE Yuuji HIROSE Yuuji on 2 Jun 2018 4 KB git gateway started
#!/bin/sh
# movie file to HTML5

mydir=`dirname $0`
mydir=`(cd $mydir; pwd -P)`
myname=`basename $0`
mybase=${myname%.*}
# aspect='16/9'
for cmd in ffmpeg ffmpeg3 ffmpeg2 ffmpeg1 ffmpeg010; do
  if type $cmd >/dev/null 2>&1; then
    ffmpeg=$cmd; break
  fi
done
ffmpeg="${FFMPEG:-$ffmpeg}"

crf=32
pxw=640
tmpd=`mktemp -d -t $myname.$$`
# formats="mp4 ogv webm"
formats="mp4 webm"
prefix="mov_"				title="Movie list"
mark_b="_MovStart_"			mark_e="_MovEnd_"
mark_eom="_MovEOM_"			index="index.html"

if [ -z "$tmpd" ]; then
  echo "Cannot create temporary directory...Abort." >&2
  exit 1
fi

htmlheader() {
  cat<<-EOF
	<!DOCTYPE html>
	<html>
	<head><title>$title</title>
	<link rel="stylesheet" type="text/css" href="${mybase}.css">
	</head>
	<body>
	<h1>$title</h1>
	EOF
}
elementmovie() {
  cat
}

trap "rm -rf $tmpd" INT TERM QUIT EXIT PIPE

usage() {
  cat<<-EOF
	Usage: $mybase [options] MovieFile(s)
	Options are as follows:
	  -c CRF	Set CRF: 0:lossless <-> 63:worst ($crf)
	  -f FORMATS	Comma Separated output format list($formats)
	  -i INDEX	HTML file to output ($index)
	  -N		Generate movies only, Not producing HTML
	  -n		No EXEC - Only showing command lines
	  -p PREFIX	Set output file prefix ($prefix)
	  -T TITLE	Set title of HTML index ($title)
	  -s START	Convert From START seconds
	  -t LENGTH	Convert duration in seconds
	EOF
}
while getopts a:c:f:i:Nnp:s:T:t:w: i; do
  case $i in
    a)	aspect=$OPTARG ;;
    c)	crf=$OPTARG ;;
    f)	formats=`echo $OPTARG | tr , ' '` ;;
    i)	index=$OPTARG ;;
    N)	nohtml=1 ;;
    n)	noexec=1 ;;
    p)	prefix=$OPTARG ;;
    s)	start=$OPTARG ;;
    t)	length=$OPTARG ;;
    T)	title=$OPTARG ;;
    w)	pxw=$OPTARG ;;
    \?)	echo "Unknown option $i" >&2; usage; exit 1 ;;
  esac
done
shift $((OPTIND - 1))

if [ -z "$1" ]; then
  usage; exit 0
fi
ohtml="$tmpd/$index"
for i in "${prefix}hoge" "$index" "$ohtml"; do
  d=`dirname $i`
  [ -d $d ] || mkdir $d || {
	echo "Cannot create dir[$prefix].  Abort." >&2
	exit 2
      }
done
ffopts="${start:+ -ss $start}${length:+ -t $length}"
ffopts2='-movflags +faststart'
doit=${noexec:+"echo"}

# Create index.html if none
[ -n "$nohtml" ] || [ -s "$index" ] || htmlheader > $index

for i; do
  exec 3> $ohtml
  fnroot=${i%.*}
  fposter=$fnroot.jpg	pospos=""
  case $i in
    *:[0-9]*)		# foo.mov:5 -> Make poster at 5s position of foo.mov
      pospos=${i##*:}
      i=${i%:*}
      ;;
  esac
  info=`$ffmpeg -i $i 2>&1 | grep '^ *Stream.*Video' | head -1`
  echo info=$info
  geom=`expr x"$info" : ".* \([0-9][0-9]*x[0-9][0-9]*\)[^0-9].*"`
  echo geom=$geom
  if [ -z "$geom" ]; then
    echo Cannot get video information from "[$i]">&2
    continue
  fi
  origw=${geom%%x*} origh=${geom##*x}
  pxh=`echo "0.5 + $pxw/$origw*$origh" | bc -l`
  pxh=${pxh%%.*}		# Trim after decimal point
  case $pxh in
    *[13579]) pxh=$((pxh+1)) ;;	# Make it even number
  esac
  geomspec="-s $pxw:$pxh"
  [ -n "$pospos" ] &&
      $doit $ffmpeg -y -ss $pospos -i $i -ss 0 -frames 1 $geomspec $fposter
  if [ -s "$fposter" ]; then
    poster=" poster=\"$fposter\""
  else
    poster=""
  fi
  cat<<-EOF							>&3
	<!-- $mark_b:$i *DO NOT DELETE THIS LINE* この行は残してください -->
	<video$poster controls>
	EOF
  for f in $formats; do
    o=$prefix${fnroot}.$f
    [ x"$i" = x"$o" ] && { echo InputFile==OutputFile, skipped >&2; continue;}
    if [ "$o" -nt "$i" ]; then
      : output file is newer or above condition fails - $o does not exists
    else
      $doit $ffmpeg$ffopts -y -i $i -crf "$crf" $geomspec $ffopts2 $o
    fi
    echo "<source src=\"$o\">"					>&3
  done
  cat<<-EOF							>&3
	</video>
	<!-- $mark_e:$i *DO NOT DELETE THIS LINE* この行は残してください -->
	EOF
  exec 3>&-			# Close temporary file
  [ -n "$nohtml" ] && continue
  #
  # Modify HTML index file
  if fgrep "$mark_b:$i" $index >/dev/null 2>&1; then
    # Replace between _MovStart_ to _MovEnd_
    cat<<-EOF | ed $index
	1
	/$mark_b:$i/
	ka
	.+1,/$mark_e:$i/d
	-1r $ohtml
	'a
	d
	w
	q
	EOF
  elif fgrep "$mark_eom" $index >/dev/null 2>&1; then
    # Insert before EOM marker
    cat<<-EOF | ed $index
	/$mark_eom/-1r $ohtml
	w
	q
	EOF
  else
    cat $ohtml >> $index
  fi
done
[ -z "$nohtml" ] && if ! fgrep "</body>" $index >/dev/null 2>&1; then
  cat<<-EOF >> $index
	<!-- $mark_eom -->
	</body>
	</html>
	EOF
fi
# cat $ohtml