由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Blive with RasperryPi

Reference: Bilibili: 五维谐振子

Qick Start

  1. sudo apt upgrade
  2. sudo apt update
  3. sudo apt install ffmpeg
  4. sudo raspivid -o - -t 0 -w 1280 -h 720 -fps 24 -b 1000000 | ffmpeg -f h264 -i - -vcodec copy -acodec aac -b:a 192k -f flv "你的rtmp地址/你的直播码"

降低延迟:

CSDN: 北雨南萍

  1. sudo raspivid -o - -t 0 -w 1280 -h 720 -fps 24 -b 1000000 |
  2. ffmpeg -f h264 -i - -vcodec copy -r 30 -acodec aac -b:a 100k -preset ultrafast -tune zerolatency -f flv "你的rtmp地址/你的直播码"

raspivid

树莓派实验室: Spoony

  1. "raspivid" Camera App (commit 06bc6daa0213 Tainted)
  2. Display camera output to display, and optionally saves an H264 capture at requested bitrate
  3. usage: raspivid [options]
  4. Image parameter commands
  5. -b, --bitrate : Set bitrate. Use bits per second (e.g. 10MBits/s would be -b 10000000)
  6. -t, --timeout : Time (in ms) to capture for. If not specified, set to 5s. Zero to disable
  7. -d, --demo : Run a demo mode (cycle through range of camera options, no capture)
  8. -fps, --framerate : Specify the frames per second to record
  9. -e, --penc : Display preview image *after* encoding (shows compression artifacts)
  10. -g, --intra : Specify the intra refresh period (key frame rate/GoP size). Zero to produce an initial I-frame and then just P-frames.
  11. -pf, --profile : Specify H264 profile to use for encoding
  12. -td, --timed : Cycle between capture and pause. -cycle on,off where on is record time and off is pause time in ms
  13. -s, --signal : Cycle between capture and pause on Signal
  14. -k, --keypress : Cycle between capture and pause on ENTER
  15. -i, --initial : Initial state. Use 'record' or 'pause'. Default 'record'
  16. -qp, --qp : Quantisation parameter. Use approximately 10-40. Default 0 (off)
  17. -ih, --inline : Insert inline headers (SPS, PPS) to stream
  18. -sg, --segment : Segment output file in to multiple files at specified interval <ms>
  19. -wr, --wrap : In segment mode, wrap any numbered filename back to 1 when reach number
  20. -sn, --start : In segment mode, start with specified segment number
  21. -sp, --split : In wait mode, create new output file for each start event
  22. -c, --circular : Run encoded data through circular buffer until triggered then save
  23. -x, --vectors : Output filename <filename> for inline motion vectors
  24. -if, --irefresh : Set intra refresh type
  25. -fl, --flush : Flush buffers in order to decrease latency
  26. -pts, --save-pts : Save Timestamps to file for mkvmerge
  27. -cd, --codec : Specify the codec to use - H264 (default) or MJPEG
  28. -lev, --level : Specify H264 level to use for encoding
  29. -r, --raw : Output filename <filename> for raw video
  30. -rf, --raw-format : Specify output format for raw video. Default is yuv
  31. -l, --listen : Listen on a TCP socket
  32. -stm, --spstimings : Add in h.264 sps timings
  33. -sl, --slices : Horizontal slices per frame. Default 1 (off)
  34. H264 Profile options :
  35. baseline,main,high
  36. H264 Level options :
  37. 4,4.1,4.2
  38. H264 Intra refresh options :
  39. cyclic,adaptive,both,cyclicrows
  40. Raw output format options :
  41. yuv,rgb,gray
  42. Raspivid allows output to a remote IPv4 host e.g. -o tcp://192.168.1.2:1234or -o udp://192.168.1.2:1234
  43. To listen on a TCP port (IPv4) and wait for an incoming connection use the -l option
  44. e.g. raspivid -l -o tcp://0.0.0.0:3333 -> bind to all network interfaces,
  45. raspivid -l -o tcp://192.168.1.1:3333 -> bind to a certain local IPv4 port
  46. Common Settings commands
  47. -?, --help : This help information
  48. -w, --width : Set image width <size>
  49. -h, --height : Set image height <size>
  50. -o, --output : Output filename <filename> (to write to stdout, use '-o -'). If not specified, no file is saved
  51. -v, --verbose : Output verbose information during run
  52. -cs, --camselect : Select camera <number>. Default 0
  53. -md, --mode : Force sensor mode. 0=auto. See docs for other modes available
  54. -gps, --gpsdexif : Apply real-time GPS information to output (e.g. EXIF in JPG, annotation in video (requires libgps.so.23)
  55. Preview parameter commands
  56. -p, --preview : Preview window settings <'x,y,w,h'>
  57. -f, --fullscreen : Fullscreen preview mode
  58. -op, --opacity : Preview window opacity (0-255)
  59. -n, --nopreview : Do not display a preview window
  60. -dn, --dispnum : Display on which to display the preview window (dispmanx/tvservice numbering)
  61. Image parameter commands
  62. -sh, --sharpness : Set image sharpness (-100 to 100)
  63. -co, --contrast : Set image contrast (-100 to 100)
  64. -br, --brightness : Set image brightness (0 to 100)
  65. -sa, --saturation : Set image saturation (-100 to 100)
  66. -ISO, --ISO : Set capture ISO
  67. -vs, --vstab : Turn on video stabilisation
  68. -ev, --ev : Set EV compensation - steps of 1/6 stop
  69. -ex, --exposure : Set exposure mode (see Notes)
  70. -fli, --flicker : Set flicker avoid mode (see Notes)
  71. -awb, --awb : Set AWB mode (see Notes)
  72. -ifx, --imxfx : Set image effect (see Notes)
  73. -cfx, --colfx : Set colour effect (U:V)
  74. -mm, --metering : Set metering mode (see Notes)
  75. -rot, --rotation : Set image rotation (0-359)
  76. -hf, --hflip : Set horizontal flip
  77. -vf, --vflip : Set vertical flip
  78. -roi, --roi : Set region of interest (x,y,w,d as normalised coordinates [0.0-1.0])
  79. -ss, --shutter : Set shutter speed in microseconds
  80. -awbg, --awbgains : Set AWB gains - AWB mode must be off
  81. -drc, --drc : Set DRC Level (see Notes)
  82. -st, --stats : Force recomputation of statistics on stills capture pass
  83. -a, --annotate : Enable/Set annotate flags or text
  84. -3d, --stereo : Select stereoscopic mode
  85. -dec, --decimate : Half width/height of stereo image
  86. -3dswap, --3dswap : Swap camera order for stereoscopic
  87. -ae, --annotateex : Set extra annotation parameters (text size, text colour(hex YUV), bg colour(hex YUV), justify, x, y)
  88. -ag, --analoggain : Set the analog gain (floating point)
  89. -dg, --digitalgain : Set the digital gain (floating point)
  90. -set, --settings : Retrieve camera settings and write to stdout

python + openCVC + ffmpeg

more: CSDN: mind_programmonkey

基本框架

  1. import cv2 as cv
  2. import subprocess as sp
  3. rtmpUrl = "你的rtmp地址/你的直播码"
  4. camera_path = "/dev/video0"
  5. cap = cv.VideoCapture(camera_path)
  6. ## Get video information
  7. fps = 30 #int(cap.get(cv.CAP_PROP_FPS))
  8. width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
  9. height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
  10. ## ffmpeg command
  11. command = ['ffmpeg',
  12. '-y',
  13. '-f', 'rawvideo',
  14. '-vcodec','rawvideo',
  15. '-pix_fmt', 'bgr24',
  16. '-s', "{}x{}".format(width, height),
  17. '-r', str(fps),
  18. '-i', '-',
  19. '-c:v', 'libx264',
  20. '-pix_fmt', 'yuv420p',
  21. '-preset', 'ultrafast',
  22. '-f', 'flv',
  23. rtmpUrl]
  24. ## 管道配置
  25. p = sp.Popen(command, stdin=sp.PIPE)
  26. ## read webcamera
  27. while(cap.isOpened()):
  28. ret, frame = cap.read()
  29. if not ret:
  30. print("Opening camera is failed")
  31. break
  32. # process frame
  33. # your code
  34. # process frame
  35. # write to pipe
  36. p.stdin.write(frame.tostring())

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗