Raspberry Pi 视频采集
Easycap D60
最近入手了树莓派,而家里又装了机顶盒。 所以准备用树莓派采集机顶盒的视频, 然后再作为服务器将视频播放出来。这样用手机,ipad或PC都可以看电视了。
当然在网上也可以看电视,但老妈竟让抱怨在ipad看电视竟然断线。 所以就产生了这个想法。
首先是视频采集部分。在淘宝购入一usb采集卡,40左右。型号是Easycap D60. 关于这个东西的信息可以参考:
http://linuxtv.org/wiki/index.php/Easycap#Known_Easycap_devices
简单的说就是有很多山寨货,十分混乱,买的时候一定要问清是要stk1160 三芯片的卡。
采集卡就是这个样子的:
现在最新的kernel中已经带有这个的驱动,但如果买的卡decoder芯片是GM7113的话, 现在3.10 的kernel上的驱动有问题。所以要去下个3.12的kernel,找出saa7115的驱动 (driver/media/i2c/saa7115.c),自行编个driver放进去才可以用,不然就是绿屏。
The EasyCAP DC60 works with the following applications on Linux
- mplayer / mencoder
- Tvtime
- Vlc
- Xawtv
- Zoneminder
- motion
- gstreamer (cheese; you cannot select a different input than 0 in cheese)
- gmerlin recorder
- sox (audio)
至于如何编译kernel,可以参考
http://shumeipai.nxez.com/2013/10/09/raspberry-pi-kernel-cross-compiler.html
vlc player
Using the grafical interface for viewing:
In the main menu select "Media" > "Open Capture device"
-
into the field "Video device name" type:
/dev/video0:input=1
-
into the field "Audio device name" type:
plughw:1,0
- (change the numbers according to your system if necessary)
If you have OSS audio:
-
into the field "Audio device name" type:
/dev/dsp
- check the 'Show more options' checkbox and
- change the line in the 'Edit Options' dialog from "alsa" to "oss"
- Now click on "Play" if you want to view with vlc.
mp4 - avi
/usr/bin/cvlc v4l2:///dev/video0:input=1:width=720:height=576 --input-slave=alsa://plughw:1,0 --run-time 10 --sout='#transcode{vcodec=mp4v, vb=1024, acodec=mpga, ab=128}:std{access=file, mux=avi, dst=file_name.avi}' vlc://quit;
MPEG - DVD
/usr/bin/cvlc v4l2:///dev/video1:input=1:width=720:height=576 --input-slave=alsa://plughw:1,0 --run-time 10 --sout='#transcode{vcodec=mp2v, vb=5000, acodec=mp2a, ab=128}:std{access=file, mux=mpeg1, dst=file_name.mpg}' vlc://quit;
MPlayer
然后安装mplayer
sudo apt-get update sudo apt-get install mplayer
如果只要画面不要声音的话,运行:
mplayer tv:// -tv driver=v4l2:norm=NTSC-M:width=720:height=480:outfmt=uyvy:device=/dev/video0:input=0
就可以看到画面了。如果要加上声音:
mplayer tv:// -tv driver=v4l2:norm=NTSC-M:width=720:height=480:outfmt=uyvy:device=/dev/video0:input=0:fps=30:alsa:amode=1:forcechan=2:audiorate=48000:adevice=plughw.1,0:forceaudio:immediatemode=0 -ao sdl,alsa
如果是自带摄像头和麦克的笔记本电脑,/dev/video0
和plughw.1,0
可能已经被占了。
要换成/dev/video1
和plughw.2,0
:
mplayer tv:// -tv driver=v4l2:norm=NTSC-M:width=720:height=480:outfmt=uyvy:device=/dev/video1:input=0:fps=30:alsa:amode=1:forcechan=2:audiorate=48000:adevice=plughw.2,0:forceaudio:immediatemode=0 -ao sdl,alsa
Viewing with MPlayer OSS:
mplayer tv:// -tv driver=v4l2:norm=PAL:width=720:height=576:outfmt=uyvy:device=/dev/video0:input=1:fps=25:amode=1:forcechan=2:audiorate=48000:adevice=/dev/dsp:forceaudio:immediatemode=0 -ao sdl ,oss
如果输入的视频是PAL格式:
mplayer tv:// -tv driver=v4l2:norm=PAL:width=720:height=576:outfmt=uyvy:device=/dev/video0:input=1:fps=25:alsa:amode=1:forcechan=2:audiorate=48000:adevice=hw.1,0:forceaudio:immediatemode=0 -vf pp=lb -aspect 16:9 -ao sdl -vo sdl
参数说明:
-
mplayer tv://
-
Switches mplayer to analogue TV mode. This command alone will work if all other needed options are specified in
~/.mplayer/config
.
-
Switches mplayer to analogue TV mode. This command alone will work if all other needed options are specified in
-
-tv driver=v4l2:norm=PAL:width=720:height=576:outfmt=uyvy:device=/dev/video0:input=1:fps=25
-
With the
-tv
option all settings for TV capturing are defined. Norm, device and input must be set right to see a picture. The FPS setting is only needed if mplayer doesn't recognize the right framerate. Consult mplayers manpage for more detailed description.
-
With the
-
:alsa:amode=1:forcechan=2:audiorate=48000:adevice=hw.1,0:forceaudio:immediatemode=0
-
This line is the ALSA audio part of the TV settings. The audiorate must be appropriate for your device,
8000
is low quality but always safe. -
If you do not want to capture audio, cut out this part of the command and add the
-nosound
option at the end of the mplayer command
-
This line is the ALSA audio part of the TV settings. The audiorate must be appropriate for your device,
-
-vf pp=lb -aspect 16:9 -ao sdl -vo sdl
-
Additional options:
-vf
opens a filter chain,-aspect
sets the aspect of the mplayer window,-ao sdl
defines sdl as audio-out driver and-vo sdl
defines sdl as video-out driver to avoid picture problems (flickering, picture freeze). Read more about filters below at MEncoder.
-
Additional options:
More information about TV capturing with mplayer on linuxtv.org.
FFmpeg: Avplay
Avplay template command for viewing any v4l2 source:
avplay -f video4linux2 /dev/video0
Avplay template command for viewing with sound:
avplay -f video4linux2 -channel 1 -i /dev/video0 | avplay -f alsa -i hw:1 -nodisp
FFmpeg: Avconv
Avconv template command for creating a DivX 5 compatible mpeg4-mp3 video file (~10 MB/minute)
avconv -f video4linux2 -channel 1 -i /dev/video0 -f alsa -i hw:1 -vcodec mpeg4 -vtag DX50 -b 1200k -r 25 -acodec libmp3lame -ar 44100 -ac 2 -ab 128k -vf yadif,scale=720:406 -y test.avi
FFmpeg
This line creates divx5 avi files
ffmpeg -f video4linux2 -aspect 1.3333 -s 720x576 -r 25 -vc 1 -pix_fmt yuv420p -i /dev/video0 -f alsa -ar 44100 -ac 2 -acodec pcm_s16le -i hw:1 -vcodec mpeg4 -vtag DX50 -b 1200k -r 25 -acodec libmp3lame -ar 44100 -ac 2 -ab 128k -y test.avi
This line creates DVD mpeg files
ffmpeg -v 9 -vsync 1 -map 0.0:1.0 -map 1.0 -tvstd PAL_BGHIN -f video4linux2 -pix_fmt uyvy422 -r 25 -s 720x576 -aspect 4:3 -i /dev/easycap0 -f oss -ac 2 -ab 192K -ar 48000 -i /dev/easysnd1 -b 4000k -bt 300k -acodec mp2 -ac 2 -ab 192k -a 48000 -vcodec mpeg2video -pix_fmt uyvy422 -me_method epzs -threads 4 -f vob test.mpg
推送流直播
使用FFmpeg
将你要直播推流的视频文件转换为flv格式,因为视频流直播只支持flv格式, 在后面推流的过程中,如果是其他格式有可能可以转换为flv,为什么说有可能呢, 因为事实证明mp4格式就不能转换为flv,在推流的过程中只有声音没有图像,很是蛋疼。
把文件推上直播流,在树莓派终端输入以下代码:
ffmpeg -i "./sample.flv" -f flv "rtmp://dl.live.acg.tv/live-dl/你的直播码" \ -re -vcodec copy -acodec aac -b:a 192k
-
-f flv
指定输出格式,这个必须是flv才能推到直播服务器。 -
-i
指定输入文件地址。 -
-re
按照视频的FPS进行推流。 -
-vcodec copy
指定视频编码为复制。 -
-acodec aac
音频使用aac编码。 -
-b:a 192k
则是指定码率。
把采集卡的视频推上直播流:
ffmpeg -i /dev/video1 -f flv "rtmp://elsa.jade-dungeon.net/hls/video01" \ -f video4linux2 -r 6 -s 720x480 \ -f alsa -i hw:2 -ab 16 -ar 22050 -ac 1 -f mp3 # ffmpeg -f video4linux2 -r 6 -s 720x480 -i /dev/video1 -f alsa -i hw:2 -ab 16 -ar 22050 -ac 1 -f mp3 -f flv "rtmp://elsa.jade-dungeon.net/hls/video01"
把采集卡的视频推上直播流另一个例子:
ffmpeg -i /dev/video0 -f flv "rtmp://127.0.0.1/rtmpsvr/rtmp1" \ -f video4linux2 -r 12 -s 640x480 -qscale 10 \ -f alsa -i hw:1 -ab 16 -ar 22050 -ac 1 -f mp3 # ffmpeg -f video4linux2 -qscale 10 -r 12 -s 640x480 -i /dev/video0 -f alsa -i hw:1 -ab 16 -ar 22050 -ac 1 -f mp3 -f flv rtmp://127.0.0.1/rtmpsvr/rtmp1
把采集卡的视频推上直播流另一个例子:
avconv -f video4linux2 -i /dev/video0 -q 1 -f alsa -i plughw:1,0 -ab 16000 -ar 22050 -ac 1 -q 1 -f flv rtmp://127.0.0.1:1935/rtmp/live -threads 2
使用GSteamer
avconv 采用软编码实现,因此 CPU 消耗比较高,推荐用 GStreamer
#安装 GStreamer sudo apt-get install gstreamer1.0-tools #安装 GStreamer 扩展组件 sudo apt-get install libgstreamer1.0-0 libgstreamer1.0-0-dbg libgstreamer1.0-dev liborc-0.4-0 liborc-0.4-0-dbg liborc-0.4-dev liborc-0.4-doc gir1.2-gst-plugins-base-1.0 gir1.2-gstreamer-1.0 gstreamer1.0-alsa gstreamer1.0-doc gstreamer1.0-omx gstreamer1.0-plugins-bad gstreamer1.0-plugins-bad-dbg gstreamer1.0-plugins-bad-doc gstreamer1.0-plugins-base gstreamer1.0-plugins-base-apps gstreamer1.0-plugins-base-dbg gstreamer1.0-plugins-base-doc gstreamer1.0-plugins-good gstreamer1.0-plugins-good-dbg gstreamer1.0-plugins-good-doc gstreamer1.0-plugins-ugly gstreamer1.0-plugins-ugly-dbg gstreamer1.0-plugins-ugly-doc gstreamer1.0-pulseaudio gstreamer1.0-tools gstreamer1.0-x libgstreamer-plugins-bad1.0-0 libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-base1.0-0 libgstreamer-plugins-base1.0-dev
gst-launch-1.0 -v v4l2src device=/dev/video0 ! 'video/x-raw, width=1024, height=768, framerate=30/1' ! queue ! videoconvert ! omxh264enc ! h264parse ! flvmux ! rtmpsink location='rtmp://树莓派的IP地址/live live=1'