James Townsend
James Townsend

Reputation: 55

Passing processed Video from OpenCV to FFmpeg for HLS streaming (Raspberry PI)

Hi I a have a question I have openCV and ffmpeg on the Raspberry Pi and I am trying to stream live video from the raspberry pi. At the moment I have the output output of openCV saving as a .avi file and I have a command for ffmpeg ffmpeg -i out.avi -hls_segment_filename '%03d.ts' stream.m3u8 This Command take the output creates the playlist(.m3u8) and the segments(.ts).

At present I have openCV programmed in C++ (this can not change) I have an executable programmed from this and I have both the executable C++ and the above ffmpeg in a Bash Script.

#!/bin/bash

while true; do
./OpenCV
ffmpeg -i out.avi -hls_segment_filename '%03d.ts' stream.m3u8
done

This does allow me to stream the processed openCV video my issue is as the Bash script is in a while loop it keeps resetting the playlist and the .ts files, so i have to constantly press play on the client connection.

Is there anyway around this?

I tried including a variable that would increment every loop but if i replace '%03d' with this i get an error.

Upvotes: 1

Views: 3077

Answers (1)

Svetlin Mladenov
Svetlin Mladenov

Reputation: 4427

If you insist on using your program (OpenCV) and ffmpeg in a loop then you can specify the initial hls sequence number for stream.m3u8 using start_number. Something like this:

... as before ...
ffmpeg -i out.avi -hls_segment_filename '%03d.ts' --start_number $I stream.m3u8

where I is a variable that you have to increment each time the loop runs. But this approach is very fragile and will probably result in an incorrect stream because it assumes that ffmpeg will produce only a single segment but in reality it will probably produce multiple segments.

A much better approach is to run OpenCV and ffmpeg in parallel and make them talk to each other. By doing so there will be no need to write to a temporary file out.avi and run OpenCV and ffmpeg in sequences and keep the media sequences synchronized.

I think you can hack it like this. Note that you may need to change OpenCV so that it writes constantly to out.avi and does not return after a while:

./OpenCV &
tail -n +0 -f out.avi  | ffmpeg -i pipe:0 -hls_segment_filename '%03d.ts' stream.m3u8

A better approach is change your program to write to stdout or to a named pipe and run it like so:

./OpenCV | ffmpeg -i pipe:0 -hls_segment_filename '%03d.ts' stream.m3u8

Upvotes: 2

Related Questions