Skip to content

Instantly share code, notes, and snippets.

@geta6
Created March 27, 2014 14:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geta6/9808582 to your computer and use it in GitHub Desktop.
Save geta6/9808582 to your computer and use it in GitHub Desktop.
cronで回すと$TARGET内にある`.mp4` で終わらないファイルを自動でエンコードします
#!/bin/zsh
USER='nouser'
GROUP='nogroup'
TARGET='require to set'
function source
{
find $TARGET -type f \( ! -iname '.*' \) | sed -E 's/ */ /g' | grep -vE 'mp4$' | sort -t'/' -k 5nr -k 6n
}
function findproc
{
ps ax | grep -v grep | grep $1 | wc -l
}
function target
{
echo ${1} | while read LINE; do
echo ${LINE} | sed -E 's/\.[^.]*$/\.mp4/g' | sed -E 's/\.mp4\.mp4/\.mp4/g'
done
}
function encode
{
HandBrakeCLI -i ${1} -o ${2} \
-e x264 -r 24 -X 1280 --denoise="weak" -5 -O -m -2 -T \
-E faac -B 128 -R Auto -6 dpl2 \
-x level=31:bframes=0:cabac=0:keyint=250:min-keyint=25:\
merange=24:subq=6:ref=8:mixed-refs=1:qcomp=0.6:vbv-maxrate=1500:vbv-bufsize=2000:\
analyse=all:threads=0:me=umh:no-fast-pskip=1:deblock=-2,-2:no-dct-decimate=1:\
non-deterministic=1:weightb=0:8x8dct=0:cqm=flat:aq-strength=0:psy-rd=0,0
touch -mr ${1} ${2}
chown $USER:$GROUP ${2}
python tagfix.py ${2}
rm ${1}
}
if [[ ${1} = '-h' ]]; then
echo "Usage: handbrake [options]"
echo " NoOpt Encode head of queued movie."
echo " -a Display all queued movie."
echo " 'handbrake -a'"
echo " -x Execute encode manually."
echo " 'handbrake -x SRC DST'"
exit 0
fi
SRCS=`source`
DSTS=`target ${SRCS}`
if [[ ${1} = '-a' ]]; then
if [[ $DSTS = '' ]]; then
echo "Remaining 0"
else
WORKS=0
echo $DSTS | while read DST; do
if [[ 1 -eq `findproc $DST` ]]; then
WORKS=1
echo -e "> $DST"
else
echo -e " $DST"
fi
done
echo
echo "Remaining `echo ${DSTS} | wc -l`"
if [[ 0 -eq $WORKS ]]; then
echo "Not working."
else
ps alx | grep -v grep | grep HandBrakeCLI | awk '{printf ("PID:%d\tRSS:%s\tTIME:%s\n", $3,$8,$12)}'
fi
fi
exit 0
fi
if [[ ${1} = '-x' ]]; then
SRC=$2
DST=$3
else
SRC=`echo ${SRCS} | head -1`
DST=`echo ${DSTS} | head -1`
fi
[[ -z ${SRC} ]] && exit 1
[[ -z ${DST} ]] && exit 1
[[ ! 0 = `ps ax | grep HandBrake | grep -v grep | wc -l` ]] && exit 0
[[ -f ${DST} ]] && rm ${DST}
TIME=`date '+%s'`
encode ${SRC} ${DST} > /dev/null 2>&1
exit 0
#!/usr/bin/env python
#coding:utf-8
import os
import sys
import time
import datetime
from mutagen.mp4 import MP4
reload(sys)
sys.setdefaultencoding('utf-8')
if __name__ == '__main__':
# 1 trkn
# 2 disk
# 3 titl / genr
if len(sys.argv) < 2:
print 'tagfix [target]'
sys.exit()
orig = os.path.basename(sys.argv[1]).split(' ')
nums = orig[0]
try:
disk = int(nums.split('-')[0])
except:
disk = 99
try:
trkn = int(nums.split('-')[1])
except:
trkn = 0
titl = unicode(' '.join(orig[1:]).replace('.mp4', ''), 'utf-8', 'ignore')
mpeg = MP4(sys.argv[1])
for field in mpeg.keys():
if field in mpeg:
del mpeg[field]
mpeg['\xa9alb'] = titl
mpeg['\xa9ART'] = titl
mpeg['\xa9gen'] = titl
mpeg['aART'] = titl
mpeg['trkn'] = [(trkn, 0)]
mpeg['disk'] = [(disk, 0)]
mtime = datetime.datetime.fromtimestamp(int(os.path.getmtime(sys.argv[1]))).strftime('%Y%m%d%H%M')
mpeg.save()
os.system('touch -t "%s" "%s"' % (mtime, sys.argv[1]))
sys.exit(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment