| 1 |
import io |
| 2 |
from typing import cast |
| 3 |
|
| 4 |
import av |
| 5 |
from av.codec.context import CodecContext |
| 6 |
from av.subtitles.codeccontext import SubtitleCodecContext |
| 7 |
from av.subtitles.subtitle import AssSubtitle, BitmapSubtitle, Subtitle, SubtitleSet |
| 8 |
|
| 9 |
from .common import TestCase, fate_suite |
| 10 |
|
| 11 |
|
| 12 |
class TestSubtitle: |
| 13 |
def test_movtext(self) -> None: |
| 14 |
path = fate_suite("sub/MovText_capability_tester.mp4") |
| 15 |
|
| 16 |
subs: list[AssSubtitle] = [] |
| 17 |
with av.open(path) as container: |
| 18 |
for packet in container.demux(): |
| 19 |
subs.extend(cast(list[AssSubtitle], packet.decode())) |
| 20 |
|
| 21 |
assert len(subs) == 3 |
| 22 |
|
| 23 |
sub = subs[0] |
| 24 |
assert isinstance(sub, AssSubtitle) |
| 25 |
assert sub.type == b"ass" |
| 26 |
assert sub.text == b"" |
| 27 |
assert sub.ass == b"0,0,Default,,0,0,0,,- Test 1.\\N- Test 2." |
| 28 |
assert sub.dialogue == b"- Test 1.\n- Test 2." |
| 29 |
|
| 30 |
def test_subset(self) -> None: |
| 31 |
path = fate_suite("sub/MovText_capability_tester.mp4") |
| 32 |
|
| 33 |
with av.open(path) as container: |
| 34 |
subs = container.streams.subtitles[0] |
| 35 |
for packet in container.demux(subs): |
| 36 |
subset = subs.decode2(packet) |
| 37 |
if subset is not None: |
| 38 |
assert not isinstance(subset, Subtitle) |
| 39 |
assert isinstance(subset, SubtitleSet) |
| 40 |
assert subset.format == 1 |
| 41 |
assert hasattr(subset, "pts") |
| 42 |
assert subset.start_display_time == 0 |
| 43 |
assert hasattr(subset, "end_display_time") |
| 44 |
|
| 45 |
def test_vobsub(self) -> None: |
| 46 |
path = fate_suite("sub/vobsub.sub") |
| 47 |
|
| 48 |
subs: list[BitmapSubtitle] = [] |
| 49 |
with av.open(path) as container: |
| 50 |
for packet in container.demux(): |
| 51 |
subs.extend(cast(list[BitmapSubtitle], packet.decode())) |
| 52 |
|
| 53 |
assert len(subs) == 43 |
| 54 |
|
| 55 |
sub = subs[0] |
| 56 |
assert isinstance(sub, BitmapSubtitle) |
| 57 |
assert sub.type == b"bitmap" |
| 58 |
assert sub.x == 259 |
| 59 |
assert sub.y == 379 |
| 60 |
assert sub.width == 200 |
| 61 |
assert sub.height == 24 |
| 62 |
assert sub.nb_colors == 4 |
| 63 |
|
| 64 |
bms = sub.planes |
| 65 |
assert len(bms) == 1 |
| 66 |
assert len(memoryview(bms[0])) == 4800 # type: ignore |
| 67 |
|
| 68 |
def test_subtitle_flush(self) -> None: |
| 69 |
path = fate_suite("sub/MovText_capability_tester.mp4") |
| 70 |
|
| 71 |
subs: list[object] = [] |
| 72 |
with av.open(path) as container: |
| 73 |
stream = container.streams.subtitles[0] |
| 74 |
for packet in container.demux(stream): |
| 75 |
subs.extend(stream.decode(packet)) |
| 76 |
subs.extend(stream.decode()) |
| 77 |
|
| 78 |
assert len(subs) == 3 |
| 79 |
|
| 80 |
def test_subtitle_header_read(self) -> None: |
| 81 |
"""Test reading subtitle_header from a decoded subtitle stream.""" |
| 82 |
path = fate_suite("sub/MovText_capability_tester.mp4") |
| 83 |
|
| 84 |
with av.open(path) as container: |
| 85 |
stream = container.streams.subtitles[0] |
| 86 |
ctx = cast(SubtitleCodecContext, stream.codec_context) |
| 87 |
header = ctx.subtitle_header |
| 88 |
assert header is None or isinstance(header, bytes) |
| 89 |
|
| 90 |
def test_subtitle_header_write(self) -> None: |
| 91 |
"""Test setting subtitle_header on encoder context.""" |
| 92 |
ctx = cast(SubtitleCodecContext, CodecContext.create("mov_text", "w")) |
| 93 |
assert ctx.subtitle_header is None |
| 94 |
|
| 95 |
ass_header = b"[Script Info]\nScriptType: v4.00+\n" |
| 96 |
ctx.subtitle_header = ass_header |
| 97 |
assert ctx.subtitle_header == ass_header |
| 98 |
|
| 99 |
new_header = b"[Script Info]\nScriptType: v4.00\n" |
| 100 |
ctx.subtitle_header = new_header |
| 101 |
assert ctx.subtitle_header == new_header |
| 102 |
|
| 103 |
ctx.subtitle_header = None |
| 104 |
assert ctx.subtitle_header is None |
| 105 |
|
| 106 |
|
| 107 |
class TestSubtitleEncoding(TestCase): |
| 108 |
def test_subtitle_set_create(self) -> None: |
| 109 |
"""Test creating SubtitleSet for encoding.""" |
| 110 |
from av.subtitles.subtitle import SubtitleSet |
| 111 |
|
| 112 |
text = b"0,0,Default,,0,0,0,,Hello World" |
| 113 |
subtitle = SubtitleSet.create(text=text, start=0, end=2000, pts=0) |
| 114 |
|
| 115 |
assert subtitle.format == 1 |
| 116 |
assert subtitle.start_display_time == 0 |
| 117 |
assert subtitle.end_display_time == 2000 |
| 118 |
assert subtitle.pts == 0 |
| 119 |
assert len(subtitle) == 1 |
| 120 |
assert cast(AssSubtitle, subtitle[0]).ass == text |
| 121 |
|
| 122 |
def test_subtitle_dialogue_extended_chars(self) -> None: |
| 123 |
"""Test handling of extended UTF-8 characters in subtitle dialogue.""" |
| 124 |
from av.subtitles.subtitle import SubtitleSet |
| 125 |
|
| 126 |
text = "0,0,Default,,0,0,0,,♪ Hey, hey, hey ♪".encode("utf-8") |
| 127 |
subtitle = SubtitleSet.create(text=text, start=0, end=2000, pts=0) |
| 128 |
sub = cast(AssSubtitle, subtitle[0]) |
| 129 |
|
| 130 |
assert sub.dialogue == "♪ Hey, hey, hey ♪".encode("utf-8") |
| 131 |
|
| 132 |
def test_subtitle_encode_mp4(self) -> None: |
| 133 |
"""Test encoding subtitles to MP4 container.""" |
| 134 |
from av.subtitles.subtitle import SubtitleSet |
| 135 |
|
| 136 |
ass_header = b"""[Script Info] |
| 137 |
ScriptType: v4.00+ |
| 138 |
PlayResX: 640 |
| 139 |
PlayResY: 480 |
| 140 |
|
| 141 |
[V4+ Styles] |
| 142 |
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding |
| 143 |
Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1 |
| 144 |
|
| 145 |
[Events] |
| 146 |
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text |
| 147 |
""" |
| 148 |
|
| 149 |
output = io.BytesIO() |
| 150 |
with av.open(output, "w", format="mp4") as container: |
| 151 |
# MP4 requires video for subtitles |
| 152 |
video_stream = container.add_stream("libx264", rate=30) |
| 153 |
video_stream.width = 640 |
| 154 |
video_stream.height = 480 |
| 155 |
video_stream.pix_fmt = "yuv420p" |
| 156 |
|
| 157 |
sub_stream = container.add_stream("mov_text") |
| 158 |
sub_ctx = cast(SubtitleCodecContext, sub_stream.codec_context) |
| 159 |
sub_ctx.subtitle_header = ass_header |
| 160 |
|
| 161 |
container.start_encoding() |
| 162 |
|
| 163 |
frame = av.VideoFrame(640, 480, "yuv420p") |
| 164 |
frame.pts = 0 |
| 165 |
for packet in video_stream.encode(frame): |
| 166 |
container.mux(packet) |
| 167 |
|
| 168 |
time_base = sub_stream.time_base |
| 169 |
assert time_base is not None |
| 170 |
subtitle = SubtitleSet.create( |
| 171 |
text=b"0,0,Default,,0,0,0,,Hello World", |
| 172 |
start=0, |
| 173 |
end=int(2 / time_base), |
| 174 |
pts=0, |
| 175 |
) |
| 176 |
packet = sub_ctx.encode_subtitle(subtitle) |
| 177 |
packet.stream = sub_stream |
| 178 |
container.mux(packet) |
| 179 |
|
| 180 |
for packet in video_stream.encode(): |
| 181 |
container.mux(packet) |
| 182 |
|
| 183 |
output.seek(0) |
| 184 |
with av.open(output) as container: |
| 185 |
assert len(container.streams.subtitles) == 1 |
| 186 |
|
| 187 |
def test_subtitle_encode_mkv_srt(self) -> None: |
| 188 |
"""Test encoding SRT subtitles to MKV container.""" |
| 189 |
from av.subtitles.subtitle import SubtitleSet |
| 190 |
|
| 191 |
minimal_header = b"[Script Info]\n" |
| 192 |
|
| 193 |
output = io.BytesIO() |
| 194 |
with av.open(output, "w", format="matroska") as container: |
| 195 |
sub_stream = container.add_stream("srt") |
| 196 |
sub_ctx = cast(SubtitleCodecContext, sub_stream.codec_context) |
| 197 |
sub_ctx.subtitle_header = minimal_header |
| 198 |
|
| 199 |
container.start_encoding() |
| 200 |
|
| 201 |
time_base = sub_stream.time_base |
| 202 |
assert time_base is not None |
| 203 |
for text, start_sec, duration_sec in [ |
| 204 |
(b"0,0,Default,,0,0,0,,First subtitle", 0, 2), |
| 205 |
(b"0,0,Default,,0,0,0,,Second subtitle", 2, 2), |
| 206 |
(b"0,0,Default,,0,0,0,,Third subtitle", 4, 2), |
| 207 |
]: |
| 208 |
subtitle = SubtitleSet.create( |
| 209 |
text=text, |
| 210 |
start=0, |
| 211 |
end=int(duration_sec / time_base), |
| 212 |
pts=int(start_sec / time_base), |
| 213 |
) |
| 214 |
packet = sub_ctx.encode_subtitle(subtitle) |
| 215 |
packet.stream = sub_stream |
| 216 |
container.mux(packet) |
| 217 |
|
| 218 |
output.seek(0) |
| 219 |
with av.open(output, mode="r") as input_container: |
| 220 |
assert len(input_container.streams.subtitles) == 1 |
| 221 |
subs: list[AssSubtitle] = [] |
| 222 |
for packet in input_container.demux(): |
| 223 |
subs.extend(cast(list[AssSubtitle], packet.decode())) |
| 224 |
assert len(subs) == 3 |
| 225 |
assert b"First subtitle" in subs[0].dialogue |
| 226 |
assert b"Second subtitle" in subs[1].dialogue |
| 227 |
assert b"Third subtitle" in subs[2].dialogue |