Infinite stream from a video file (in a loop)

32.5k views Asked by At

Is there any way howto create an infinite h264 stream from a video file (eg. mp4, avi, ...). I'd like to use ffmpeg to transcode avi file to h264 but there's no loop option for output.

3

There are 3 answers

3
budthapa On BEST ANSWER

No you can't. There is no such command in ffmpeg to loop video. You can use -loop for image only. If you are interested you can use concat demuxer. Create a playlist file e.g. playlist.txt

Inside playlist.txt add the video location

file '/path/to/video/video.mp4'
file '/path/to/video/video.mp4'
file '/path/to/video/video.mp4'
file '/path/to/video/video.mp4'
file '/path/to/video/video.mp4'

Run ffmpeg

ffmpeg -f concat -i playlist.txt -c copy output.mp4

See here

0
Leszek Szary On

If you want to have a live stream by looping a single video file then you could split it into .ts files and then simulate .m3u8 file with a php script which would return different .ts based on current time. You could try something similar to this:

<?php
// lets assume that we have stream splitted to parts named testXXXXX.ts
// and all parts have 2.4 seconds and we want to play in loop part
// from test0.ts to test29.ts forever in a live stream
header('Content-Type: application/x-mpegURL');
$time = intval(time() / 2.40000);
$s1 = ($time + 1) % 30;
$s2 = ($time + 2) % 30;
$s3 = ($time + 3) % 30;
$s4 = ($time + 4) % 30;
?>
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:2
#EXT-X-MEDIA-SEQUENCE:<?php echo "$time\n"; ?>
#EXTINF:2.40000,
test<?php echo $s1; ?>.ts
<?php if ($s2 < $s1) echo "#EXT-X-DISCONTINUITY\n"; ?>
#EXTINF:2.40000,
test<?php echo $s2; ?>.ts
<?php if ($s3 < $s2) echo "#EXT-X-DISCONTINUITY\n"; ?>
#EXTINF:2.40000,
test<?php echo $s3; ?>.ts
<?php if ($s4 < $s3) echo "#EXT-X-DISCONTINUITY\n"; ?>
#EXTINF:2.40000,
test<?php echo $s4; ?>.ts
2
chovy On

You should be able to use the -stream_loop -1 flag before the input (-i):

ffmpeg -threads 2 -re -fflags +genpts -stream_loop -1 -i ./test.mp4 -c copy ./test.m3u8

The -fflags +genpts will regenerate the pts timestamps so it loops smoothly, otherwise the time sequence will be incorrect as it loops.