Skip to content

Instantly share code, notes, and snippets.

@radium226
Created April 26, 2017 12:27
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save radium226/1aeaa98eb0d885eb18732699a44a7538 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 "${@}"
@lddd
Copy link

lddd commented Mar 17, 2018

Can you tell me how to use this? thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment