How do I create a perfect MP4 to HLS encode?

name69

Active Member
1,436
2021
476
14,720

How do I create a perfect MP4 to HLS encode?​



Hi everyone,

I have a VOD MP4 file with:

  1. Scenario A: 23.976 FPS (CFR Source)
  2. Scenario B: 23.976 FPS (VFR Source)
  3. Scenario C: 24.0 FPS (CFR Source)
I want to convert it to HLS with 6-second segments, single bitrate, fully compatible with Apple/iOS devices.
 
8 comments
I mean,

  • Video freeze / black frame at segment start
  • iOS / Apple devices stutter
  • Seeking inaccurate

The black frame at segment start comes from lack of IDR per segment, for that you need to ensure that each segment starts from keyframe. For 6 second segments you go with

-hls_time 6
-hls_flags independent_segments (This enforces keyframes for each segment).
-force_key_frames "expr:gte(t,n_forced*6)" (This ensures key frames every 6 seconds independently from fps since you have each video with diff fps).
-sc_threshold 0 (This eliminated GOP floating)

Stutter at IOS usually happens if your video is having floating VFR so if you want your videos always working on IOS/Mac you should go with stable CFR eg 25/30. This will fix the seeking as well.
 
The black frame at segment start comes from lack of IDR per segment, for that you need to ensure that each segment starts from keyframe. For 6 second segments you go with

-hls_time 6
-hls_flags independent_segments (This enforces keyframes for each segment).
-force_key_frames "expr:gte(t,n_forced*6)" (This ensures key frames every 6 seconds independently from fps since you have each video with diff fps).
-sc_threshold 0 (This eliminated GOP floating)

Stutter at IOS usually happens if your video is having floating VFR so if you want your videos always working on IOS/Mac you should go with stable CFR eg 25/30. This will fix the seeking as well.

Is it true?

Dangerous Keyframe Method: -force_key_frames:v expr:'gte(t,n_forced*2.000)' is risky for 23.976fps.

  • The Math: 2.000 seconds at 23.976fps = 47.95 frames.
  • FFmpeg has to guess whether to put the keyframe at frame 47 or 48. If it drifts, your HLS segments will snap, causing playback glitches.
  • The Fix: Use strict frame counting (-g 48).
 
I have provided you example and explaination, my post is not "ready to go" commands since each video is different and settings should be adjusted per video, so kindly refer to ffmpeg docs and learn what are you trying to do/achieve, you have exact reason of your issues provided in my previous post.
 
Back
Top