4

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.

Chamath
  • 1,650
  • 2
  • 18
  • 30
Josef Zamrzla
  • 163
  • 1
  • 2
  • 10

3 Answers3

22

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.

chovy
  • 59,357
  • 43
  • 187
  • 234
6

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

Community
  • 1
  • 1
budthapa
  • 824
  • 11
  • 21
  • 4
    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 site](https://ffmpeg.org/ffmpeg-filters.html#loop) – user2843110 Nov 02 '17 at 20:42
  • There's also `movie=` which can loop, and works fine here. – Ken Sharp Feb 15 '18 at 13:31
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
Leszek Szary
  • 8,558
  • 1
  • 49
  • 46