]> git.xonotic.org Git - xonotic/xonotic.git/blobdiff - misc/tools/encode-demos.sh
Merge branch 'Mario/glx_build' into 'master'
[xonotic/xonotic.git] / misc / tools / encode-demos.sh
index 090fa20ff1feaf1ceac1293a7f0788141c75ac9a..ef920a9727fd14ec94a485bbf5017d42b96ee70e 100755 (executable)
@@ -1,9 +1,9 @@
 #!/bin/bash
 # name: encode-demos.sh
-# version: 0.6.1
-# author: Tyler "-z-" Mulligan
+# version: 0.6.3
+# author: Tyler "-z-" Mulligan <z@xnz.me>
 # license: GPL & MIT
-# date: 24-02-2017
+# date: 01-04-2017
 # description: headless encoding of demo files to HD video concurrently with Xfvb and parallel
 #
 # The encoding is done with a full Xonotic client inside Xfvb.
 #
 
 # Customize
-XONDIR=${HOME}/xonotic/xonotic                      # path to ./all
 USERDIR=${HOME}/.xonotic-clean                      # path to Xonotic userdir for client that does encoding
+GAMEDIR=${USERDIR}/data                             # path to Xonotic gamedir for client that does encoding
 XONOTIC_BIN="./all"                                 # binary used to launch Xonotic
-JOB_TIMEOUT="1h"                                    # if demo doesn't quit itself or hangs
+JOB_TIMEOUT="4h"                                    # if demo doesn't quit itself or hangs
 JOBS=4                                              # number of concurrent jobs
 DEFAULT_DEMO_LIST_FILE="demos.txt"                  # for batch
 DISPLAY=:1.0                                        # display for Xvfb
@@ -71,6 +71,25 @@ REGEX_VIDEO_TYPES="^(mp4|webm)$"
 # State
 export KILLER_KEYWORD_WATCHING=true
 
+# Xonotic Helpers
+
+_check_xonotic_dir() {
+    local xon_dir=$1
+    if [[ ! -d ${xon_dir} ]]; then
+        echo "[ ERROR ] Unable to locate Xonotic"; exit 1
+    fi
+}
+
+_get_xonotic_dir() {
+    relative_dir=$(dirname $0)/../..
+    _check_xonotic_dir ${relative_dir}
+    export XONOTIC_DIR="$(cd ${relative_dir}; pwd)"
+}
+
+_kill_xonotic() {
+    pkill -f "\-simsound \-sessionid xonotic_${SCRIPT_NAME}_"
+}
+
 # Data Helpers
 ###############
 
@@ -103,7 +122,7 @@ _get_demo_command() {
     local demo_file=$1
     local index=$2
     name_format=$(basename "${demo_file}" .dem)
-    command="${XONDIR}/${XONOTIC_BIN} run sdl -simsound -sessionid xonotic_${SCRIPT_NAME}_${index} -userdir \"${USERDIR}\" \
+    command="${XONOTIC_DIR}/${XONOTIC_BIN} run sdl -simsound -sessionid xonotic_${SCRIPT_NAME}_${index} -userdir \"${USERDIR}\" \
         +log_file \"xonotic_${SCRIPT_NAME}_${index}_${name_format}.log\" \
         +cl_capturevideo_nameformat \"${name_format}_\" \
         +cl_capturevideo_number 0 \
@@ -145,7 +164,7 @@ _queue_add_compression_jobs() {
     local type=$1; shift
     local videos="$@"
     for video_file in ${videos[@]}; do
-        local command=$(_get_compression_command ${USERDIR}/data/${video_file} ${type})
+        local command=$(_get_compression_command ${GAMEDIR}/${video_file} ${type})
         _queue_add_job ${queue_file} "${command}" ${video_file}
     done
 }
@@ -256,7 +275,7 @@ _run_demo_jobs() {
 _cleanup() {
     rm -f ${QUEUE_FILE_DEMOS}
     rm -f ${LOCK_FILE}
-    rm -f ${USERDIR}/data/*.log
+    rm -f ${GAMEDIR}/*.log
     export KILLER_KEYWORD_WATCHING=false
     sleep 1
     _kill_xonotic
@@ -270,10 +289,6 @@ _cleanup_compress() {
     rm -f ${QUEUE_FILE_COMPRESSING}
 }
 
-_kill_xonotic() {
-    pkill -f "\-simsound \-sessionid xonotic_${SCRIPT_NAME}_"
-}
-
 # Application Helpers
 ######################
 
@@ -330,7 +345,7 @@ compress() {
     fi
 
     # compress
-    local command=$(_get_compression_command ${USERDIR}/data/${video_file} ${type})
+    local command=$(_get_compression_command ${GAMEDIR}/${video_file} ${type})
     echo ${command}
     echo "[ INFO ] Compressing '${video_file}'"
     _queue_add_compression_jobs ${QUEUE_FILE_COMPRESSING} ${type} ${video_file[@]}
@@ -343,6 +358,24 @@ compress() {
     fi
 }
 
+create_gif() {
+    local video=$1
+    local fps=${2:-15}
+    local width=${3:-320}
+    local start=${4:-0}
+    local length=${5:-999999}
+    local loop=${6:-1}
+    local output=$(basename ${video%.*}.gif)
+
+    # Generate palette for better quality
+    ${FFMPEG} -i ${GAMEDIR}/${video} -vf fps=${fps},scale=${width}:-1:flags=lanczos,palettegen ${GAMEDIR}/tmp_palette.png
+
+    # Generate gif using palette
+    ${FFMPEG} -i ${GAMEDIR}/${video} -i ${GAMEDIR}/tmp_palette.png -ss ${start} -t ${length} -loop ${loop} -filter_complex "fps=${fps},scale=${width}:-1:flags=lanczos[x];[x][1:v]paletteuse" ${GAMEDIR}/${output}
+
+    rm ${GAMEDIR}/tmp_palette.png
+}
+
 list_jobs() {
     completed_jobs=$(_get_completed_jobs)
     active_jobs=$(_get_active_demo_jobs)
@@ -417,13 +450,13 @@ log_keyword_grep() {
     local keyword="$@"
     for worker in $(_get_active_demo_workers); do
         local log_file="${worker}.log"
-        local keyword_count=$(grep -c "${keyword}" "${USERDIR}/data/${log_file}")
+        local keyword_count=$(grep -c "${keyword}" "${GAMEDIR}/${log_file}")
         if [[ ${keyword_count} > 0 ]]; then
             if [[ ${type} == "worker" ]]; then
                 echo "${worker}"
             else
                 echo "[ worker ] ${worker}"
-                grep "${keyword}" "${USERDIR}/data/${log_file}"
+                grep "${keyword}" "${GAMEDIR}/${log_file}"
             fi
         fi
     done
@@ -495,19 +528,23 @@ COMMANDS
 
     Encoding
     --------
-    batch  [demos.txt] [timeout] [--compress]   batch process a list of demos from file relative to \$USERDIR/data
-    single <demo> [timeout] [--compress]        process a single demo file in \$USERDIR/data. ex: demos/cool.dem
-                                                'timeout' does not include '--compress', compress starts a new job
+    batch  [demos.txt] [timeout] [--compress]           batch process a list of demos from file relative to \$USERDIR/data
+    single <demo> [timeout] [--compress]                process a single demo file in \$USERDIR/data. ex: demos/cool.dem
+                                                        'timeout' does not include '--compress', compress starts a new job
     Compression
     -----------
-    compress <video> [mp4|webm] [--cleanup]     compress an encoded ogv in \$USERDIR/data, ex: video/cool.ogv
+    compress <video> [mp4|webm] [--cleanup]             compress an encoded ogv in \$USERDIR/data, ex: video/cool.ogv
+
+    Convert
+    -----------
+    gif <video> [fps] [width] [start] [length] [loop]   convert a video to gif in \$USERDIR/data, ex: video/cool.ogv
 
     Job Management
     --------------
-    grep <keyword>                              grep the server logs of the workers
-    kkill <keyword>                             keyword kill, kill a worker if string is matched
-    list [-f]                                   list currently active/queued/completed jobs
-    log [-f]                                    tail the current log (-f follows log)
+    grep <keyword>                                      grep the server logs of the workers
+    kkill <keyword>                                     keyword kill, kill a worker if string is matched
+    list [-f]                                           list currently active/queued/completed jobs
+    log [-f]                                            tail the current log (-f follows log)
 
 EXAMPLES
 
@@ -523,6 +560,9 @@ EXAMPLES
     # compress a video in \$USERDIR/data (outputs test.mp4, and deletes the original)
     ./encode-demos.sh compress video/test.ogv --cleanup
 
+    # convert video to gif (14 fps, 640 width, start at 4s, length of 4s, loop 100 times)
+    ./encode-demos.sh gif video/2017-04-01_11-53_afterslime_000.ogv 14 640 4 4 100
+
     # list jobs
     ./encode-demos.sh list
 
@@ -531,9 +571,23 @@ EXAMPLES
 
     # follow a completed job log
     ./encode-demos.sh log -f
+
+    # Override the path to Xonotic (assumed from relative location of this script)
+    XONOTIC_DIR=\$HOME/some/other/dir; ./misc/tools/encode-demos.sh --version
 "
 }
 
+# Init
+######
+
+# Allow for overriding the path assumption
+# XONOTIC_DIR=$HOME/some/other/dir; ./misc/tools/encode-demos.sh --version
+if [[ -z ${XONOTIC_DIR} ]]; then
+    _get_xonotic_dir
+else
+    _check_xonotic_dir ${XONOTIC_DIR}
+fi
+
 case $1 in
     # flags
     '--version')        _version;;
@@ -543,6 +597,8 @@ case $1 in
     'single')           _run_xvfb; process_single $2 $3 $4;;
     # compression
     'compress')         compress $2 $3 $4;;
+    # convert
+    'gif')              create_gif $2 $3 $4 $5 $6 $7;;
     # monitoring/management
     'grep')             log_keyword_grep 'normal' $2;;
     'kkill')            log_killer_keyword $2;;