Skip to content

Instantly share code, notes, and snippets.

@kanreisa
Last active August 29, 2015 14:16
Show Gist options
  • Save kanreisa/ca5f500a459fa7a40c18 to your computer and use it in GitHub Desktop.
Save kanreisa/ca5f500a459fa7a40c18 to your computer and use it in GitHub Desktop.
mp4 encode utils
#!/bin/bash
ATAG="fdkaac-he96ab"
# BDAV MPEG-2 TS something audio -> HE-AAC v1 audio in MPEG-4
avconv -y -i $1 \
-c:a libfdk_aac -profile:a aac_he -b:a 96k -afterburner 1 \
-f mp4 $1.$ATAG.mp4
#!/bin/bash
EXT="mp4"
STAG="BD"
PTAG="1080p-x264-q18"
ATAG="fdkaac-he96ab"
VTAG="r15"
# BDAV MPEG-2 TS audio track -> mp4 audio
avconv -y -i $1 -vn -map 0:a:0 \
-c:a libfdk_aac -profile:a aac_he -b:a 96k -afterburner 1 \
-f mp4 $1.tmp.a.mp4
# BDAV MPEG-2 TS H.264 video track -> mp4 H.264 video
avconv -threads 6 -y -i $1 -c:v libx264 \
-ssim 1 -tune ssim -crf 18 -bluray-compat 1 \
-x264-params b-adapt=2:me=umh:merange=48:subme=10:trellis=2:ref=12:bframes=5:analyse=all:b-pyramid=strict \
-f mp4 $1.tmp.v.mp4 2>&1 | tee $1.tmp.log
SSIM=`cat "$1.tmp.log" | grep SSIM | sed -e "s/^.*Mean Y:\([0-9.]*\).*$/\1/"`
# mux
MP4Box -add "$1.tmp.v.mp4"#video -add "$1.tmp.a.mp4"#audio -new "$1_${STAG}_${PTAG}-SSIM=${SSIM}_${ATAG}_${VTAG}.$EXT"
# clean up
rm -fv $1.tmp.v.mp4 $1.tmp.a.mp4 $1.tmp.log
#!/bin/bash
STAG="BD"
PTAG="1080p-x264-q18"
VTAG="r15"
# BDAV MPEG-2 TS H.264 video track -> mp4 H.264 video
avconv -threads 6 -y -i $1 -c:v libx264 \
-ssim 1 -tune ssim -crf 18 -bluray-compat 1 \
-x264-params b-adapt=2:me=umh:merange=48:subme=10:trellis=2:ref=12:bframes=5:analyse=all:b-pyramid=strict \
-f mp4 $1.mp4 2>&1 | tee $1.tmp.log
SSIM=`cat "$1.tmp.log" | grep SSIM | sed -e "s/^.*Mean Y:\([0-9.]*\).*$/\1/"`
mv -v $1.mp4 $1_${STAG}_${PTAG}-SSIM=${SSIM}_${VTAG}.mp4
# clean up
rm -fv $1.tmp.log
#!/bin/bash
# Japanese ISDB-T/ISDB-S MPEG-2 TS -> mp4 H.264 video+audio
# コメンタリートラックがあればあとでmuxすべし
# 音ズレ対策が面倒なので自分の環境用にビルドしたHandBrakeを使う
ENCODER="HandBrakeCLI"
EXT="mp4"
PTAG="r12f18"
ONAME="$1_$PTAG.$EXT"
OPTS='-t 1 -c 1 -f mp4 -4 -O --denoise="2:1.5:3:2.25" \
-w 1280 -l 720 --crop 0:8:0:0 --modulus 2 -e x264 -q 18 \
-a 1 -E faac -6 stereo -R Auto -Q 128 -D 0 \
-x b-adapt=2:me=umh:merange=64:subq=10:trellis=2:ref=12:bframes=6:analyse=all:b-pyramid=strict:deblock=2,2 --x264-tune ssim --verbose=1'
$ENCODER $OPTS -i "$1" -o "$ONAME"
#!/bin/bash
# cron 用スクリプト (+SSIM記録)
dir="/path/to/encode/pool/"
fproc="dtvts_to_mp4"
fext="ts"
lext="bak"
eext="mp4"
vtag="r12f18"
# check process
proc=`ps cax | grep -c ${fproc}`
if [ ${proc} -gt 2 ]; then
exit 0;
fi
# cd
cd ${dir}
# find
fns=`ls *.${fext} 2> /dev/null | grep ${fext}`
if [ "${fns}" = "" ]; then
exit 0;
fi
# lock-on
for fn in ${fns}; do
mv "${fn}" "${fn}.${lext}"
~/dtvts_to_mp4 "${fn}.${lext}" > "${fn}.log" 2>&1
# get SSIM
SSIM=`cat "${fn}.log" | grep SSIM | sed -e "s/^.*Mean Y:\([0-9.]*\).*$/\1/"`
rename "s/.${eext}/_SSIM\=${SSIM}.${eext}/" "${fn}.${lext}_${vtag}.${eext}"
# rename "s/.m2ts.ts//" "${fn}.${lext}_${vtag}_SSIM\=${SSIM}.${eext}"
break
done
#!/bin/bash
# mux
MP4Box -add "$1" -add "$2" -new "$1.tmp.mp4"
mv -v "$1" "$1.bak.mp4"
mv -v "$1.tmp.mp4" "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment