Skip to content

Instantly share code, notes, and snippets.

@borisisok
Last active April 6, 2022 19:36
Show Gist options
  • Save borisisok/1083bc3a30dca023e13f6a224a60f00a to your computer and use it in GitHub Desktop.
Save borisisok/1083bc3a30dca023e13f6a224a60f00a to your computer and use it in GitHub Desktop.
Mirabox - Record via ffmpeg
# The Linux version
VDEV=$(v4l2-ctl --list-devices | grep -A1 -i mirabo | grep /dev | sed 's/\s//g')
ADEV=$(grep "Capture.*Mira" /proc/asound/cards | cut -f2 -d" ")
TS=$(date +%Y%m%d_%H%M%S)
nohup ffmpeg -f alsa -ac 2 -i hw:${ADEV=},0 -f video4linux2 -i ${VDEV} -acodec ac3 -ab 128k -f matroska -movflags frag_keyframe -s 1920x1080 -vcodec libx264 -preset ultrafast -qp 16 ${TS}.mkv &
sleep 5;
vlc ${TS}.mkv
killall ffmpeg
echo ${TS}.mkv
@borisisok
Copy link
Author

borisisok commented Jul 9, 2019

Record in windows

mkv

ffmpeg -f dshow -rtbufsize 1024M -framerate 60 -i video="MiraBox Video Capture":audio="Digitale Audioschnittstelle (MiraBox Video Capture)" -acodec ac3 -ab 128k -f matroska -movflags frag_keyframe -s 1920x1080 -vcodec libx264 -preset ultrafast -qp 16 "out.mkv"

ffmpeg -f dshow -rtbufsize 1024M -framerate 60 -i video="MiraBox Video Capture":audio="Digitale Audioschnittstelle (2- MiraBox Video Capture)" -acodec ac3 -ab 384k -f matroska -movflags frag_keyframe -s 1920x1080 -vcodec libx264 -preset ultrafast -qp 16 "out.mkv"

mp4

ffmpeg -f dshow -rtbufsize 1024M -framerate 60 -i video="MiraBox Video Capture":audio="Digitale Audioschnittstelle (2- MiraBox Video Capture)" -c:v libx264 -preset slow -profile:v high -crf 18 -coder 1 -pix_fmt yuv420p -movflags +faststart -g 30 -bf 2 -c:a aac -b:a 384k -profile:a aac_low "out.mp4"

copy mpeg video and raw audio stream (bad results)

ffmpeg -f dshow -video_size 1920x1080 -rtbufsize 702000k -framerate 60 -i video="MiraBox Video Capture":audio="Digitale Audioschnittstelle (2- MiraBox Video Capture)" -c:v copy -c:a copy "out.mkv"

cd "D:\ffmpeg-z"
./ffmpeg.exe -y -s 640x480 -framerate 25 -rtbufsize 100MB -f dshow -i video="Logitech HD Webcam C525":audio="Microphone (HD Webcam C525)" -vf "drawtext=expansion=strftime:fontfile=arial.ttf:text='%a %d.%m.%Y / %H:%M:%S':x=10:y=10:fontcolor=white: box=1: boxcolor=0x00000000@0.7" -vcodec h264 -acodec aac -t 00:58:00 $("D:/"+(Get-Date -UFormat "%Y%m%d%-%H%M").ToString()+".mp4")

https://sunoo.github.io/homebridge-camera-ffmpeg/configs/Logitech-C525-USB-camera.html

@borisisok
Copy link
Author

borisisok commented Jul 9, 2019

List devices in windows

ffmpeg -list_devices true -f dshow -i dummy

List device options

ffmpeg -f dshow -list_options true -i video="MiraBox Video Capture"

List device opts in linux

ffmpeg -y -f video4linux2 -i /dev/video0
ffmpeg -f video4linux2 -list_formats all -i /dev/video0

ffmpeg -hide_banner -f v4l2 -list_formats all -i /dev/video0

v4l2-ctl

v4l2-ctl --list-devices
v4l2-ctl -D -d /dev/video0
v4l2-ctl --list-ctrls -d /dev/video0
v4l2-ctl --list-ctrls-menus -d /dev/video0
v4l2-ctl --list-formats-ext

Logitech C525 record compressed video

ffmpeg -f v4l2 -input_format mjpeg -i /dev/video0 -c:v copy output.mkv

pi@raspicam1:~ $ ffmpeg -hide_banner -f v4l2 -list_formats all -i /dev/video0
[video4linux2,v4l2 @ 0x501e40] Raw : yuyv422 : YUYV 4:2:2 : 640x480 160x120 176x144 320x176 320x240 432x240 352x288 544x288 640x360 752x416 800x448 864x480 960x544 1024x576 800x600 1184x656 960x720 1280x720 1392x768 1504x832 1600x896 1280x960 1712x960 1792x1008 1920x1080
[video4linux2,v4l2 @ 0x501e40] Compressed: mjpeg : Motion-JPEG : 640x480 160x120 176x144 320x176 320x240 432x240 352x288 544x288 640x360 752x416 800x448 864x480 960x544 1024x576 800x600 1184x656 960x720 1280x720 1392x768 1504x832 1600x896 1280x960 1712x960 1792x1008 1920x1080

@borisisok
Copy link
Author

borisisok commented Jul 9, 2019

@borisisok
Copy link
Author

borisisok commented Jul 16, 2019

Fix linux records delayed/async audio

ffmpeg.exe -err_detect ignore_err -i "in.mkv" -itsoffset 0.55 -i "in.mkv" -map 1:v -map 0:a -c copy "out.mkv"

@borisisok
Copy link
Author

YouTube recommended encoding settings on ffmpeg (+ libx264)

ffmpeg -i input -c:v libx264 -preset slow -profile:v high -crf 18 -coder 1 -pix_fmt yuv420p -movflags +faststart -g 30 -bf 2 -c:a aac -b:a 384k -profile:a aac_low output
Source: https://gist.github.com/mikoim/27e4e0dc64e384adbcb91ff10a2d3678

@borisisok
Copy link
Author

borisisok commented Aug 15, 2021

Notepad

source": "-f alsa -ac 1 -ar 44100 -thread_queue_size 2048 -i plughw:CARD=C525,DEV=0 -re -f video4linux2 -i
/dev/video0 -vsync 0 -af aresample=async=1",
"stillImageSource": "-s 1280x720 -f video4linux2 -i /dev/video0",
"maxStreams": 2,
"maxWidth": 1280,
"maxHeight": 720,
"maxFPS": 30,
"audio": true,
"debug": true,
"packetSize": 188,
"mapvideo": "1",
"mapaudio": "0"

pi@raspberrypi:~ $ v4l2-ctl --list-devices

bcm2835-codec-decode (platform:bcm2835-codec):
/dev/video10
/dev/video11
/dev/video12

bcm2835-isp (platform:bcm2835-isp):
/dev/video13
/dev/video14
/dev/video15
/dev/video16

HD Webcam C525 (usb-20980000.usb-1.1):
/dev/video0
/dev/video1

pi@raspberrypi:~ $ v4l2-ctl -d /dev/video0 --list-ctrls
brightness 0x00980900 (int) : min=0 max=255 step=1 default=128 value=128
contrast 0x00980901 (int) : min=0 max=255 step=1 default=32 value=32
saturation 0x00980902 (int) : min=0 max=255 step=1 default=32 value=32
white_balance_temperature_auto 0x0098090c (bool) : default=1 value=1
gain 0x00980913 (int) : min=0 max=255 step=1 default=64 value=128
power_line_frequency 0x00980918 (menu) : min=0 max=2 default=2 value=2
white_balance_temperature 0x0098091a (int) : min=2800 max=6500 step=1 default=5500 value=5838 flags=inactive
sharpness 0x0098091b (int) : min=0 max=255 step=1 default=22 value=22
backlight_compensation 0x0098091c (int) : min=0 max=1 step=1 default=1 value=1
exposure_auto 0x009a0901 (menu) : min=0 max=3 default=3 value=3
exposure_absolute 0x009a0902 (int) : min=3 max=2047 step=1 default=166 value=664 flags=inactive
exposure_auto_priority 0x009a0903 (bool) : default=0 value=1
pan_absolute 0x009a0908 (int) : min=-36000 max=36000 step=3600 default=0 value=0
tilt_absolute 0x009a0909 (int) : min=-36000 max=36000 step=3600 default=0 value=0
focus_absolute 0x009a090a (int) : min=0 max=255 step=5 default=60 value=60 flags=inactive
focus_auto 0x009a090c (bool) : default=1 value=1
zoom_absolute 0x009a090d (int) : min=1 max=5 step=1 default=1 value=1
led1_mode 0x0a046d05 (menu) : min=0 max=3 default=3 value=3
led1_frequency 0x0a046d06 (int) : min=0 max=255 step=1 default=0 value=0

complex example from fancy homebridge setup

ffmpeg -f alsa -ac 1 -ar 44100 -thread_queue_size 2048
-i plughw:CARD=C525,DEV=0 -re -f video4linux2 -i /dev/video0
-vsync 0 -af aresample=async=1 -map 1
-codec:v libx264 -pix_fmt yuv420p -color_range mpeg -r 30 -f rawvideo -preset ultrafast -tune zerolatency
-filter:v scale='min(1280,iw)':'min(720,ih)':force_original_aspect_ratio=decrease,scale=trunc(iw/2)*2:trunc(ih/2)*2
-b:v 802k -payload_type 99 -ssrc 9061732 -f rtp -srtp_out_suite AES_CM_128_HMAC_SHA1_80 -srtp_out_params \ nq06StWnJvjHfEQiY9FNwuCPzQrpqgN/r+RpJR/K srtp://192.168.192.128:60221?rtcpport=60221&pkt_size=188
-map 0 -codec:a libfdk_aac -profile:a aac_eld -flags +global_header -f null -ar 16k -b:a 24k -ac 1 -payload_type 110
-ssrc 5827949 -f rtp -srtp_out_suite AES_CM_128_HMAC_SHA1_80
-srtp_out_params YgdtCtJOK59vjDHVS2goJ+eIv7eRD4pFaU2M3XwW srtp://192.168.192.128:61334?rtcpport=61334&pkt_size=188
-loglevel level+verbose

ffmpeg -f v4l2 -input_format yuyv422 -s 640x480 -i /dev/video0 -c:v libx264 -profile:v baseline -trellis 0 -subq 1 -level 32 -preset superfast -tune zerolatency -me_method epzs -crf 30 -threads 0 -bufsize 1 -refs 4 -coder 0 -b_strategy 0 -bf 0 -sc_threshold 0 -x264-params vbv-maxrate=2000:slice-max-size=1500:keyint=30:min-keyint=10: -pix_fmt yuv420p -an -f mpegts udp://192.168.1.8:5001
The ffplay command used to display the video feed is,

ffplay -analyzeduration 1 -fflags -nobuffer -i udp://192.168.1.8:5001

gsteamer

https://stackoverflow.com/questions/38185041/vlc-how-to-play-a-mjpeg-stream

gst-launch-1.0 v4l2src ! image/jpeg,width=1280,height=720 ! tcpserversink host=192.168.0.127
gst-launch-1.0 v4l2src device=/dev/video0 do-timestamp=true ! image/jpeg,width=1280,height=720 ! tcpserversink sync=false host=192.168.0.127

vlc 'tcp://192.168.0.127:4953'

https://medium.com/@petehouston/play-two-webcams-in-mjpg-format-simultaneously-using-gstreamer-2302ca91504

@borisisok
Copy link
Author

borisisok commented Aug 17, 2021

pi@raspberrypi:~/gst $ v4l2-ctl -d /dev/video0 -l

                     brightness 0x00980900 (int)    : min=0 max=255 step=1 default=128 value=167
                       contrast 0x00980901 (int)    : min=0 max=255 step=1 default=32 value=32
                     saturation 0x00980902 (int)    : min=0 max=255 step=1 default=32 value=32
 white_balance_temperature_auto 0x0098090c (bool)   : default=1 value=1
                           gain 0x00980913 (int)    : min=0 max=255 step=1 default=64 value=128
           power_line_frequency 0x00980918 (menu)   : min=0 max=2 default=2 value=2
      white_balance_temperature 0x0098091a (int)    : min=2800 max=6500 step=1 default=5500 value=3482 flags=inactive
                      sharpness 0x0098091b (int)    : min=0 max=255 step=1 default=22 value=22
         backlight_compensation 0x0098091c (int)    : min=0 max=1 step=1 default=1 value=1
                  exposure_auto 0x009a0901 (menu)   : min=0 max=3 default=3 value=3
              exposure_absolute 0x009a0902 (int)    : min=3 max=2047 step=1 default=166 value=996 flags=inactive
         exposure_auto_priority 0x009a0903 (bool)   : default=0 value=0
                   pan_absolute 0x009a0908 (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                  tilt_absolute 0x009a0909 (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                 focus_absolute 0x009a090a (int)    : min=0 max=255 step=5 default=60 value=0 flags=inactive
                     focus_auto 0x009a090c (bool)   : default=1 value=1
                  zoom_absolute 0x009a090d (int)    : min=1 max=5 step=1 default=1 value=3
                      led1_mode 0x0a046d05 (menu)   : min=0 max=3 default=3 value=1
                 led1_frequency 0x0a046d06 (int)    : min=0 max=255 step=1 default=0 value=0

pi@raspberrypi:~/gst $ v4l2-ctl -d /dev/video0 -L

                     brightness 0x00980900 (int)    : min=0 max=255 step=1 default=128 value=167
                       contrast 0x00980901 (int)    : min=0 max=255 step=1 default=32 value=32
                     saturation 0x00980902 (int)    : min=0 max=255 step=1 default=32 value=32
 white_balance_temperature_auto 0x0098090c (bool)   : default=1 value=1
                           gain 0x00980913 (int)    : min=0 max=255 step=1 default=64 value=128
           power_line_frequency 0x00980918 (menu)   : min=0 max=2 default=2 value=2
                                0: Disabled
                                1: 50 Hz
                                2: 60 Hz
      white_balance_temperature 0x0098091a (int)    : min=2800 max=6500 step=1 default=5500 value=3482 flags=inactive
                      sharpness 0x0098091b (int)    : min=0 max=255 step=1 default=22 value=22
         backlight_compensation 0x0098091c (int)    : min=0 max=1 step=1 default=1 value=1
                  exposure_auto 0x009a0901 (menu)   : min=0 max=3 default=3 value=3
                                1: Manual Mode
                                3: Aperture Priority Mode
              exposure_absolute 0x009a0902 (int)    : min=3 max=2047 step=1 default=166 value=996 flags=inactive
         exposure_auto_priority 0x009a0903 (bool)   : default=0 value=0
                   pan_absolute 0x009a0908 (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                  tilt_absolute 0x009a0909 (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                 focus_absolute 0x009a090a (int)    : min=0 max=255 step=5 default=60 value=0 flags=inactive
                     focus_auto 0x009a090c (bool)   : default=1 value=1
                  zoom_absolute 0x009a090d (int)    : min=1 max=5 step=1 default=1 value=3
                      led1_mode 0x0a046d05 (menu)   : min=0 max=3 default=3 value=1
                                0: Off
                                1: On
                                2: Blink
                                3: Auto
                 led1_frequency 0x0a046d06 (int)    : min=0 max=255 step=1 default=0 value=0

@borisisok
Copy link
Author

borisisok commented Sep 1, 2021

try the same (mjpeg cam steaming) using vlc

https://wiki.videolan.org/Documentation:Modules/v4l2/

vlc v4l2:///dev/video0:chroma=mjpg:width=1280:height=720

https://www.lewisroberts.com/2015/05/15/raspberry-pi-mjpeg-at-30fps/
v4l2-ctl -c sharpness=30,compression_quality=100,video_bitrate_mode=1,video_bitrate=25000000

@borisisok
Copy link
Author

Find and select rec format via v4l2-ctl

root@mediapi:~# v4l2-ctl --list-formats-ext -d /dev/video0
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

    [0]: 'YUYV' (YUYV 4:2:2)
            Size: Discrete 640x360
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.040s (25.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 320x240
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.040s (25.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 640x480
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.040s (25.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 960x720
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 1280x720
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)
    [1]: 'MJPG' (Motion-JPEG, compressed)
            Size: Discrete 640x360
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.040s (25.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 320x240
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.040s (25.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 640x480
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.040s (25.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 960x720
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.040s (25.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 1280x720
                    Interval: Discrete 0.033s (30.000 fps)
                    Interval: Discrete 0.040s (25.000 fps)
                    Interval: Discrete 0.050s (20.000 fps)
                    Interval: Discrete 0.067s (15.000 fps)
                    Interval: Discrete 0.100s (10.000 fps)
                    Interval: Discrete 0.200s (5.000 fps)

root@mediapi:~# v4l2-ctl -d /dev/video0 --set-parm=15
Frame rate set to 15.000 fps

v4l2-ctl --set-fmt-video=width=640,height=480,pixelformat=JPEG --stream-mmap --stream-count=100 --stream-to=pics@30fps.jpeg

@borisisok
Copy link
Author

pi@raspicam2:~ $ v4l2-ctl -D -d /dev/video0
Driver Info:
Driver name : sonixj
Card type : USB camera
Bus info : usb-20980000.usb-1.4
Driver version : 5.10.17
Capabilities : 0x85200001
Video Capture
Read/Write
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x05200001
Video Capture
Read/Write
Streaming
Extended Pix Format

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