Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save siwtom/88ab360d184142443b7094866e8c5c50 to your computer and use it in GitHub Desktop.
Save siwtom/88ab360d184142443b7094866e8c5c50 to your computer and use it in GitHub Desktop.
Run an Android emulator with a fake webcam
#!/bin/bash
export REAL_DEVICE="/dev/video0"
export FAKE_DEVICE="/dev/video1"
export DURATION="5"
export SIZE="320x240"
export AVD="Nexus_5X_API_25"
export VIDEO_FILE="./webcam.avi"
record_webcam()
{
declare video_fp="${1}"
ffmpeg \
-loglevel "quiet" \
-y \
-f "v4l2" \
-r "30" \
-s "${SIZE}" \
-i "${REAL_DEVICE}" \
-pix_fmt "yuv420p" \
-vcodec "libx264" \
-ss "00:00:0.0" -t "${DURATION}" \
"${video_fp}"
}
fake_webcam()
{
declare video_fp="${1}"
ffmpeg \
-loglevel "quiet" \
-re \
-f "lavfi" \
-i "movie=filename=${video_fp}:loop=0, setpts=N/(FRAME_RATE*TB)" \
-f "v4l2" "${FAKE_DEVICE}"
}
run_emulator()
{
cd "${ANDROID_HOME}/tools"
./emulator @${AVD} -camera-back "webcam1"
cd -
}
main()
{
sudo modprobe "v4l2loopback"
declare action="${1:-"run"}"
shift
case "${action}" in
"run")
(
fake_webcam "${VIDEO_FILE}"
) &
declare ffmpeg_pid="${!}"
run_emulator
pkill -TERM -P "${ffmpeg_pid}"
;;
"record")
record_webcam "${VIDEO_FILE}"
;;
esac
}
main "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment