树莓派摄像头的使用

2021-05-12, updated 2021-09-12

准备一个树莓派,一个摄像头模块

开启camera

  1. 执行sudo raspi-config
┌─────────────────────────────┤ Raspberry Pi Software Configuration Tool (raspi-config) ├──────────────────────────────┐ 
│                                                                                                                      │ 
│                           1 System Options       Configure system settings                                           │ 
│                           2 Display Options      Configure display settings                                          │ 
│                           3 Interface Options    Configure connections to peripherals                                │ 
│                           4 Performance Options  Configure performance settings                                      │ 
│                           5 Localisation Options Configure language and regional settings                            │ 
│                           6 Advanced Options     Configure advanced settings                                         │ 
│                           8 Update               Update this tool to the latest version                              │ 
│                           9 About raspi-config   Information about this configuration tool                           │ 
│                                                                                                                      │ 
│                                                                                                                      │ 
│                                                                                                                      │ 
│                                                                                                                      │ 
│                                                                                                                      │ 
│                                  <Select>                                  <Finish>                                  │ 
│                                                                                                                      │ 
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
  1. 选择 3
┌─────────────────────────────┤ Raspberry Pi Software Configuration Tool (raspi-config) ├──────────────────────────────┐ 
│                                                                                                                      │ 
│                        P1 Camera      Enable/disable connection to the Raspberry Pi Camera                           │ 
│                        P2 SSH         Enable/disable remote command line access using SSH                            │ 
│                        P3 VNC         Enable/disable graphical remote access using RealVNC                           │ 
│                        P4 SPI         Enable/disable automatic loading of SPI kernel module                          │ 
│                        P5 I2C         Enable/disable automatic loading of I2C kernel module                          │ 
│                        P6 Serial Port Enable/disable shell messages on the serial connection                         │ 
│                        P7 1-Wire      Enable/disable one-wire interface                                              │ 
│                        P8 Remote GPIO Enable/disable remote access to GPIO pins                                      │ 
│                                                                                                                      │ 
│                                                                                                                      │ 
│                                                                                                                      │ 
│                                                                                                                      │ 
│                                                                                                                      │ 
│                                  <Select>                                  <Back>                                    │ 
│                                                                                                                      │ 
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
  1. 选择P1 Camera
 ┌──────────────────────────────────────────────────────────┐
 │                                                          │
 │ Would you like the camera interface to be enabled?       │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │                                                          │
 │               <Yes>                  <No>                │
 │                                                          │
 └──────────────────────────────────────────────────────────┘
  1. 选择Yes 然后重启系统

测试是否可以正常使用

执行raspistill -o image.jpg看是否可以拍照成功

通过vlc实现http视频播放

在树莓派中安装vlc

sudo apt update
sudo apt install vlc

执行以下代码

raspivid -o - -t 0  -fps 30 -b 6000000 -ex auto -awb auto -vs  |cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8080}' :demux=h264

在同一个局域网的电脑中,使用视频播放器打开url http://192.168.1.9:8080, 不可以用浏览器打开

直播推流

[email protected]:~ $ cat obs-start.sh 
#!/bin/sh

rtmpurl=""
rtmpval=""

if tmux has-session -t 'obs-start'; then
        echo "obs is already running."
        echo "To stop it use: ./obs-stop.sh"
        echo "To attach to it use: tmux attach -t obs"
else
        tmux new-session -s 'obs' -d
        tmux send-keys 'raspivid -o - -t 0 -fps 24 -b 6000000 -ex auto | ffmpeg -re -stream_loop -1 -f h264 -i - -vcodec copy -acodec aac -b:a 192k -f flv "'$rtmpurl$rtmpval'"' 'C-m'
        #tmux split-window
        #tmux select-layout tiled 2>/dev/null
        tmux rename-window 'obs'

        echo "obs has been started in tmux session 'obs'"
        echo "To attach to session, use: tmux attach -t obs"
        echo "To switch between panes use Ctrl+B-ArrowKey"
        echo "To deattach, press Ctrl+B-D"
        echo "To stop obs, use: ./obs-stop.sh"
fi
[email protected]:~ $ cat obs-stop.sh 
#!/bin/sh
if tmux has-session -t 'obs' 2>/dev/null; then
        tmux kill-session -t obs 2>/dev/null
        echo "obs has been stopped."
else
        echo "obs is not running!"
fi

raspivid 部分参数解释

words: 1153 tags: raspberry camera