You are not logged in.

#1 2008-09-25 20:12:58

ahaslam
Member
Registered: 2007-05-11
Posts: 119

Mediatomb scripts & MP4 encoding

I've been testing some different settings for a while & thought someone else may as well benefit too.

Here's my script to stream the main title from a DVD image without mounting or transcoding :

#!/bin/bash

TITLE=`lsdvd "$1" | grep Longest | cut -d":" -f2`
OPTS="-of mpeg -oac copy -ovc copy -really-quiet"

mencoder dvd://"$TITLE" -dvd-device "$1" ${OPTS} -o "$2"

There are a few scripts out there that transcode h264/ac3 mkv's to mpeg's but they tend to lack quality. Here's mine:

#!/bin/bash

mplayer -identify -frames 0 "$1" 2>/dev/null > /tmp/$$
IAC=`grep ID_AUDIO_FORMAT /tmp/$$ | cut -d"=" -f2`
IFPS=`grep ID_VIDEO_FPS /tmp/$$ | cut -d"=" -f2`
rm /tmp/$$

echo  ${IAC} ${IFPS}

if [ ${IAC} = 8192 ]; then  # (AC3)
    OAC="-oac copy"
    else
    OAC="-oac lavc"
    AOPTS="acodec=ac3:abitrate=256"
fi 

if [ ${IFPS} = "23.976" ]; then
    FPS="-ofps 24000/1001"
    elif [ ${IFPS} = "29.970" ]; then
    FPS="-ofps 30000/1001"
    else
    FPS="-ofps ${IFPS}"    
fi

echo ${OAC} ${AOC} ${AOPTS} ${FPS}

mencoder "$1" ${OAC} -ovc lavc -of mpeg ${FPS} -lavcopts vcodec=mpeg2video:\
threads=4:keyint=12:vqscale=2:vrc_maxrate=16000:vrc_buf_size=2560:mbd=2:${AOPTS} \
-mpegopts muxrate=19200:tsaf -noskip -mc 2 -vf scale=1280:-11,harddup -o "$2"

That's about as good as it gets if you're streaming via wireless. If you are plugged into a LAN you could raise/remove scale & maxrate. If you're struggling to transcode in real-time, remove 'mbd=2' & play with threads & vqscale.

Buffer sizes are also important when you get close to your performance limits, my config is pasted here: http://pastebin.com/fa92ba5

Feel free to share your scripts, config's & anything else. I hope this saves someone some time & frustration. If nothing else, it'll serve as a personal backup. wink

Last edited by ahaslam (2008-10-26 20:06:52)

Offline

#2 2008-10-25 21:02:45

ahaslam
Member
Registered: 2007-05-11
Posts: 119

Re: Mediatomb scripts & MP4 encoding

Media tomb seems great at 1st, but small issues really end up bugging me. I now encode content that can't be remuxed to an AAC/AVC mp4. It's far from real-time tho the result is worth it.

To make the process easier I created a simple script to calculate the bitrate, scale & encode. Everything should be self explanatory, just note that the PS3 & 360 can't play MP4's > 4096MB. The target file size should come in slightly under what's specified, so no need to undershoot.

#!/bin/bash

#Script to calculate bitrate, scale & encode to aac/avc mp4
#uses ffmpeg presets within ~/.ffmpeg (-vpre fast/-vpre vhq)
#examples: http://svn.mplayerhq.hu/ffmpeg/trunk/ffpresets
#depends: mplayer, ffmpeg, x264 & faac
#use: ./mp4 inputFile requiredSize(MB)

IN="$1"
MB="$2"

info() {
mplayer -identify -frames 0 "$IN" 2>/dev/null > /tmp/$$
RESX=`grep ID_VIDEO_WIDTH /tmp/$$ | cut -d"=" -f2`
RESY=`grep ID_VIDEO_HEIGHT /tmp/$$ | cut -d"=" -f2`
LENGTH=`grep ID_LENGTH /tmp/$$ | cut -d"=" -f2`
rm /tmp/$$ && echo ${RESX} ${RESY} ${LENGTH}
}

height() {
ASPECT=$(echo "scale=3; ${RESX} / ${RESY}" | bc)
    
if [ "${RESX}" -ge "1280" ]; 
then
    HEIGHT=$(echo "1280 / ${ASPECT}" | bc)
    MOD16=$(( ${HEIGHT} / 16 * 16 ))
else
    MOD16=$(( ${RESY} / 16 * 16 ))    
fi
echo ${MOD16}
}

audioSize() {
ffmpeg -i "$IN" -vn -acodec libfaac -ac 2 -aq 200 -async 2 "$IN.m4a"
STAT=$(stat "$IN.m4a" | grep Size | awk '{print $2}')
SIZE=$( echo "scale=6; ${STAT} / 1024" | bc)
echo ${SIZE}
}

bitrate() {
RATE=$(echo "(( "$MB" * 1024 ) / ${LENGTH}) - ( ${SIZE} / ${LENGTH})" | bc)
R8=$(echo "(( ${RATE} * 8 ) * 1.02)" | bc)
KBPS=`echo "tmp=${R8}; tmp /= 1; tmp" | bc`
echo ${KBPS}
}

encode() {
ffmpeg -i "$IN" -an -pass 1 -s 1280x${MOD16} -vcodec libx264 -vpre fast \
-b ${KBPS}k -threads 0 -rc_eq 'blurCplx^(1-qComp)' -level 41 "$IN.mp4"
ffmpeg -i "$IN" -acodec libfaac -ac 2 -aq 200 -async 2 -pass 2 \
-s 1280x${MOD16} -vcodec libx264 -vpre vhq -b ${KBPS}k -threads 0 \
-rc_eq 'blurCplx^(1-qComp)' -level 41 -psnr -y "$IN.mp4"
}

info;
height;
audioSize;
bitrate;
encode;
rm *.m4a *.log && echo Finished!

Hope it comes in handy for someone. If nothing else it'll serve as a personal backup again. wink

Last edited by ahaslam (2008-10-31 18:02:16)

Offline

#3 2008-10-27 19:18:18

ahaslam
Member
Registered: 2007-05-11
Posts: 119

Re: Mediatomb scripts & MP4 encoding

Ffmpeg presets

What's used here can impact the bitrate required to achieve the specified size. Line 42 of my script may need editing if different presets are used. For example, if no bframes are used you'd certainly need to reduce '1.02' closer to 1.00. If different presets are used frequently, either remove this modifier or specify a preset on the command line & have it change accordingly.

libx264-fast.ffpreset:

coder=1
flags=+loop
cmp=+chroma
partitions=-parti8x8-parti4x4-partp8x8-partp4x4-partb8x8
me_method=dia
subq=1
me_range=16
g=480
keyint_min=24
sc_threshold=40
i_qfactor=0.71
b_strategy=2
qcomp=0.6
qmin=10
qmax=51
qdiff=4
bf=3
refs=1
directpred=1
trellis=0
flags2=-bpyramid-wpred-mixed_refs-dct8x8+fastpskip


libx264-vhq.ffpreset:

coder=1
flags=+loop
cmp=+chroma
partitions=+parti8x8+parti4x4+partp8x8+partp4x4+partb8x8
me_method=umh
subq=9
me_range=32
g=480
keyint_min=24
sc_threshold=40
i_qfactor=0.71
b_strategy=2
qcomp=0.6
qmin=10
qmax=51
qdiff=4
bf=3
refs=6
directpred=3
trellis=2
flags2=+bpyramid+wpred+mixed_refs+dct8x8-fastpskip

PS. These presets will only work with x264 & ffmpeg > 20081002 (a highly recommended update).
</blog>

Last edited by ahaslam (2008-10-27 19:38:18)

Offline

Board footer

Powered by FluxBB