|
| 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