Refactor camera script to use mpv instead of mplayer and delete dead code

master
Miles Alan 4 years ago
parent 44784c106b
commit 607ef17637
  1. 201
      scripts/core/sxmo_camera.sh

@ -1,143 +1,74 @@
#!/usr/bin/env sh #!/usr/bin/env sh
REAR_NODE="ov5640 3-004c"
SXMO_IMAGESDIR="$XDG_PICTURES_DIR/sxmo" REAR_LINK='"gc2145 3-003c":0->"sun6i-csi":0'
REAR_MODE="1920x1080@20"
node_rear="ov5640 3-004c" FRONT_NODE="gc2145 3-003c"
route_rear="ov5640" FRONT_LINK='"ov5640 3-004c":0->"sun6i-csi":0'
mode_rear="1920x1080@20" FRONT_MODE="1600x1200@15"
FMTPIX='UYVY'
node_front="gc2145 3-003c" FMTBUS='UYVY8_2X8'
route_front="gc2145"
mode_front="1600x1200@15" err() {
printf %b "$1" | dmenu -fn Terminus-20 -c -l 10
pixfmt='UYVY' exit 1
set_route () {
camera="$1"
if [ "$camera" = "$route_rear" ]
then
link1='"gc2145 3-003c":0->"sun6i-csi":0[0]'
link2='"ov5640 3-004c":0->"sun6i-csi":0[1]'
elif [ "$camera" = "$route_front" ]
then
link1='"ov5640 3-004c":0->"sun6i-csi":0[0]'
link2='"gc2145 3-003c":0->"sun6i-csi":0[1]'
fi
media-ctl -d /dev/media1 --links "$link1" && media-ctl -d /dev/media1 --links "$link2" || exit 1
} }
setupmediactllinks() {
setup () { media-ctl -d /dev/media1 --links "$1"
node="$1" media-ctl -d /dev/media1 --links "$2"
mode="$2"
res="${mode%%@*}"
speed="${mode##*@}"
busfmt='UYVY8_2X8'
setup="media-ctl -d /dev/media1 --set-v4l2 '\"$node\":0[fmt:$busfmt/$res@1/$speed]'"
eval "$setup"
height="${res%%x*}"
width="${res##*x}"
v4l2-ctl --device /dev/video1 --set-fmt-video="width=$width,height=$height,pixelformat=$pixfmt"
} }
setupv4l2() {
still () { MODE="$1"
node="$1" NODE="$2"
mode="$2" RES="${MODE%%@*}"
angle="$3" SPEED="${MODE##*@}"
skip="$4" HEIGHT="${RES%%x*}"
WIDTH="${RES##*x}"
setup "$node" "$mode" eval "media-ctl -d /dev/media1 --set-v4l2 '\"$NODE\":0[fmt:$FMTBUS/$RES@1/$SPEED]'"
v4l2-ctl --device /dev/video1 --set-fmt-video="width=$WIDTH,height=$HEIGHT,pixelformat=$FMTPIX" ||
speed="30" err "Couldnt set up camera\n Is killswitch in right position?"
res="${mode%%@*}" }
speed="${mode##*@}" startmpv() {
MODE="$1"
height="${res##*x}" RES="${MODE%%@*}"
width="${res%%x*}" SPEED="${MODE##*@}"
HEIGHT="${RES##*x}"
mkdir "$SXMO_IMAGESDIR" WIDTH="${RES%%x*}"
# -vf=transpose=1 - TODO: figure out why rotation is so slow..
SCREENSHOT_PRE="$(date)" mpv -v --demuxer-lavf-format=video4linux2 \
-demuxer-lavf-o=input_format=rawvideo,video_size=${WIDTH}x${HEIGHT}:framerate=$SPEED \
mplayer tv:// -tv driver=v4l2:width=$width:height=$height:device=/dev/video1 -fps $speed -vf rotate="$angle",screenshot="$SXMO_IMAGESDIR/$SCREENSHOT_PRE" --profile=low-latency --untimed --fps=$SPEED --vo=xv \
av://v4l2:/dev/video1
find "$SXMO_IMAGESDIR" -iname "$SCREENSHOT_PRE*" | sxiv -t -i -
# mpv command doesnt work =( https://wiki.archlinux.org/index.php/Webcam_setup#MPlayer
# mpv --demuxer-lavf-format=video4linux2 --demuxer-lavf-o-set=input_format=rawvideo:video_size=1920x1080:framerate=20 av://v4l2:/dev/video1 --profile=low-latency
} }
movie () { camerarear() {
# Not working but shows off the performance of this script setupmediactllinks "$REAR_LINK[0]" "$FRONT_LINK[1]"
node="$1" setupv4l2 "$REAR_MODE" "$REAR_NODE"
mode="$2" startmpv "$REAR_MODE"
angle="$3" }
skip="$4" camerafront() {
setupmediactllinks "$FRONT_LINK[0]" "$REAR_LINK[1]"
setup "$node" "$mode" setupv4l2 "$FRONT_MODE" "$FRONT_NODE"
startmpv "$FRONT_MODE"
speed="30" }
res="${mode%%@*}"
speed="${mode##*@}"
height="${res##*x}"
width="${res%%x*}"
VIDEO_NAME="$SXMO_IMAGESDIR"/"$(date)".mkv
# this command gives the best performance but I cannot get a video preview with this command =(
# please note, to stop recording, you need to type `killall ffmpeg` in a terminal
ffmpeg -f v4l2 -framerate $speed -video_size $res -i /dev/video1 -preset ultrafast -filter:v fps=fps=$speed -f matroska "$VIDEO_NAME"
save="Save: $VIDEO_NAME"
playback="Playback: $(ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$VIDEO_NAME")"
delete="Delete Recording"
DONE=0
while [ $DONE != 1 ]; do cameramenu() {
result="$(printf %b "$save\n$playback\n$delete" | dmenu -fn Terminus-30 -c -p "Record" -l 20)" CHOICE="$(
if [ "$result" = "$save" ]; then printf %b "Rear Camera\nFront Camera\nClose Menu" |
return 0 dmenu -fn Terminus-30 -c -p "Camera" -l 20
elif [ "$result" = "$playback" ]; then )"
mpv "$VIDEO_NAME" if [ "$CHOICE" = "Close Menu" ]; then
else exit 0
rm "$VIDEO_NAME" elif [ "$CHOICE" = "Rear Camera" ]; then
return 0 sxmo_rotate.sh rotright #TODO - figure out how to rotate w mpv
camerarear
elif [ "$CHOICE" = "Front Camera" ]; then
sxmo_rotate.sh rotleft #TODO - figure out how to rotate w mpv
camerafront
fi fi
done
# outputs 10 fps =(
# ffmpeg -f v4l2 -framerate $speed -video_size $res -i /dev/video1 -preset ultrafast -filter:v fps=fps=$speed -f matroska pipe: | tee file.mkv | mplayer - -fps $speed -vf screenshot
# I tried forking ffmpeg to background and using mplayer. When mplayer closes, killall ffmpeg. It didnt work =(
#mplayer tv:// -tv driver=v4l2:width=$width:height=$height:device=/dev/video1 -fps $speed -vf screenshot
} }
if [ $# -gt 0 ]; then
type="$(printf %b "picture\nmovie" | dmenu -fn Terminus-30 -c -p "Record" -l 20)" "$@"
camera="$(printf %b "front\nrear" | dmenu -fn Terminus-30 -c -p "Record" -l 20)" else
cameramenu
# Note: "angle" is set according to mplayer's conventions. See man mplayer and look for "rotate" fi
[ "$camera" = "rear" ] && set_route "$route_rear" && skip=5 && node="$node_rear" && mode="$mode_rear" && angle="1"
[ "$camera" = "front" ] && set_route "$route_front" && skip=0 && node="$node_front" && mode="$mode_front" && angle="2"
# our angle variables are set for rotnormal orientation
# rotating will ruin the preview
RUNNING_AUTO="$(ps aux | grep "sh /usr/bin/sxmo_autorotate.sh" | grep -v grep | cut -f2 -d' ')"
[ -n "$RUNNING_AUTO" ] && echo "$RUNNING_AUTO" | tr '\n' ' ' | xargs kill -9; notify-send "Turning autorotate off"
sxmo_rotate.sh isrotated && sxmo_rotate.sh rotnormal
[ "$type" = "picture" ] && still "$node" "$mode" "$angle" "$skip"
[ "$type" = "movie" ] && movie "$node" "$mode" "$angle" "$skip"
[ -n "$RUNNING_AUTO" ] && sxmo_autorotate.sh

Loading…
Cancel
Save