给定一个输入视频,input.mp4
以下命令将生成一个out
文件夹,其中包含一堆 HLS 视频片段和m3u8
多种分辨率的播放列表文件:
ffmpeg -hide_banner -y -i input.mp4 \
-vf scale=w=640:h=360:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -hls_segment_filename out/360p_%03d.ts out/360p.m3u8 \
-vf scale=w=842:h=480:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 1400k -maxrate 1498k -bufsize 2100k -b:a 128k -hls_segment_filename out/480p_%03d.ts out/480p.m3u8 \
-vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 2800k -maxrate 2996k -bufsize 4200k -b:a 128k -hls_segment_filename out/720p_%03d.ts out/720p.m3u8 \
-vf scale=w=1920:h=1080:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 5000k -maxrate 5350k -bufsize 7500k -b:a 192k -hls_segment_filename out/1080p_%03d.ts out/1080p.m3u8
运行该命令时,输出如下所示:
[hls @ 0x12be06030] Opening 'out/360p_000.ts' for writing
[hls @ 0x12be088d0] Opening 'out/480p_000.ts' for writing
[hls @ 0x12be0b810] Opening 'out/720p_000.ts' for writing
[hls @ 0x12be0e790] Opening 'out/1080p_000.ts' for writing
[hls @ 0x12be06030] Opening 'out/360p_001.ts' for writing
[hls @ 0x12be088d0] Opening 'out/480p_001.ts' for writing
[hls @ 0x12be0b810] Opening 'out/720p_001.ts' for writing
[hls @ 0x12be0e790] Opening 'out/1080p_001.ts' for writing
[hls @ 0x12be06030] Opening 'out/360p_002.ts' for writing
[hls @ 0x12be088d0] Opening 'out/480p_002.ts' for writing
[hls @ 0x12be0b810] Opening 'out/720p_002.ts' for writing
[hls @ 0x12be0e790] Opening 'out/1080p_002.ts' for writing
[hls @ 0x12be06030] Opening 'out/360p_003.ts' for writing
[hls @ 0x12be088d0] Opening 'out/480p_003.ts' for writing
[hls @ 0x12be0b810] Opening 'out/720p_003.ts' for writing
[hls @ 0x12be0e790] Opening 'out/1080p_003.ts' for writing
etc
如您所见,ffmpeg 的输出告诉我它正在打开哪些文件进行写入。ffmpeg 是否有任何方法可以输出文件何时关闭以进行写入,或者以其他方式知道何时可以安全地以编程方式与这些文件交互?我专门在 Unix 系统上运行它,因此如果答案涉及一些 unix 技巧来监视正在运行的 ffmpeg 进程,那很好,但我更希望 ffmpeg 直接告诉我何时完成文件写入。
答案1
添加-hls_flags temp_file
到所有输出。FFmpeg 会将数据写入临时文件并在完成后重命名。因此,如果段文件存在,则完成。