Updated to work on PipeWire, probably still works on Pulse

Also cleaned up some code
This commit is contained in:
Logan G 2021-04-09 00:47:10 -06:00
parent 1bd3ff1ed8
commit 36bc4a16e1
Signed by: logan
GPG key ID: E328528C921E7A7A

View file

@ -14,7 +14,7 @@ LOOPBACK=true
VIDEO=true VIDEO=true
AUDIO=true AUDIO=true
VIDEODEVICE=/dev/video0 VIDEODEVICE=/dev/video0
AUDIODEVICE=/run/user/$UID/virtmic AUDIODEVICE=VirtPlay
BUFFERFILE=/tmp/virtplay BUFFERFILE=/tmp/virtplay
FFMPEGOPTIONS="" FFMPEGOPTIONS=""
@ -50,7 +50,7 @@ while [ ! $# == 0 ]; do
-v | --volume) VOLUME=$2 -v | --volume) VOLUME=$2
;; ;;
-h | --help | "") -h | --help | "")
echo "VirtPlay v3 - Plays a video/audio file through a virtual webcam and microphone" echo "VirtPlay v4 - Plays a video/audio file through a virtual webcam and microphone"
echo "" echo ""
echo "Usage: $0 [options] [value] [file]" echo "Usage: $0 [options] [value] [file]"
echo "" echo ""
@ -66,7 +66,7 @@ while [ ! $# == 0 ]; do
echo "-ao, --audio-only | Create a virtual microphone only" echo "-ao, --audio-only | Create a virtual microphone only"
echo "-vo, --video-only | Create a virtual webcam only" echo "-vo, --video-only | Create a virtual webcam only"
echo "-vd, --video-device [$VIDEODEVICE] | Changes which video device to output to" echo "-vd, --video-device [$VIDEODEVICE] | Changes which video device to output to"
echo "-ad, --audio-device [$AUDIODEVICE] | Changes the path for the fake audio device" echo "-ad, --audio-device [$AUDIODEVICE] | Changes the name for the fake audio device"
echo "-bf, --buffer-file [$BUFFERFILE] | Changes the path of the file that will be used when piping from stdin because bash is stupid" echo "-bf, --buffer-file [$BUFFERFILE] | Changes the path of the file that will be used when piping from stdin because bash is stupid"
exit exit
;; ;;
@ -86,16 +86,13 @@ if [[ ! $FILE ]]; then
eval cat /dev/stdin > $BUFFERFILE eval cat /dev/stdin > $BUFFERFILE
fi fi
trap ctrl_c INT trap cleanup INT
function ctrl_c() { cleanup () {
# If fake mic is enabled, delete the fake device
if [[ $AUDIO == true ]]; then if [[ $AUDIO == true ]]; then
pactl unload-module module-pipe-source pactl set-default-source $DEFAULTSOURCE
# Delete the pipe used for it too
if [[ -f $AUDIODEVICE ]]; then rm $AUDIODEVICE; fi
fi fi
# Gets rid of the stupid buffer file if it was used # Gets rid of the stupid buffer file if it was used
if [[ $FILE == $BUFFERFILE ]]; then if [[ $FILE == $BUFFERFILE ]]; then
eval rm $BUFFERFILE eval rm $BUFFERFILE
@ -114,28 +111,31 @@ fi
# If audio is enabled and the input has an audio stream, do some pulse wizardry and add it to ffmpeg # If audio is enabled and the input has an audio stream, do some pulse wizardry and add it to ffmpeg
if [[ $AUDIO == true ]] && ffprobe -i "$FILE" -show_streams -select_streams a -loglevel error | grep STREAM 2>&1 >/dev/null; then if [[ $AUDIO == true ]] && ffprobe -i "$FILE" -show_streams -select_streams a -loglevel error | grep STREAM 2>&1 >/dev/null; then
# Cleanup old pipes # If our fake audio device doesn't exist, then make it
if [[ -f $AUDIODEVICE ]]; then rm $AUDIODEVICE; fi if ! pactl list sources | grep $AUDIODEVICE; then
PULSEID=$(pactl load-module module-null-sink sink_name=$AUDIODEVICE object.linger=1 channels=$CHANNELS media.class=Audio/Duplex)
fi
# Creates a fake microphone and sets it as the default # Gets the current default source so we can switch back to it
pactl load-module module-pipe-source source_name=virtmic file=$AUDIODEVICE format=s16le rate=$RATE channels=$CHANNELS DEFAULTSOURCE=$(pactl info | sed -En 's/Default Source: (.*)/\1/p')
pactl set-default-source virtmic
pactl set-default-source $AUDIODEVICE
FFMPEGOPTIONS+=" -map 1:a -f s16le -af aresample=async=1000,aresample=$RATE,volume=$VOLUME -ar $RATE -ac $CHANNELS -y $AUDIODEVICE " FFMPEGOPTIONS+=" -map 1:a -f s16le -af aresample=async=1000,aresample=$RATE,volume=$VOLUME -ar $RATE -ac $CHANNELS -f pulse -device $AUDIODEVICE \"Virtplay\" "
# If loopback is enabled, add even more ffmpeg stuff # If loopback is enabled, add even more ffmpeg stuff
if [[ $LOOPBACK == true ]]; then if [[ $LOOPBACK == true ]]; then
FFMPEGOPTIONS+=" -map 1:a -af aresample=async=1000,aresample=$RATE,volume=$VOLUME -ar $RATE -ac $CHANNELS -buffer_duration 10 -f pulse default " FFMPEGOPTIONS+=" -map 1:a -af aresample=async=1000,aresample=$RATE,volume=$VOLUME -ar $RATE -ac $CHANNELS -f pulse default "
fi fi
fi fi
# So that Discord doesn't cut off the beginning of our high quality meme that we're playing # So that Discord doesn't cut off the beginning of our high quality meme that we're playing
sleep 1 #sleep 1
ffmpeg -fflags +genpts -ss $TIMESTAMP -re -err_detect ignore_err -i "$FILE" -ss $TIMESTAMP -re -err_detect ignore_err -i "$FILE" $FFMPEGOPTIONS ffmpeg -fflags +genpts -ss $TIMESTAMP -re -err_detect ignore_err -i "$FILE" -ss $TIMESTAMP -re -err_detect ignore_err -i "$FILE" $FFMPEGOPTIONS
# So that Discord doesn't cut off the end of the dank meme # So that Discord doesn't cut off the end of the dank meme
sleep 1 #sleep 1
# Cleanup # Cleanup
ctrl_c cleanup