tAdd note on syncing audio and video - adamsgaard.dk - my academic webpage
 (HTM) git clone git://src.adamsgaard.dk/adamsgaard.dk
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit dd1fafe054a5b921d85668cb7a09a05eafe4bbe3
 (DIR) parent 5b74834ab0d5bb546c96734bf5fa36c6b2c7353b
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Tue, 17 Mar 2020 12:36:24 +0100
       
       Add note on syncing audio and video
       
       Diffstat:
         M pages/004-screencasts.html          |      20 ++++++++++++++++++++
         M pages/004-screencasts.txt           |      81 +++++++++++++++++++------------
       
       2 files changed, 70 insertions(+), 31 deletions(-)
       ---
 (DIR) diff --git a/pages/004-screencasts.html b/pages/004-screencasts.html
       t@@ -96,6 +96,26 @@ for f in "$@"; do
        done
        </code></pre>
        
       +<p>If there is a delay between video and audio, this can also be
       +adjusted using ffmpeg(1).  I correct for a 0.3 s delay that I
       +encounter when recording on my laptop:</p>
       +
       +<pre><code>#!/bin/sh
       +
       +synchronize() {
       +        ffmpeg -y -i "$1" \
       +                -itsoffset 0.300 \
       +                -i "$1" \
       +                -map 0:v -map 1:a \
       +                -c copy \
       +                "${1%.*}_out.${1##*.}"
       +}
       +
       +for f in "$@"; do
       +        synchronize "$f"
       +done
       +</code></pre>
       +
        <figure class="pagefigure">
                <video poster="video/screencast.jpg" style="object-fit:fill;"
                        controls preload="none" class="mediaframe">
 (DIR) diff --git a/pages/004-screencasts.txt b/pages/004-screencasts.txt
       t@@ -19,37 +19,37 @@ script serves the purpose of starting and stopping recording:
        lockfile=/tmp/screenrecord.pid
        
        startrecording() {
       -        out="$HOME/screenrecord-$(date '+%Y-%m-%d_%H:%M:%S').mkv"
       -        ffmpeg -y \
       -                -f x11grab \
       -                -framerate 60 \
       -                -s "$(xdpyinfo | grep dimensions | awk '{print $2}')" \
       -                -i $DISPLAY \
       -                -f sndio -i default \
       -                -r 30 \
       -                -c:v libx264rgb -crf 0 -preset ultrafast -c:a flac \
       -                "$out" >/dev/null 2>&1 &
       -        printf '%s' "$!" > "$lockfile"
       -
       -        sleep 1
       -        if [ ! -f "$out" ]; then
       -                echo 'error: ffmpeg recording did not start' >&2
       -                notify-send -u CRITICAL "${0##*/}" 'ffmpeg recording did not start'
       -                rm -f "$lockfile"
       -                exit 1
       -        fi
       +        out="$HOME/screenrecord-$(date '+%Y-%m-%d_%H:%M:%S').mkv"
       +        ffmpeg -y \
       +                -f x11grab \
       +                -framerate 60 \
       +                -s "$(xdpyinfo | grep dimensions | awk '{print $2}')" \
       +                -i $DISPLAY \
       +                -f sndio -i default \
       +                -r 30 \
       +                -c:v libx264rgb -crf 0 -preset ultrafast -c:a flac \
       +                "$out" >/dev/null 2>&1 &
       +        printf '%s' "$!" > "$lockfile"
       +
       +        sleep 1
       +        if [ ! -f "$out" ]; then
       +                echo 'error: ffmpeg recording did not start' >&2
       +                notify-send -u CRITICAL "${0##*/}" 'ffmpeg recording did not start'
       +                rm -f "$lockfile"
       +                exit 1
       +        fi
        }
        
        stoprecording() {
       -        kill "$(cat "$lockfile")"
       -        rm -f "$lockfile"
       -        notify-send "${0##*/}" 'recording ended'
       +        kill "$(cat "$lockfile")"
       +        rm -f "$lockfile"
       +        notify-send "${0##*/}" 'recording ended'
        }
        
        if [ -f "$lockfile" ]; then
       -        stoprecording
       +        stoprecording
        else
       -        startrecording
       +        startrecording
        fi
        
        On Linux systems, the sound driver sndio should be replaced by alsa
       t@@ -63,9 +63,9 @@ command. The following script toggles the video feed:
        #!/bin/sh
        # remember to `chown $USER /dev/video0`
        if pgrep video >/dev/null 2>&1; then
       -        pkill video
       +        pkill video
        else
       -        nohup video -s 320 >/dev/null 2>&1 &
       +        nohup video -s 320 >/dev/null 2>&1 &
        fi
        
        On Linux, the command mpv /dev/video0 can take place of the video(1)
       t@@ -80,14 +80,33 @@ concievable loss in quality:
        #!/bin/sh
        
        encode() {
       -        ffmpeg -y -i "$1" \
       -                -c:v libx264 -threads 0 -preset faster -pix_fmt yuv420p \
       -                -c:a aac -crf 10 \
       -                "${1%.*}_out.mp4"
       +        ffmpeg -y -i "$1" \
       +                -c:v libx264 -threads 0 -preset faster -pix_fmt yuv420p \
       +                -c:a aac -crf 10 \
       +                "${1%.*}_out.mp4"
        }
        
        for f in "$@"; do
       -        encode "$f"
       +        encode "$f"
       +done
       +
       +If there is a delay between video and audio, this can also be
       +adjusted using ffmpeg(1).  I correct for a 0.3 s delay that I
       +encounter when recording on my laptop:
       +
       +#!/bin/sh
       +
       +synchronize() {
       +        ffmpeg -y -i "$1" \
       +                -itsoffset 0.300 \
       +                -i "$1" \
       +                -map 0:v -map 1:a \
       +                -c copy \
       +                "${1%.*}_out.${1##*.}"
       +}
       +
       +for f in "$@"; do
       +        synchronize "$f"
        done
        
        [3]Example screen recording using ffmpeg(1) and video(1) with the