From 3693b9bd040c98ea3476c1042cca8c653183c1f5 Mon Sep 17 00:00:00 2001
From: uvjustin <46082645+uvjustin@users.noreply.github.com>
Date: Sat, 6 Nov 2021 09:54:51 +0800
Subject: [PATCH] Adjust frag_duration setting in stream (#59135)

---
 homeassistant/components/stream/worker.py | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/homeassistant/components/stream/worker.py b/homeassistant/components/stream/worker.py
index 881614b04a3..e4be3168393 100644
--- a/homeassistant/components/stream/worker.py
+++ b/homeassistant/components/stream/worker.py
@@ -102,18 +102,18 @@ class SegmentBuffer:
                         # The LL-HLS spec allows for a fragment's duration to be within the range [0.85x,1.0x]
                         # of the part target duration. We use the frag_duration option to tell ffmpeg to try to
                         # cut the fragments when they reach frag_duration. However, the resulting fragments can
-                        # have variability in their durations and can end up being too short or too long. If
-                        # there are two tracks, as in the case of a video feed with audio, the fragment cut seems
-                        # to be done on the first track that crosses the desired threshold, and cutting on the
-                        # audio track may result in a shorter video fragment than desired. Conversely, with a
+                        # have variability in their durations and can end up being too short or too long. With a
                         # video track with no audio, the discrete nature of frames means that the frame at the
                         # end of a fragment will sometimes extend slightly beyond the desired frag_duration.
-                        # Given this, our approach is to use a frag_duration near the upper end of the range for
-                        # outputs with audio using a frag_duration at the lower end of the range for outputs with
-                        # only video.
+                        # If there are two tracks, as in the case of a video feed with audio, there is an added
+                        # wrinkle as the fragment cut seems to be done on the first track that crosses the desired
+                        # threshold, and cutting on the audio track may also result in a shorter video fragment
+                        # than desired.
+                        # Given this, our approach is to give ffmpeg a frag_duration somewhere in the middle
+                        # of the range, hoping that the parts stay pretty well bounded, and we adjust the part
+                        # durations a bit in the hls metadata so that everything "looks" ok.
                         "frag_duration": str(
-                            self._stream_settings.part_target_duration
-                            * (98e4 if add_audio else 9e5)
+                            self._stream_settings.part_target_duration * 9e5
                         ),
                     }
                     if self._stream_settings.ll_hls