Skip to content

Instantly share code, notes, and snippets.

@dokker
Created November 25, 2011 13:18
Show Gist options
  • Save dokker/1393508 to your computer and use it in GitHub Desktop.
Save dokker/1393508 to your computer and use it in GitHub Desktop.
Convert video for meizu m6 player
#!/bin/bash
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of the nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Copyleft 2007, 2008 wishmechaos
#
# Version 0.1, 21-oct-07
# Version 0.2, 22-oct-07, Added ratio autodetect (requires ffmpeg),
# small subtitle fix.
# routine from http://tuxicity.wordpress.com/2006/12/01/avi-to-dvd-with-ffmpeg-and-dvdauthor/
# Version 0.2.1, 12-feb-08, Fixed small bug when filename has spaces.
# Version 0.3, 1-mar-08, Tuned mencoder parameters.
# Version 0.4, 20-mar-08, fixed double pass and subtitles loading
# version 0.5, 17-oct-08, detect ratio using file rather than ffmpeg,
# dropping requirement for ffmpeg
# For debugging purposes
# set -x
### Defaults
# Frames per second (15-20)
fps=20
# Two pass encoding, takes longer, better quality
# yes / no
twopass=no
# Crop settings.
# "auto" will try to detect if the source video is 4:3 or 16:9
# and leave that intact.
# "yes" will leave 4:3 videos as they are, and will crop 16:9
# videos in order to take up all of the screen.
crop="auto"
# Video bitrate
vbitrate=350
# Audio bitrate
abitrate=112
# Sample rate
srate=44100
# Maximum Bitrate allowed
vrc_maxrate=460
# Buffer size
vrc_buf_size=1835
# Brightness
bright=""
### End defaults
# Some common options
common="-idx -noodml -force-avi-aspect 1.3333 -msglevel mencoder=1 -ofps $fps -ovc lavc -ffourcc XVID"
show_help() {
echo "Usage: `basename "$0"` [-h(elp)] [-c(rop) auto yes] [-p (two-pass)] [-f(ps) number]
[-v(ideo bitrate) number] [-a(udio bitrate) number] file1.avi [file2.avi...]
Subtitles are automatically loaded if their filename is the same as the movie file"
exit 1
}
# find subtitles with same filename
findsubs() {
for possiblefile in "$filename"*; do
case $possiblefile in
*.sub) subfile="$possiblefile";;
*.srt) subfile="$possiblefile";;
esac
done
if test -n "$subfile"; then #if [[ "$subfile" != "" ]]; then
echo "Found $subfile."
subs=(-utf8 -noautoexpand -subpos 95 -subfont-autoscale 3 -subfont-outline 1 -subcp enca -sub "$subfile")
fi
}
aspectdetect() {
aspect=$(( $(file "$moviefile" | sed "s/.* \([1-9][0-9]*\) x \([0-9]*\).*/\10000 \/ \2/") ))
if [[ $aspect -lt 15555 ]]; then
# 4:3
echo "$moviefile appears to be 4:3"
vf="scale=-2:240,crop=320:240,expand=320:240:::1"
else
# 16:9
echo "$moviefile appears to be Widescreen"
vf="scale=320:-2,crop=320:240,expand=320:240:::1"
fi
}
# Convert function
convert() {
case $crop in
auto) aspectdetect;;
yes) vf="scale=-2:240,crop=320:240,expand=320:240:::1"
esac
if [[ "$twopass" == "no" ]]; then
mencoder "${subs[@]}" -vf ${bright}softskip,$vf,rotate=1 "$moviefile" $common -lavcopts threads=8:vcodec=mpeg4:mbd=2:trell:vbitrate=$vbitrate:vrc_maxrate=$vrc_maxrate:vrc_buf_size=$vrc_buf_size:vmax_b_frames=0:vhq:acodec=libmp3lame:abitrate=$abitrate:keyint=100:sc_factor=5 -sws 9 -srate $srate -oac lavc -af volnorm,delay=230:230 -o "$filename.meizu.avi"
else
#pass 1
mencoder -nosound -vf ${bright}softskip,$vf,rotate=1 "$moviefile" $common -lavcopts threads=8:vcodec=mpeg4:vpass=1:vbitrate=$vbitrate:vrc_maxrate=$vrc_maxrate:vrc_buf_size=$vrc_buf_size:vmax_b_frames=0:vhq:turbo -sws 9 -o "$filename.meizu.avi"
#pass 2
mencoder "${subs[@]}" -vf ${bright}softskip,$vf,rotate=1 "$moviefile" $common -lavcopts threads=8:vcodec=mpeg4:mbd=2:trell:vpass=2:vbitrate=$vbitrate:vrc_maxrate=$vrc_maxrate:vrc_buf_size=$vrc_buf_size:vmax_b_frames=0:vhq:acodec=libmp3lame:abitrate=$abitrate:keyint=100:sc_factor=5 -sws 9 -srate $srate -oac lavc -af volnorm,delay=230:230 -o "$filename.meizu.avi"
rm divx2pass.log
fi
echo " Finished converting $moviefile"
}
cleanUp() {
rm "$filename.meizu.avi"
rm divx2pass.log
exit 1
}
trap cleanUp INT
# If there're no arguments
if [ $# = 0 ]; then show_help; fi
# Parse arguments
x=1 # Avoids an error if we get no options at all.
while getopts "hc:pf:b" opt; do
case "$opt" in
h|\?) show_help;;
c) crop="$OPTARG";;
p) twopass="yes";;
f) fps="$OPTARG";;
a) abitrate="$OPTARG";;
v) vbitrate="$OPTARG";;
# eq2=gamma:contrast:brightness:saturation
# gamma 0.1 - 10 (1.0), contrast -2 − 2 (1.0), brightnes -1 - 1 (0.0), saturation 0 − 3 (1.0)
b) bright="eq2=1.0:1.2:0.2:1.0,";;
esac
x=$OPTIND
done
shift $((x-1))
for moviefile in "$@"; do
if [[ -f "$moviefile" ]]; then
echo "Moviefile is $moviefile"
extension=".${moviefile##*.}"
echo "Extension is $extension"
filename="`basename "$moviefile" "$extension"`"
echo "Filename is therefore "$filename""
# ugly way to do it, works though.
if [[ "$moviefile" != "${moviefile%/*}" ]]; then cd "${moviefile%/*}"; fi
findsubs
echo "Converting $moviefile"
convert
else
echo ""$moviefile" doesn't exist."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment