Skip to content

Commit ef21abb

Browse files
Merge pull request #138 from mlubbad/fix/hls-instance-state-and-dotted-paths
fix(hls): isolate HLS instance state, preserve dotted output names, and handle copied audio bitrate
2 parents f5eafae + 707f960 commit ef21abb

4 files changed

Lines changed: 118 additions & 10 deletions

File tree

‎ffmpeg_streaming/_hls_helper.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def rotate_key(self, line: str):
7373
"""
7474
if self.needle in line and line not in self.segments:
7575
self.segments.append(line)
76-
if len(self.segments) % self.period == 0:
76+
if self.period > 0 and len(self.segments) % self.period == 0:
7777
self.update_suffix()
7878
self.generate()
7979

@@ -154,7 +154,8 @@ def stream_path(self, rep):
154154
"""
155155
@TODO: add documentation
156156
"""
157-
return ["{}_{}p.m3u8".format(os.path.basename(self.media.output_).split('.')[0], rep.size.height)]
157+
_, name = get_path_info(self.media.output_)
158+
return ["{}_{}p.m3u8".format(name, rep.size.height)]
158159

159160
def sub_path(self, rep):
160161
"""
@@ -164,4 +165,5 @@ def sub_path(self, rep):
164165
----------
165166
rep : Representation
166167
"""
167-
return ["{}_{}p_vtt.m3u8".format(os.path.basename(self.media.output_).split('.')[0], rep.size.height)]
168+
_, name = get_path_info(self.media.output_)
169+
return ["{}_{}p_vtt.m3u8".format(name, rep.size.height)]

‎ffmpeg_streaming/_media.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,26 @@ class HLS(Streaming):
177177
PERIODIC_RE_KEY_FLAG = 'periodic_rekey'
178178
MASTER_PLAYLIST_IS_SAVED = False
179179

180+
def __init__(self, media, _format: Format, **options):
181+
self.KEY_INFO_FILE_PATH = None
182+
self.MASTER_PLAYLIST_IS_SAVED = False
183+
super(HLS, self).__init__(media, _format, **options)
184+
180185
def set_up(self):
181186
"""
182187
@TODO: add documentation
183188
"""
184-
if not HLS.MASTER_PLAYLIST_IS_SAVED:
189+
if not self.MASTER_PLAYLIST_IS_SAVED:
185190
self.save_master_playlist()
186191

187192
def encryption(self, path: str, url: str, key_rotation_period: int = 0, needle: str = ".ts' for writing", length: int = 16):
188193
"""
189194
@TODO: add documentation
190195
"""
191196
with tempfile.NamedTemporaryFile(mode='w', suffix='_py_ff_vi_st.tmp', delete=False) as key_info:
192-
HLS.KEY_INFO_FILE_PATH = key_info.name
197+
self.KEY_INFO_FILE_PATH = key_info.name
193198

194-
key_info_file = HLSKeyInfoFile(HLS.KEY_INFO_FILE_PATH, path, url, key_rotation_period, needle, length)
199+
key_info_file = HLSKeyInfoFile(self.KEY_INFO_FILE_PATH, path, url, key_rotation_period, needle, length)
195200
self.options.update({'hls_key_info_file': str(key_info_file)})
196201

197202
if key_rotation_period > 0:
@@ -209,7 +214,7 @@ def save_master_playlist(self, path=None):
209214
@TODO: add documentation
210215
"""
211216
if path is not None:
212-
HLS.MASTER_PLAYLIST_IS_SAVED = True
217+
self.MASTER_PLAYLIST_IS_SAVED = True
213218

214219
HLSMasterPlaylist.generate(self, path)
215220

@@ -230,8 +235,8 @@ def finish_up(self):
230235
"""
231236
@TODO: add documentation
232237
"""
233-
if HLS.KEY_INFO_FILE_PATH is not None:
234-
rm(HLS.KEY_INFO_FILE_PATH)
238+
if self.KEY_INFO_FILE_PATH is not None:
239+
rm(self.KEY_INFO_FILE_PATH)
235240

236241
super(HLS, self).finish_up()
237242

‎ffmpeg_streaming/_media_property.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def calc_overall(self):
7878
"""
7979
@TODO: add documentation
8080
"""
81-
return self.overall_ if self.overall_ is not None else self.video_ + self.audio_
81+
return self.overall_ if self.overall_ is not None else (self.video_ or 0) + (self.audio_ or 0)
8282

8383

8484
def multiple_up(value, multiple):
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os
2+
import shutil
3+
import tempfile
4+
import unittest
5+
6+
import ffmpeg_streaming
7+
from ffmpeg_streaming import Bitrate, Formats, Representation, Size
8+
from ffmpeg_streaming._command_builder import _hls
9+
from ffmpeg_streaming._hls_helper import HLSKeyInfoFile, HLSMasterPlaylist
10+
11+
12+
class TestHLSRegressions(unittest.TestCase):
13+
def setUp(self):
14+
self.tmp_dir = tempfile.mkdtemp(prefix="hls_reg_")
15+
self.src_video = os.path.join(
16+
os.path.dirname(os.path.abspath(__file__)), "files", "test.mp4"
17+
)
18+
19+
def tearDown(self):
20+
shutil.rmtree(self.tmp_dir, ignore_errors=True)
21+
22+
def test_master_playlist_matches_command_builder_for_dotted_filenames(self):
23+
video = ffmpeg_streaming.input(self.src_video)
24+
hls_obj = video.hls(Formats.h264())
25+
hls_obj.representations(
26+
Representation(Size(1280, 720), Bitrate(2048 * 1024)),
27+
Representation(Size(854, 480), Bitrate(1024 * 1024)),
28+
)
29+
dotted_output = os.path.join(self.tmp_dir, "episode.01.v2.m3u8")
30+
hls_obj.output(dotted_output, run_command=False)
31+
32+
with open(dotted_output, encoding="utf-8") as fp:
33+
manifest = fp.read()
34+
35+
cmd_args = _hls(hls_obj)
36+
self.assertIn(os.path.join(self.tmp_dir, "episode.01.v2_720p.m3u8"), cmd_args)
37+
self.assertIn("episode.01.v2_720p.m3u8", manifest)
38+
self.assertIn("episode.01.v2_480p.m3u8", manifest)
39+
self.assertEqual(
40+
HLSMasterPlaylist(hls_obj).sub_path(hls_obj.reps[0]),
41+
["episode.01.v2_720p_vtt.m3u8"],
42+
)
43+
44+
def test_hls_instances_isolate_master_playlist_and_key_info_state(self):
45+
video = ffmpeg_streaming.input(self.src_video)
46+
47+
hls_first = video.hls(Formats.h264())
48+
hls_first.representations(Representation(Size(640, 360), Bitrate(512 * 1024)))
49+
custom_master = os.path.join(self.tmp_dir, "custom_first.m3u8")
50+
hls_first.save_master_playlist(custom_master)
51+
hls_first.encryption(
52+
os.path.join(self.tmp_dir, "first.key"),
53+
"https://cdn.example.com/first.key",
54+
)
55+
56+
hls_second = video.hls(Formats.h264())
57+
hls_second.representations(Representation(Size(640, 360), Bitrate(512 * 1024)))
58+
second_master = os.path.join(self.tmp_dir, "second_auto.m3u8")
59+
hls_second.output(second_master, run_command=False)
60+
61+
self.assertTrue(os.path.isfile(custom_master))
62+
self.assertTrue(os.path.isfile(second_master))
63+
64+
hls_second.encryption(
65+
os.path.join(self.tmp_dir, "second.key"),
66+
"https://cdn.example.com/second.key",
67+
)
68+
self.assertNotEqual(
69+
hls_first.KEY_INFO_FILE_PATH, hls_second.KEY_INFO_FILE_PATH
70+
)
71+
72+
hls_first.finish_up()
73+
self.assertFalse(os.path.exists(hls_first.KEY_INFO_FILE_PATH))
74+
self.assertTrue(os.path.exists(hls_second.KEY_INFO_FILE_PATH))
75+
hls_second.finish_up()
76+
77+
def test_rotate_key_ignores_non_positive_period(self):
78+
key_path = os.path.join(self.tmp_dir, "enc.key")
79+
info_path = os.path.join(self.tmp_dir, "enc.keyinfo")
80+
key_info = HLSKeyInfoFile(
81+
info_path,
82+
key_path,
83+
"https://cdn.example.com/enc.key",
84+
period=0,
85+
needle=".ts' for writing",
86+
)
87+
key_info.generate()
88+
initial_url = key_info.url
89+
key_info.rotate_key("Opening 'seg_0001.ts' for writing")
90+
self.assertEqual(key_info.url, initial_url)
91+
92+
def test_bitrate_calc_overall_when_audio_is_copied(self):
93+
b_video_only = Bitrate(102400)
94+
self.assertEqual(b_video_only.audio, "copy")
95+
self.assertEqual(b_video_only.calc_overall, 102400)
96+
b_both = Bitrate(102400, 65536)
97+
self.assertEqual(b_both.calc_overall, 167936)
98+
99+
100+
if __name__ == "__main__":
101+
unittest.main()

0 commit comments

Comments
 (0)