【发布时间】:2015-08-31 00:57:55
【问题描述】:
有什么方法可以从视频文件(例如 mp4、avi、...)中创建无限 h264 流。我想使用 ffmpeg 将 avi 文件转码为 h264 但没有 loop 输出选项。
【问题讨论】:
标签: video ffmpeg video-streaming avconv
有什么方法可以从视频文件(例如 mp4、avi、...)中创建无限 h264 流。我想使用 ffmpeg 将 avi 文件转码为 h264 但没有 loop 输出选项。
【问题讨论】:
标签: video ffmpeg video-streaming avconv
不,你不能。 ffmpeg 中没有这样的命令来循环视频。您只能将-loop 用于图像。如果您有兴趣,可以使用 concat demuxer。创建一个播放列表文件,例如播放列表.txt
在 playlist.txt 中添加视频位置
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'
运行ffmpeg
ffmpeg -f concat -i playlist.txt -c copy output.mp4
见here
【讨论】:
-stream_loop 或循环过滤器循环视频:`-filter_complex loop=repeat:size:start' 更多信息请阅读FFMPEG site 上的文档跨度>
movie=可以循环播放,在这里可以正常使用。
您应该能够在输入 (-i) 之前使用-stream_loop -1 标志:
ffmpeg -threads 2 -re -fflags +genpts -stream_loop -1 -i ./test.mp4 -c copy ./test.m3u8
-fflags +genpts 将重新生成 pts 时间戳,使其顺利循环,否则循环时时间顺序将不正确。
【讨论】:
如果您想通过循环播放单个视频文件来进行直播,则可以将其拆分为 .ts 文件,然后使用 php 脚本模拟 .m3u8 文件,该脚本将根据当前时间返回不同的 .ts 文件。您可以尝试类似的方法:
<?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
【讨论】: