You are not logged in.

#1 2008-12-06 14:53:23

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

dem2vid - script to capture & encode nexuiz demos

Simple script to encode a .dem to an aac/avc video.

To produce a high quality mp4 simply specify the demo to encode in addition to your desired width & file size. If no file size is specified a VBR encode will be done.The demo file must be within your ~/.nexuiz/data/demos/ directory, I recommend renaming it to something simple beforehand.

#!/bin/bash

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

DEM="$1"
WID="$2"
MB="$3"

captureDemo() {
LOC=`locate nexuiz-linux-glx.sh`    
${LOC} -demo +cl_capturevideo 1 +cl_capturevideo_fps 24 +playdemo demos/${DEM}
}

info() {
VID=`find ~/.nexuiz/data/video/ -iname "dpvideo*.avi" -print | tail -1`    
mplayer -identify -frames 0 ${VID} 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} ${VID}
}

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

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

encode() {
ffmpeg -i ${VID} -an -pass 1 -s ${WID}x${MOD16} -vcodec libx264 -vpre fast \
-b ${KBPS}k -threads 0 -rc_eq 'blurCplx^(1-qComp)' -level 41 "$DEM.mp4"
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 -y "$DEM.mp4"
}

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 "$DEM.mp4"
}

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

If you wish to try my specific ffmpeg presets, they can be found here: http://bbs.archlinux.org/viewtopic.php? … 71#p441071

An example of the VBR encode: http://www.mediafire.com/?d1dd2xczz9z

I hope it's of some use. wink

Offline

#2 2009-05-25 22:11:11

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

Re: dem2vid - script to capture & encode nexuiz demos

Updated to retain quality when used with Nexuiz 2.5.1 & removed mplayer dependency.

#!/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}"

Last edited by ahaslam (2009-06-01 18:19:45)

Offline

Board footer

Powered by FluxBB