Skip to content

Instantly share code, notes, and snippets.

@undocumented-code
Last active January 9, 2022 00:46
Show Gist options
  • Save undocumented-code/2e6d8724670ffaea39a38c4b3d877d86 to your computer and use it in GitHub Desktop.
Save undocumented-code/2e6d8724670ffaea39a38c4b3d877d86 to your computer and use it in GitHub Desktop.
Stream a Headless QEMU instance to Youtube From Docker - https://blog.tuckerosman.com/2018/06/live-streaming-headless-processes-to.html
FROM ubuntu:18.04
ENV KEY <put your key here>
WORKDIR /root/
COPY entry.sh .
COPY windows98hd.img .
RUN chmod +x entry.sh
RUN apt -y update
RUN apt -y install software-properties-common
RUN apt -y install ffmpeg xvfb qemu-system-i386 screen
ENTRYPOINT ["./entry.sh"]
#!/bin/bash
# Adapted from https://docs.docker.com/config/containers/multi-service_container/
cd /root/
# Start the first process
screen -S xfvb -d -m xvfb-run -s ":99 -auth /tmp/xvfb.auth -ac -screen 0 640x480x16" qemu-system-i386 -hda windows98hd.img -vga cirrus
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start xvfb: Screen returned $status"
exit $status
fi
sleep 5
# Start the second process
screen -S ffmpeg -d -m ffmpeg -y -f x11grab -video_size 640x480 -i :99 -ar 44100 -ac 2 -f s16le -i /dev/zero -codec:a copy -deinterlace -vcodec libx264 -pix_fmt yuv420p -preset fast -r 30 -acodec libmp3lame -ar 44100 -bufsize 512k -f flv "rtmp://a.rtmp.youtube.com/live2/$KEY"
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start my_second_process: $status"
exit $status
fi
# Naive check runs checks once a minute to see if either of the processes exited.
# This illustrates part of the heavy lifting you need to do if you want to run
# more than one service in a container. The container exits with an error
# if it detects that either of the processes has exited.
# Otherwise it loops forever, waking up every 60 seconds
while sleep 60; do
ps aux |grep xvfb |grep -q -v grep
PROCESS_1_STATUS=$?
ps aux |grep ffmpeg |grep -q -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they exit with 0 status
# If they are not both 0, then something is wrong
if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment