FFMPEG glitches when encoding from mp4 to HLS

2

For the past two days I am trying to figure out why FFMPEG produces a glitchy video when transcoding h264 mp4 to HLS with xlib264. It happens to every video. The glitch looks like this :

720p MP4 H264 to 480p HLS

This is the original mp4 file:

Original MP4 H264 720p

EDIT: Not the whole videos are like that, only some frames and, sometimes a whole segment. This happens with every video that I try, from different sources. I am unsure what causes these. I have tested with numerous options, including inserting keyframes exactly every two seconds, playing around with crf setting, bitrate and maxrate, and even removing them completely. I always get these problems at random places in the encoded video.

Here is the current command used, which produces the same results:

/root/bin/ffmpeg -i "$f" -c:v libx264 -crf 26 -b:v 1200k -bufsize 2000k -strict -2 -vf "scale=trunc(oh*a/2)*2:480" -c:a aac -b:a 128k -profile:v main -level 4.1 -preset fast -g 48 -keyint_min 48 -pix_fmt yuv420p -flags -global_header -metadata "service_provider=XXX" -hls_time 10 -hls_playlist_type vod -hls_segment_filename /root/OUTPUT/"$URLENCODED"/480p/"$URLENCODED"_%03d.ts -threads 0 /root/OUTPUT/"$URLENCODED"/480p/playlist.m3u8

I am now a bit lost. Where do I look for errors? I am unable to use Apple's HLS analyzer tool because I do not have a new enough Apple computer. Other than that, all online tools that I have found do not report any errors.

EDIT: Issue persists across FFMPEG version N-93594-g09f8b2 and N-92724-g826655d (the latter built yesterday from ffmpeg.org)

Vii

Posted 2019-04-23T16:17:34.383

Reputation: 31

Does this happen with other encoders? – Gyan – 2019-04-23T16:51:03.797

Have not yet tested. Not sure, apart from avconv, what other options I have under Linux, but will compile those that I find and will try it out with them. – Vii – 2019-04-23T17:02:34.177

Your command works for me in a single test using version 09f8b2 and playing in mpv. (You omitted -maxrate so -bufsize is being ignored, but that's just an observation and not a suggested solution.) – llogan – 2019-04-23T17:40:20.637

Thank you for testing this out! Are you not getting even a single frame distorted like in the examples that I posted above? – Vii – 2019-04-23T18:05:52.487

When @Gyan was speaking of other encoders, he probably meant libx265 or libvpx-vp9, still using ffmpeg. This would at least rule out some source decoding issues. (Also, why crf and b:v? Choose either option, not both.) Please note that in order to respond to others, you have to add @ and their username. – slhck – 2019-04-23T18:06:34.417

Thanks @slhck, tried with both and with only crf and only b:v, then only b:v and maxrate, then this. They do make a difference in that the glitches would be in different places in the same video, but I have been unable to get a proper, glitch-free encode. The command that I have used above is simply the last thing I tried, running out of ideas and coming here to ask for help.

Have not tried with x265 but I do have it compiled. Will run a test with it tonight. – Vii – 2019-04-23T18:10:43.810

Test with other encoders, like libx265 or even the native mpeg4. – Gyan – 2019-04-23T18:15:12.550

@Gyan will do tonight, thank you! – Vii – 2019-04-23T18:15:47.300

And I just run a test with libx265. The effect is the same. – Vii – 2019-04-23T18:44:55.107

@Vii No distorted frames for me, but I don't have your same input and I don't know what you are using to play, if you are playing file locally or over network. We need more info. Log may provide clues. – llogan – 2019-04-23T18:54:55.733

Test with same input on another machine, or same machine with different RAM. – Gyan – 2019-04-23T18:55:35.557

@llogan is there any chance that you could provide me with your input file? I'd appreciate this very much. – Vii – 2019-04-23T20:33:04.940

@Gyan, on the third machine now. Two were identical, the third has different everything except for the OS (Ubuntu 16.04 everywhere). Willing to test new ideas if you have any and thanks for your help. – Vii – 2019-04-23T20:33:09.190

Thank you for your help everyone! @llogan pointed me in the right direction by mentioning he does not know if I am playing the file locally or over network. I new the network quality could not be an issue which is why I never downloaded the encodings (they happen on remote servers). As soon as I did I they played perfectly fine locally. I then started looking for the error which led me to the Nginx web servers, and specifically the sub_filter module used to apply secure tokens to the videos. It causes the glitches when applied to the m3u8 files. Thanks again! – Vii – 2019-04-24T09:00:45.577

@Vii Glad you figured it out. Since you found a solution you should make it an answer (you can answer your own questions). Might be useful to someone else with the same issues. – llogan – 2019-04-24T17:13:06.330

Answers

1

The issue is completely unrelated to FFMPEG. It was caused by the sub_filter module in the Nginx web server which was being used to apply secure tokens to the videos. I should have tested locally first. To anyone who ends up with the same problem, make sure to use the sub_filter for the .ts fragments and (if using HLS encryption) for the .key file. Do not apply the filter on the manifests.

Vii

Posted 2019-04-23T16:17:34.383

Reputation: 31