Saturday, January 14, 2017

Live Preview Streaming from the Nikon 3200 using gphoto2 and ffmpeg


I tried to get a live preview streaming from my Nikon. I found some instructions.




gphoto2 has a command --capture-preview, which basically records the live feed from the camera in an mjpeg file. It can pipe out the movie to stdout.

On the same Linux Box (Centos7) I succeeded these


In one terminal window, started mplayer, to 'listen' to a TCP connection at port 5001
mplayer -demuxer mpegts 'ffmpeg://tcp://127.0.0.1:5001?listen'

On another window, I started the capture and sent it to the port

gphoto2 --capture-movie --stdout | ffmpeg -f mjpeg -i pipe:0 -r 20 -vcodec libx264 -pix_fmt yuv420p -tune zerolatency -preset ultrafast -f mpegts "tcp://127.0.0.1:5001"

This seem to work, i was able to see in the mplayer window the video coming from the camera.
There is an inconvenience, there is a considerable lag.


Another option is to use ffserver. This way, multiple client players (including web browsers) can connect.
The idea is that, gphoto2 -> ffmpeg provides the live feed, while ffserver does the 'broadcast'.
For ffserver, a ffserver.conf needs to be provided, here is a custom one.

Each stream was marked with NoAudio

HttpPort 5001
MaxHTTPConnections 200
MaxClients 100
MaxBandwidth 1000000

<Feed feed1.ffm>
File /tmp/feed1.ffm 
FileMaxSize 5M 
</Feed>



<Stream test.mjpg>
    Feed feed1.ffm
    Format mpjpeg
    VideoSize 640x480
    VideoFrameRate 15
    VideoBitRate 1024
    VideoIntraOnly
    NoAudio
    Strict -1
</Stream>


<Stream live.ts>
 Format mpegts
 Feed feed1.ffm
 VideoCodec libx264
    VideoSize 640x480
 VideoFrameRate 25
 VideoBitRate 1024
    VideoGopSize 5
 PixelFormat yuv420p
    NoAudio
</Stream>


# Flash

<Stream test.swf>
    Feed feed1.ffm
    Format swf
 VideoCodec flv
    VideoSize 640x480
 VideoFrameRate 25
 VideoBitRate 1024
    VideoGopSize 5
 PixelFormat yuv420p
    NoAudio
</Stream>

<Stream stat.html>
Format status

# Only allow local people to get the status
ACL allow localhost
ACL allow 192.168.0.0 192.168.255.255

</Stream>



To start the server, type
ffserver -f your-ffserver.config

One the server is up and running, start the live feed
gphoto2 --capture-movie --stdout | ffmpeg -f mjpeg -re -i pipe:0 http://localhost:5001/feed1.ffm


At this point, you can point your browser to
http://localhost:5051/stats.html. You will see the links to the streams.

References

https://trac.ffmpeg.org/wiki/StreamingGuide
https://trac.ffmpeg.org/wiki/ffserver

No comments: