Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dem2vid shell script
05-26-2009, 06:58 AM (This post was last modified: 06-01-2009 05:37 AM by lost.)
Post: #1
dem2vid shell script
Hi I'm Lost(prophets), dunno why it took me so long to come here. Here I am tho, hello everyone. :)

I thought I'd share my script that automates video capture & encoding on Linux (& Mac with a few tweaks). It works with the standard release from Alientrap, it may need editing if you use a distro specific package tho. One word of warning, you'll need a good 10GB of free space on your home dir, this'll be relinquished upon completion tho.

Code:
#!/bin/sh

#Script to capture demo, calculate bitrate, scale & encode to mp4
#uses ffmpeg presets within ~/.ffmpeg (-vpre fast/-vpre vhq)
#examples: http://svn.mplayerhq.hu/ffmpeg/trunk/ffpresets
#depends: faac, ffmpeg & x264 (>20081002 recommended)
#use: ./dem2vid yourDemo.dem requiredWidth requiredSizeInMB

DEM="$1"
WID="$2"
MB="$3"
DEST="${1%.*}.mp4"

capture() {
LOC=`locate nexuiz-linux-sdl.sh | tail -1`    
${LOC} -demo +cl_capturevideo 1 +cl_capturevideo_ogg 0 \
+cl_capturevideo_fps 24 +playdemo demos/${DEM}
VID=`find ~/.nexuiz/data/video/ -iname "dpvideo*.avi" -print | tail -1`
echo "Demo captured to ${VID}"
}

info() {    
ffmpeg -i ${VID} 2> /tmp/$$
TH=`grep Duration /tmp/$$ | cut -d":" -f2 | cut -c 2-3`
TM=`grep Duration /tmp/$$ | cut -d":" -f3`
TS=`grep Duration /tmp/$$ | cut -d":" -f4 | cut -c 1-5`
SEC=$(echo "( ${TH} * 3600 ) + ( ${TM} * 60 ) + ${TS}" | bc)
RESX=`grep Video: /tmp/$$ | grep -o ....x | tr -d 'x '`
RESY=`grep Video: /tmp/$$ | grep -o x.... | tr -d 'x,'`
rm /tmp/$$ && echo ${RESX} ${RESY} ${SEC}
}

height() {
ASPECT=$(echo "scale=3; ${RESX} / ${RESY}" | bc)
HEIGHT=$(echo "${WID} / ${ASPECT}" | bc)
MOD16=$(( ${HEIGHT} / 16 * 16 ))
echo ${MOD16}
}

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

encode() {
ffmpeg -i ${VID} -pass 1 -s ${WID}x${MOD16} -vcodec libx264 -vpre fast \
-b ${KBPS}k -threads 0 -rc_eq 'blurCplx^(1-qComp)' -level 41 -an "$DEST"
ffmpeg -i ${VID} -acodec libfaac -ac 2 -ab 128k -async 2 -pass 2 \
-s ${WID}x${MOD16} -vcodec libx264 -vpre vhq -b ${KBPS}k -threads 0 \
-rc_eq 'blurCplx^(1-qComp)' -level 41 -psnr -y "$DEST"
}

vbr() {
ffmpeg -i ${VID} -acodec libfaac -ac 2 -aq 100 -async 2 \
-s ${WID}x${MOD16} -vcodec libx264 -vpre vhq -crf 20 -threads 0 \
-rc_eq 'blurCplx^(1-qComp)' -level 41 -psnr "$DEST"
}

capture;
info;
height;
if [ -n "$3" ];
then
    bitrate;
    encode;
    rm *2pass*.log
else
    vbr;
fi
rm ${VID} && echo "Finished! Saved as ${DEST}"

My presets:

[b]libx264-fast.ffpreset:[/b]

Code:
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
[b]
libx264-vhq.ffpreset:[/b]

Code:
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

Example VBR encode (no file-size entered): [url]http://omploader.org/vMXFoMA/e1m6_768x432.mp4.html[/url]
^ It works for other games too. ;)

If you're not bothered by quality & compression then you may be content with Nexuiz's built in ogg/theora encoder. There's no need for ~10GB free space & requires no dep's:

Code:
#!/bin/bash

#simple script to encode a Nexuiz using the internal encoder
#use: ./dem2ogv yourDemo.dem hq/hd

DEM="$1"
CFG=~/.nexuiz/data/config.cfg

if [ "$2" == "hq" ]; then
    RESX="768"
elif [ "$2" == "hd" ]; then
    RESX="1280"
fi
echo ${RESX}

aspect() {
WIDTH=`grep vid_width $CFG | cut -d " " -f2 | tr -d '"'`
HEIGHT=`grep vid_height $CFG | cut -d " " -f2 | tr -d '"'`
ASPECT=$(echo "scale=3; ${WIDTH} / ${HEIGHT}" | bc)
echo ${WIDTH} ${HEIGHT} ${ASPECT}
}

height() {
RESY=$(echo "${RESX} / ${ASPECT}" | bc)
echo ${RESY}
}

capture() {
LOC=`locate nexuiz-linux-sdl.sh | tail -1`    
${LOC} -demo +cl_capturevideo_width ${RESX} +cl_capturevideo_height ${RESY} \
+cl_capturevideo 1 +cl_capturevideo_ogg 1 +playdemo demos/${DEM}
}

aspect;
height;
capture;
echo "Finished! Your video is saved under ~/.nexuiz/data/video/"

[b]Editing tips:[/b]

Add a soundtrack:

[i]ffmpeg -i tune.mp3 -i handsofgod.mp4 -vcodec copy -acodec copy newaudio.mp4[/i]

This uses a specified mp3 as the audio track & makes a new mp4, leaving your original untouched. No encoding is done here, it simply copies the streams.

Cut the video:

[i]ffmpeg -i newaudio.mp4 -t 65.4 -vcodec copy -acodec copy cut.mp4[/i]

-t is what does the trick here, in this example I've asked it to stop at 65.4 seconds. Again this simply copies the streams to a new file, keeping the original untouched & avoids re-encoding.

Edited example (VBR): [url]http://omploader.org/vMTdoMA/cut.mp4[/url]
^ Nexuiz this time ;)

Hope this has been of some help, if nothing else it's my introduction. For more examples see my Youtube page: [url]http://www.youtube.com/user/found666[/url]

Tony.
lost, proud to be a member of Nexuiz Ninjaz - Practicing the Ninja Art of Nexuiz since May 2009.
Find all posts by this user
Quote this message in a reply
05-26-2009, 07:15 AM (This post was last modified: 05-26-2009 07:17 AM by -z-.)
Post: #2
RE: dem2vid shell script
nice man, very cool of you to share :).

you know you can go a step further and integrate this with nautilus? if you put it in ~/.gnome2/nautilus-scripts -- you can pass $1 (the demo name) with a right click. For the other variables I'd suggest using something like [url=http://library.gnome.org/users/zenity/stable/zenity-text-entry-options.html.en]zenity[/url] to pass the other variables.

Then you can simply right click >> encode.

I have examples of this here: [url]http://github.com/z/maptoolz[/url]
Inspire your neighbor, they'll inspire you back.

[url=http://maps.nexuizninjaz.com]maps.nn[/url] | [url=http://pics.nexuizninjaz.com]pics.nn[/url] | [url=http://chat.nexuizninjaz.com]chat.nn[/url] | [url=http://toolz.nexuizninjaz.com/cvar]2.5 cvar browser[/url]
Visit this user's website Find all posts by this user
Quote this message in a reply
05-26-2009, 07:58 AM (This post was last modified: 06-01-2009 05:41 AM by lost.)
Post: #3
RE: dem2vid shell script
Cheers Z,

I don't use a full DE like Gnome, I keep it simple with openbox. Such custom actions would be a nice touch for things like (un)compressing files but requiring Zenity is taking it too far imo.

If anyone wants to do this, using my simple ogg/theora script with the hq preset would be more suitable. You could also be sure of no missing dep's & require no ffmpeg configuration.

Tony.

PS. You'd also need to copy the demo to a location within ~/.nexuiz/data for Nexuiz to be able to play it.
lost, proud to be a member of Nexuiz Ninjaz - Practicing the Ninja Art of Nexuiz since May 2009.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


Contact Us | Nexuiz Ninjaz Home | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication