Infinite stream from a video file (in a loop)
Asked Answered
I

3

7

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.

Ileanaileane answered 15/6, 2015 at 13:41 Comment(0)
K
5

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

Knorring answered 18/6, 2015 at 7:56 Comment(3)
you can loop video by either using a stream loop -stream_loop or a loop filter: `-filter_complex loop=repeat:size:start' for more info read the documentation on the FFMPEG siteBisexual
There's also movie= which can loop, and works fine here.Tanbark
While this answer is not incorrect (and a possible solution, especially if the last line of playlist.txt is file playlist.txt (thus providing an infinite loop), as @Bisexual has mentioned, you use -stream_loop -1 to accomplish infinite looping of all kinds of streamable content — and not -loop.Yser
S
25

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.

Substantive answered 25/7, 2016 at 4:28 Comment(2)
The stream_loop bug still happens.Stelly
Works fine on ffmpeg 3.3 Ubuntu 16.04 has 2.8 IIRC. Thanks.Adventurer
K
5

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

Knorring answered 18/6, 2015 at 7:56 Comment(3)
you can loop video by either using a stream loop -stream_loop or a loop filter: `-filter_complex loop=repeat:size:start' for more info read the documentation on the FFMPEG siteBisexual
There's also movie= which can loop, and works fine here.Tanbark
While this answer is not incorrect (and a possible solution, especially if the last line of playlist.txt is file playlist.txt (thus providing an infinite loop), as @Bisexual has mentioned, you use -stream_loop -1 to accomplish infinite looping of all kinds of streamable content — and not -loop.Yser
W
1

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
Whitford answered 18/2, 2020 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.