Skrypt do zgrywania audio+video z dowolnego capture device
Skrypt daje GUI do wyboru urządzeń i opcję oglądania nagrywanego streamu na żywo (z dźwiękiem lub bez; opcja bez dźwięku ma mniejsze lagi).
Enkoduje do pliku z nazwą w której jest data i godzina. Enkodowanie jest przyspieszane sprzętowo za pomocą vaapi (testowane tylko na intelu).
#!/bin/bash -x
dialog_size="--width=500 --height=500"
settings_file="/home/kuba/bin/capture-settings.conf"
last_settings=$(cat "$settings_file")
if zenity --question --text="Use previous settings: $last_settings?";
then
audio_stream=$(echo $last_settings | awk "-F ::: " '{print $1}')
video_device=$(echo $last_settings | awk "-F ::: " '{print $2}')
video_format=$(echo $last_settings | awk "-F ::: " '{print $3}')
mbps=$(echo $last_settings | awk "-F ::: " '{print $4}')
playback_audio=$(echo $last_settings | awk "-F ::: " '{print $5}')
echo $audio_stream $video_device $video_format $mps
else
audio_stream=$(pactl list sources | grep -A 14 'Source ' | grep -E '(Description|Monitor of Sink|Name)' | sed -E 's/\t[a-zA-Z ]+: //' | zenity --width=1200 --height=400 --list --column=name --column=Description "--column=Monitor Of")
video_device=$(ls -1 /dev/video* | xargs -I {} bash -c "v4l2-ctl --device {} --info | grep --extended-regexp \"(Model|Card type)\" | awk -F: '{print \$2}' | awk '{print \"{}\" \",\" \$0}' | tr , '\n'" | zenity --list --column=device --column=name $dialog_size)
formats=$(env TERM=dumb SHELL=/bin/bash ffplay -f video4linux2 -list_formats all $video_device 2>&1)
video_format=$(echo "$formats" | grep video4linux2 | tr "\r" "\n" | grep -v 0/0 | awk -F" : " '{print $2 "," $3 "," $4}' | tr "," "\n" | sed -E 's/[ ]+/ /g' | zenity --list --column=name --column=space --column=framesize $dialog_size | sed 's/ //g')
mbps=$(zenity --scale --min-value=5 --max-value=100 --value=100 "--text=Mbps for video")
playback_audio=$(zenity --question --text "Playback audio" && echo playback_yes || echo playback_no)
fi
settings="$audio_stream ::: $video_device ::: $video_format ::: $mbps ::: $playback_audio"
echo "$settings" > "$settings_file"
if [ $playback_audio = "playback_yes" ]; then
audio_map="";
else
audio_map="-an -fflags nobuffer";
fi
ffmpeg -hide_banner -loglevel error -vaapi_device /dev/dri/renderD128 \
-f video4linux2 \
-input_format "$video_format" \
-thread_queue_size 512 \
-i "$video_device" \
-f pulse \
-thread_queue_size 512 \
-i "$audio_stream" \
-vf 'format=nv12,hwupload' \
-c:v h264_vaapi \
-b:v "${mbps}M" \
-f mp4 "$(date +%Y-%m-%d__%H_%M__%S).mp4" \
-fflags nobuffer \
-f nut -c copy $audio_map pipe:1 \
| ffplay \
-infbuf \
-analyzeduration 0 \
-loglevel debug \
-f nut -