Yet Another Mencoder Script
Video and Imaging
|
03-02-2008
|
#1 (permalink)
|
Passing By

Join Date: Oct 2007
Posts: 18
|
Yet Another Mencoder Script
Since it was kinda hard to find it in the old thread, I've decided to give my script its own one. This is a script to transcode videos for the Meizu M6.
To use, copy the code and paste it to a text file, save it somewhere (it'd be a good idea to save it in your $PATH) and make executable (chmod +x filename). Run without parameters for help. You can feed it any amount of files and it'll transcode them to the same directory, appending ".meizu.avi" to the name. If it finds any SRT or SUB files with the same name as the video file, it'll embed them in the video.
It requires mencoder and ffmpeg for aspect ratio detection.
Code:
#!/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 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.3.1, 8-apr-08, Added color for messages. Warn if conversion fails.
# Warn is ffmpeg/mencoder is not installed or failed.
# For debugging purposes
# set -x
### Defaults
# Frames per second (15-20)
fps=20
# Two pass encoding, takes longer, better quality
# yes / no
twopass=yes
# 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"
# Turn colored messages on/off.
color=on
# Video bitrate
vbitrate=350
# Audio bitrate
abitrate=112
# Sample rate
srate=44100
# Maximum Bitrate allowed
vrc_maxrate=460
# Buffer size
vrc_buf_size=1835
### End defaults
# Some common options
common="-idx -noodml -force-avi-aspect 1.3333 -msglevel mencoder=1 -ofps $fps -ovc lavc -ffourcc XVID"
if [[ "$color" = "on" ]]; then
RED=$(tput setaf 1;tput setab 0)
GREEN=$(tput setaf 2;tput setab 0)
BLUE=$(tput setaf 6;tput setab 0)
NORM=$(tput sgr0)
fi
show_help() {
echo "Usage: `basename "$0"` [options] file1.avi [file2.avi...]
Options are
-h (show this)
-c (crop) auto|yes
-p (two-pass) yes|no
-f (fps) 15-20
-v (video bitrate) number
-a (audio bitrate) number
Subtitles are automatically loaded if their filename is the same as the movie file."
exit 1
}
if ! hash mencoder 2>/dev/null; then
echo " ${RED}Couldn't find mencoder. Please install it.${NORM}
${BLUE}In Debian/Ubuntu, type \"sudo apt-get install mencoder\"${NORM}"
exit 1
fi
# find subtitles with same filename
findsubs() {
echo " ${BLUE}Searching for subtitles.${NORM}"
for possiblefile in "$filename"*; do
case $possiblefile in
*.sub) subfile="$possiblefile";;
*.srt) subfile="$possiblefile";;
esac
done
if test -n "$subfile"; then
echo " ${GREEN}Found \"$subfile\"${NORM}"
subs=(-noautoexpand -subpos 95 -subfont-autoscale 3 -subfont-outline 1 -subcp enca -sub "$subfile")
else
echo " ${BLUE}No subtitles found.${NORM}"
fi
}
aspectdetect() {
if ! hash ffmpeg 2>/dev/null; then
echo " ${RED}Couldn't find ffmpeg. Please install it.${NORM}
${BLUE}In Debian/Ubuntu, type \"sudo apt-get install ffmpeg\"${NORM}"
exit 1
fi
aspect=$(( $(ffmpeg -i "$moviefile" 2>&1 | grep "Stream\ #0.0" | sed "s/.* \([1-9][0-9]*\)x\([0-9]*\).*/\10000 \/ \2/") ))
if [[ $aspect -eq 0 ]]; then
echo " ${RED}Either ffmpeg is not working correctly or it couldn't stat your video file.${NORM}"
echo " ${RED}I'm assuming that crop=yes for this file${NORM}"
vf="scale=-2:240,crop=320:240,expand=320:240:::1"
elif [[ $aspect -lt 15555 ]]; then
# 4:3
echo " ${GREEN}\"$moviefile\" appears to be 4:3${NORM}"
vf="scale=-2:240,crop=320:240,expand=320:240:::1"
else
# 16:9
echo " ${GREEN}\"$moviefile\" appears to be Widescreen${NORM}"
vf="scale=320:-2,crop=320:240,expand=320:240:::1"
fi
}
return_error() {
echo "${RED} Mencoder failed for some reason. Check your logs.${NORM}"
cleanUp
}
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 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" || return_error
else
#pass 1
mencoder -nosound -vf 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" || return_error
#pass 2
mencoder "${subs[@]}" -vf 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" || return_error
rm divx2pass.log
fi
echo "${GREEN} Finished converting $moviefile ${NORM}"
}
cleanUp() {
echo " ${RED}Deleting temporary files.${NORM}"
rm "$filename.meizu.avi" >/dev/null 2>&1
rm divx2pass.log >/dev/null 2>&1
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:" opt; do
case "$opt" in
h|\?) show_help;;
c) crop="$OPTARG";;
p) twopass="$OPTARG";;
f) fps="$OPTARG";;
a) abitrate="$OPTARG";;
v) vbitrate="$OPTARG";;
esac
x=$OPTIND
done
shift $((x-1))
for moviefile in "$@"; do
if [[ -f "$moviefile" ]]; then
echo " ${BLUE}Moviefile is \"$moviefile\"${NORM}"
extension=".${moviefile##*.}"
echo " ${BLUE}Extension is $extension${NORM}"
filename="`basename "$moviefile" "$extension"`"
echo " ${BLUE}Filename is therefore \""$filename"\"${NORM}"
# ugly way to do it, works though.
if [[ "$moviefile" != "${moviefile%/*}" ]]; then cd "${moviefile%/*}"; fi
findsubs
echo " ${GREEN}Converting \"$moviefile\"${NORM}"
convert
else
echo " ${RED}\""$moviefile"\" doesn't exist.${NORM}"
fi
done
If you use Gnome and want quick access, you can place a little helper script in ~/gnome2/nautilus-scripts and it'll add an option to your right-click menu. In my case, the main script is called "meizu-convert" and it's placed in my $PATH (/usr/local/bin in this case). Change it to whatever you called the main script, and if it's not in your path, be sure to add the full path (as in /home/me/scripts/nameofmainscript). It's a bit redundant, but I couldn't think of a better way to do it.
Code:
#!/bin/sh
for moviefile in "$@"; do
gnome-terminal -x meizu-convert "$moviefile"
done
Hope it's useful. If you spot any errors or have any suggestions, please let me know.
Last edited by wishmechaos : 04-08-2008 at 08:30 AM.
Reason: new version
|
|
|
03-10-2008
|
#2 (permalink)
|
Passing By

Join Date: Jan 2008
Posts: 7
|
that's intresting.
have you found that bitrate=460 is the maximum the meizu m6 can play? i've been trying to find the maximum without skipping.
|
|
|
03-10-2008
|
#3 (permalink)
|
Passing By

Join Date: Oct 2007
Posts: 18
|
Nope, it's just BatMan's Q4, which I find good enough.
Cheers.
|
|
|
03-10-2008
|
#4 (permalink)
|
Member

Join Date: Nov 2007
Posts: 108
|
Quote:
Originally Posted by lambchops468
that's intresting.
have you found that bitrate=460 is the maximum the meizu m6 can play? i've been trying to find the maximum without skipping.
|
460 or so is very close to what the M6 cpu can handle without at first frame droping, or worse having some short image freezes
the M6 cpu is quite low in power , so you really never know how many frames are truely decoded and screened from such a 460kbits movie; *only* a fast shutter apn and multiple time consuming tests could tell
that is , most probably, the frame 'fluctuates' ; and if this goes too far it ends in a sensation of not fluent smooth/even frame rate
not to forget that BobbyQ ( Batman's author) believed , having used of a max bit rate parameter , he had set some "clip" value for the bit rate; but indeed he was wrong ; this I proved this in some 'contest' where the Q5 profile would have problems , this due to peak bit rate overshoots quite beyond the max value he believed having set in Q5 (but also to more complex issues)
Batman 2008 ..stutters !
you can see that the Q5 Batman 'demo' movie *has* peak bit rates up to 800kbits ; which means =>
in the end --the codec-- rules ! ( with VBR and a target bit rate)
and , imho, as for a correct bit rate value with the M6 , there is very little interest , if any, going up to 460kbps ; keeping to 350k will deliver just about the very same quality on the tiny 2.4" M6 screen
I like Batman for simple straight simple encoding , but I keep with VDub (and XviD) for more precise things ; especially when it comes to decisions on cropings ; for instance if you have to remove garbage side bars on video stuff , or have to crop/resize 2:35 clean film source to M6 4/3 ratio
the other more difficult issue of encoding for the M6 is due to the overboosted lcd screen ; this can cause blacks turn as dark greys ; and then compression blockiness/pixelation becomes quite visible in these dark areas :/ ; and only shifting up the black level when encoding can be no cure at all sometimes ...
wishmechaos >> Nope, it's just BatMan's Q4, which I find good enough.
definitely 
|
|
|
03-27-2008
|
#5 (permalink)
|
Passing By

Join Date: Mar 2008
Posts: 4
|
Thanks for the script givemechaos
Before I tried yours I tried to add a single item to Nautilus-actions with mencoder and this code
Code:
%M -idx -noodml -ofps 20 -vf scale=320:-2,expand=:240:::1,crop=320:240,rotate=1 -ovc lavc -ffourcc XVID -lavcopts vcodec=mpeg4:vbitrate=384:vmax_b_frames=0:vhq -sws 9 -srate 44100 -oac mp3lame -lameopts cbr:br=192:mode=0 -o %M[mz].avi
It encoded my .avi files all right, but the sound was bad with all the files I tried.
So I was eager to try your script, especially with the .SRT embedding thing, and gave it a try yesterday but didn't manage anything.
I am still a newbie with linux, it took me more than an hour :eek: to make those first 2 scripts, make them executable and so on.
So I made my main script after yours, made it executable and put it in my Documents folder. (I didn't understand what you call "my $PATH", I use the latest Ubuntu)
Then I created the second helper script in ~/gnome2/nautilus-scripts, made it executable and carefully added the full path. For me :
Code:
#!/bin/sh
for moviefile in "$@"; do
gnome-terminal -x /home/rem/Documents/meizuconvert.sh "$moviefile"
done
So when I right-click a video file, I do have the "script" menu with its "meizuconvert.sh" item but nothing happens when I launch it.
Obviously I must have missed something, so any help appreciated, I just *hate* having to reboot win to use Virtualdub
|
|
|
03-27-2008
|
#6 (permalink)
|
Passing By

Join Date: Oct 2007
Posts: 18
|
It's weird that it doesn't work, since it seems you followed every step correctly. $PATH is a global variable that says where should the system look for executables. You can check it in a terminal by typing 'echo $PATH'; putting the script in any folder in your $PATH makes it possible to just type 'meizu-convert' (or whatever you called it) instead of '/home/me/blah/script'. In my case, I have a folder called 'bin' in my home, and I added ~/bin to my $PATH (edit ~/.bashrc and add 'PATH=$PATH:~/bin', or if there's already a PATH= line, just add :~/bin to it)
Anyway, the helper script isn't essential. Just open a terminal, type '/home/rem/Documents/meizuconvert.sh ' (notice the trailing space) and drag and drop the video files from the file manager.
Cheers
|
|
|
03-29-2008
|
#7 (permalink)
|
Passing By

Join Date: Mar 2008
Posts: 4
|
Thanks for the quick answer !
Here's what happens when I do what you suggest :
Code:
rem@ubuntu-rem:~$ '/home/rem/Documents/meizuconvert.sh ''/home/rem/Vidéos/Breaking Bad/Breaking.Bad.S01E04.avi' '/home/rem/Vidéos/Breaking Bad/Breaking.Bad.S01E04.DSR.XviD-BayHarborButcher.srt'
bash: /home/rem/Documents/meizuconvert.sh /home/rem/Vidéos/Breaking Bad/Breaking.Bad.S01E04.avi: Aucun fichier ou répertoire de ce type
rem@ubuntu-rem:~$
The french bit means : no such file or directory
I still feel dim witted with the terminal, but I have to ask and improve...
|
|
|
03-29-2008
|
#8 (permalink)
|
Passing By

Join Date: Oct 2007
Posts: 18
|
Quote:
Originally Posted by DrScholl
Code:
rem@ubuntu-rem:~$ '/home/rem/Documents/meizuconvert.sh ''/home/rem/Vidéos/Breaking Bad/Breaking.Bad.S01E04.avi' '/home/rem/Vidéos/Breaking Bad/Breaking.Bad.S01E04.DSR.XviD-BayHarborButcher.srt'
bash: /home/rem/Documents/meizuconvert.sh /home/rem/Vidéos/Breaking Bad/Breaking.Bad.S01E04.avi: Aucun fichier ou répertoire de ce type
|
It seems you're quoting the entire command, and you're also feeding meizuconvert.sh the subtitle name. Subtitles are automatically detected when they have the same filename as the .avi, for simplicity's sake (in this case it seems they aren't, so you should rename the sub file to "Breaking.Bad.S01E04.srt").
The correct command would be (notice the quoting):
$ /home/rem/Documents/meizuconvert.sh ''/home/rem/Vidéos/Breaking Bad/Breaking.Bad.S01E04.avi"
(the filename would be dragged-and-dropped, double quotes or single quotes don't matter as long as it's consistent)
you'd put a ' in the start of the command, so bash (the command line software itself) interprets it as a whole file, which it doesn't find (it's searching for a file named "/home/rem/Documents/meizuconvert.sh /home/rem/Vidéos/Breaking Bad/Breaking.Bad.S01E04.avi")
cheers.
|
|
|
03-30-2008
|
#9 (permalink)
|
Passing By

Join Date: Mar 2008
Posts: 4
|
Thanks for your patient explanations.
Thanks to them I managed to launch the conversion by dragging the files like you said.
Mencoder went on for a while, but the result file has a wrong size (o byte) and can't be read.
Here's what the terminal says :
Code:
Please supply the text font file (~/.mplayer/subfont.ttf).
subtitle font: load_sub_face failed.
New_Face failed. Maybe the font path is wrong.in 101mb A-V:0.000 [326:0]
Please supply the text font file (~/.mplayer/subfont.ttf).
subtitle font: load_sub_face failed.
Writing index...27f (99%) 168.28fps Trem: 0min 101mb A-V:0.000 [326:0]
Writing header...
ODML: vprp aspect is 4:3.
MEncoder 2:1.0~rc1-0ubuntu13.2 (C) 2000-2006 MPlayer Team
CPU: AMD Athlon(tm) 64 X2 Dual Core Processor 4400+ (Family: 15, Model: 107, Stepping: 2)
CPUflags: Type: 15 MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 1
Compiled with runtime CPU detection.
success: format: 0 data: 0x0 - 0x15e01556
AVI file format detected.
VIDEO: [XVID] 624x352 24bpp 23.976 fps 993.5 kbps (121.3 kbyte/s)
==========================================================================
Opening audio decoder: [mp3lib] MPEG layer-2, layer-3
AUDIO: 48000 Hz, 2 ch, s16le, 128.0 kbit/8.33% (ratio: 16000->192000)
Selected audio codec: [mp3] afm: mp3lib (mp3lib MPEG layer-2, layer-3)
==========================================================================
SUB: Detected subtitle file format: subviewer
SUB: error opening iconv descriptor.
SUB: Read 1003 subtitles.
SUB: Adjusted 3 subtitle(s).
Opening video filter: [rotate=1]
Opening video filter: [expand w=320 h=240 osd=1]
Expand: 320 x 240, -1 ; -1, osd: 1, aspect: 0.000000, round: 1
Opening video filter: [crop w=320 h=240]
Crop: 320 x 240, -1 ; -1
Opening video filter: [scale w=320 h=-2]
Opening video filter: [softskip]
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
Selected video codec: [ffodivx] vfm: ffmpeg (FFmpeg MPEG-4)
==========================================================================
Audio LAVC, couldn't find encoder for codec libmp3lame.
Finished converting /home/rem/Vidéos/Bones.S03E01.HDTV.XviD-XOR.avi
The first 2 sentences were repeated many times.
|
|
|
03-30-2008
|
#10 (permalink)
|
Passing By

Join Date: Oct 2007
Posts: 18
|
The first lines probably mean you probably haven't installed mplayer-fonts. Try 'sudo apt-get install mplayer-fonts'
The other issue, I'm not so sure of. Maybe you're missing some codecs, or maybe a matter of naming conventions. Could you try opening the script with a text editor and replacing all instances of 'libmp3lame' with 'mp3lame'? There should only be 2.
Cheers.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
All times are GMT. The time now is 12:07 AM.
|