1

I have a single mpeg-ts input stream, http://192.168.1.100/stream0, and I wish to do two things with this stream. One output is to be a hls multipart stream with m3u8 so apache can serve to local clients in a browser. The other output is to copy the stream into a mp4 container that is 5 minutes long. The goal being to have a constant live stream that can be viewed, whilst also a number of 5 min long mp4 files saved to disk.

The command below accomplishes part of what I need:

ffmpeg -re -i http://192.168.1.100/stream0 -c:v copy -c:a copy -preset veryfast \
       -f hls -hls_time 3 -segment_list_flags +live \
           -hls_flags second_level_segment_index\
                      +second_level_segment_size\
                      +second_level_segment_duration \
                      -hls_flags delete_segments \
                      -hls_segment_filename \
                      /var/www/html/live-%1d.ts \
                      /var/www/html/live.m3u8 \
        -f mpegts -c:v copy -c:a copy -y -t 300 \
            /store/archive/archive.$(date +%Y%m%d-%H%M%S).mp4"

The problem is the 2nd part, creates just one file and doesn't continue to create multiple files.

I can run one ffmpeg to create the live stream, then run another instance using that live stream as the input within a loop so the it creates multiple files. Ideally I think I need to use Pipes, but I either end up with out multiple mp4 files, or a live stream that keeps freezing.

What would be the best way to approach this ?

Regards Jon

3
  • 1
    Use the segment muxer for the 2nd output.
    – Gyan
    Commented Jun 11 at 15:58
  • -f mpegts overrides the earlier -f segment. -t 300 will stop processing after the first 300 seconds. Remove these. You will also need to set -strftime 1 if you are going to have datetime specifiers in the output pattern.
    – Gyan
    Commented Jun 13 at 18:20
  • Many thanks for highlighting this to point me in the right direction.
    – Jon37
    Commented Jun 14 at 11:10

1 Answer 1

1

Many thanks to Ryan for pointing out a path forward.

For the clarity of others, and in case anyone else is looking to achieve something similar. I now have a working solution with the following FFmpeg command:


ffmpeg -re -i http://192.168.1.100/stream0 -preset veryfast \
  -f hls -hls_time 2 -segment_list_flags +live \
     -hls_flags delete_segments \
     -hls_segment_type mpegts \
     -hls_segment_filename /var/www/html/livestream-%4d.ts \
        "/var/www/html/livestream.m3u8" \
  -f segment -segment_time 300 -c:a copy -c:v copy  \
        "/var/www/html/5min-savedvideo-part-%4d.mp4"

This provides a live stream from the imput mpeg-ts stream at http://localhost/livestream.m3u8 it also produces a series of 5 min (depending on keyframe) long mp4 files.

I couldn't get the "-strftime" functionality to work to include a timestamp in the filename of the mp4 file. This however in my case was a nice to have, but I'm happy to rely on the timestamp of the file so didn't progress this further.

The earlier issues I was having was due to a couple of issues, one being simple cpu power not able to keep up with the input stream, the other being my lack of knowledge/skill with the powerful ffmpeg tool.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .