Skip to content

Instantly share code, notes, and snippets.

@narma
Created March 20, 2014 04:03
Show Gist options
  • Select an option

  • Save narma/9656964 to your computer and use it in GitHub Desktop.

Select an option

Save narma/9656964 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Capture X11 with encoding to mp4 on fly with noise removal with sox
# and mixing both mic with game audio
# TODO: remove noise by pulseaudio
loadModule() {
# Pavucontrol guide
# playback: Send audio from the game to game_out
# record: stream from duplex_out
pacmd load-module module-null-sink sink_name=duplex_out sink_properties=device.description="duplex_out"
pacmd load-module module-null-sink sink_name=game_out sink_properties=device.description="Myplayback_game"
pacmd load-module module-loopback source=game_out.monitor
pacmd load-module module-loopback source=game_out.monitor sink=duplex_out
pacmd load-module module-loopback sink=duplex_out
}
unloadModule(){
echo "Stopping Audio (Don't worry if you see errors here)"
pacmd unload-module duplex_out
pacmd unload-module game_out
pacmd unload-module module-null-sink
pacmd unload-module module-loopback
echo "Exit!"
}
# Setup
echo "Please setup the Audio Output to sink null (something like 'pavucontrol')"
echo " playback: Send audio from the game to game_out"
echo " record: stream from duplex_out"
# if you see errors here, please report on github
loadModule
export LD_LIBRARY_PATH=/usr/local/lib
WEBCAM_HW="1280x720"
#WEBCAM_HW="640x480"
#NOISE_PROF="/data/tmp/noise.prof"
#NOISE_PROF="/data/w/noise2.prof" # notebook microphone
NOISE_PROF="/data/w/micro_noise.prof" # blue microphone
NOISE_LEVEL="0.1"
if [ $# -lt 1 ]; then
exit 1
fi
rm -f /tmp/audio_pipe
mkfifo /tmp/audio_pipe
# HowTo generate noise profile:
# Write only noise to noiseaud.wav, after that run:
# sox noiseaud.wav -n noiseprof noise.prof
#-f v4l2 -vcodec mjpeg -video_size $WEBCAM_HW -framerate 30 -i /dev/video0 \
# -filter_complex "[0:v]scale=1280:-1,setpts=PTS-STARTPTS[bg]; [1:v]scale=120:-1,setpts=PTS-STARTPTS[fg]; [bg][fg]overlay=W-w-10:10,format=yuv420p,scale=1280:720[out]" \
{ ffmpeg -loglevel quiet -nostdin -f pulse -i default -f wv pipe:1 | sox -q - -t .flac -C 4 /tmp/audio_pipe noisered $NOISE_PROF $NOISE_LEVEL; } &
SOX_PID=$!
/usr/local/bin/ffmpeg -f x11grab -s 1600x900 -framerate 30 -r 30 -i :0.0+2,24 \
-f v4l2 -vcodec mjpeg -video_size $WEBCAM_HW -framerate 30 -i /dev/video0 \
-filter_complex "[0:v]setpts=PTS-STARTPTS,fifo [bg]; [1:v]scale=240:-1,setpts='(RTCTIME - RTCSTART) / (TB * 1000000)',fps=30,format=yuv420p[fg]; [bg][fg]overlay=W-w-10:10,format=yuv420p,scale=1280:720[out]" \
-i /tmp/audio_pipe \
-map "[out]" -map 2:a \
-f mp4 -y -c:v libx264 -profile:v high -level 4.1 -s 1280x720 -r 30 -ar 44100 -tune film \
-c:a libfdk_aac -movflags +faststart -b:a 196k -threads 0 $1
rm -f /tmp/audio_pipe
kill $SOX_PID
sleep 0.05
kill -0 $SOX_PID 2>/dev/null; RET=$?
if [ "$RET" -eq "0" ]; then
echo "kill -9"
kill -9 $SOX_PID
fi
killall -9 sox
killall -9 ffmpeg
unloadModule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment