diff --git a/virtplay b/virtplay index c77ee11..0ebb5a9 100755 --- a/virtplay +++ b/virtplay @@ -14,7 +14,7 @@ LOOPBACK=true VIDEO=true AUDIO=true VIDEODEVICE=/dev/video0 -AUDIODEVICE=/run/user/$UID/virtmic +AUDIODEVICE=VirtPlay BUFFERFILE=/tmp/virtplay FFMPEGOPTIONS="" @@ -50,7 +50,7 @@ while [ ! $# == 0 ]; do -v | --volume) VOLUME=$2 ;; -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 "Usage: $0 [options] [value] [file]" echo "" @@ -66,7 +66,7 @@ while [ ! $# == 0 ]; do echo "-ao, --audio-only | Create a virtual microphone only" echo "-vo, --video-only | Create a virtual webcam only" 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" exit ;; @@ -86,16 +86,13 @@ if [[ ! $FILE ]]; then eval cat /dev/stdin > $BUFFERFILE fi -trap ctrl_c INT +trap cleanup INT -function ctrl_c() { - # If fake mic is enabled, delete the fake device +cleanup () { if [[ $AUDIO == true ]]; then - pactl unload-module module-pipe-source - - # Delete the pipe used for it too - if [[ -f $AUDIODEVICE ]]; then rm $AUDIODEVICE; fi + pactl set-default-source $DEFAULTSOURCE fi + # Gets rid of the stupid buffer file if it was used if [[ $FILE == $BUFFERFILE ]]; then 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 == true ]] && ffprobe -i "$FILE" -show_streams -select_streams a -loglevel error | grep STREAM 2>&1 >/dev/null; then - # Cleanup old pipes - if [[ -f $AUDIODEVICE ]]; then rm $AUDIODEVICE; fi + # If our fake audio device doesn't exist, then make it + 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 - pactl load-module module-pipe-source source_name=virtmic file=$AUDIODEVICE format=s16le rate=$RATE channels=$CHANNELS - pactl set-default-source virtmic + # Gets the current default source so we can switch back to it + DEFAULTSOURCE=$(pactl info | sed -En 's/Default Source: (.*)/\1/p') + + 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 == 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 # 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 # So that Discord doesn't cut off the end of the dank meme -sleep 1 +#sleep 1 # Cleanup -ctrl_c +cleanup