22
33import os
44from fractions import Fraction
5+ from typing import Iterator , TypedDict , overload
56from unittest import SkipTest
67
78import av
8- from av import AudioLayout , AudioResampler , Codec , Packet
9+ from av import (
10+ AudioCodecContext ,
11+ AudioFrame ,
12+ AudioLayout ,
13+ AudioResampler ,
14+ Codec ,
15+ Packet ,
16+ VideoCodecContext ,
17+ VideoFrame ,
18+ )
919from av .codec .codec import UnknownCodecError
1020from av .video .frame import PictureType
1121
1222from .common import TestCase , fate_suite
1323
1424
15- def iter_raw_frames (path , packet_sizes , ctx ):
25+ class Options (TypedDict , total = False ):
26+ b : str
27+ crf : str
28+ pix_fmt : str
29+ width : int
30+ height : int
31+ max_frames : int
32+ time_base : Fraction
33+ gop_size : int
34+
35+
36+ @overload
37+ def iter_raw_frames (
38+ path : str , packet_sizes : list , ctx : VideoCodecContext
39+ ) -> Iterator [VideoFrame ]: ...
40+ @overload
41+ def iter_raw_frames (
42+ path : str , packet_sizes : list , ctx : AudioCodecContext
43+ ) -> Iterator [AudioFrame ]: ...
44+ def iter_raw_frames (
45+ path : str , packet_sizes : list , ctx : VideoCodecContext | AudioCodecContext
46+ ) -> Iterator [VideoFrame | AudioFrame ]:
1647 with open (path , "rb" ) as f :
1748 for i , size in enumerate (packet_sizes ):
1849 packet = Packet (size )
@@ -244,24 +275,24 @@ def image_sequence_encode(self, codec_name: str) -> None:
244275 assert frame .height == height
245276 assert frame .format .name == pix_fmt
246277
247- def test_encoding_h264 (self ):
278+ def test_encoding_h264 (self ) -> None :
248279 self .video_encoding ("h264" , {"crf" : "19" })
249280
250- def test_encoding_mpeg4 (self ):
281+ def test_encoding_mpeg4 (self ) -> None :
251282 self .video_encoding ("mpeg4" )
252283
253- def test_encoding_xvid (self ):
284+ def test_encoding_xvid (self ) -> None :
254285 self .video_encoding ("mpeg4" , codec_tag = "xvid" )
255286
256- def test_encoding_mpeg1video (self ):
287+ def test_encoding_mpeg1video (self ) -> None :
257288 self .video_encoding ("mpeg1video" )
258289
259- def test_encoding_dvvideo (self ):
260- options = {"pix_fmt" : "yuv411p" , "width" : 720 , "height" : 480 }
290+ def test_encoding_dvvideo (self ) -> None :
291+ options : Options = {"pix_fmt" : "yuv411p" , "width" : 720 , "height" : 480 }
261292 self .video_encoding ("dvvideo" , options )
262293
263- def test_encoding_dnxhd (self ):
264- options = {
294+ def test_encoding_dnxhd (self ) -> None :
295+ options : Options = {
265296 "b" : "90M" , # bitrate
266297 "pix_fmt" : "yuv422p" ,
267298 "width" : 1920 ,
@@ -271,7 +302,12 @@ def test_encoding_dnxhd(self):
271302 }
272303 self .video_encoding ("dnxhd" , options )
273304
274- def video_encoding (self , codec_name , options = {}, codec_tag = None ):
305+ def video_encoding (
306+ self ,
307+ codec_name : str ,
308+ options : Options = {},
309+ codec_tag : str | None = None ,
310+ ) -> None :
275311 try :
276312 codec = Codec (codec_name , "w" )
277313 except UnknownCodecError :
@@ -280,21 +316,23 @@ def video_encoding(self, codec_name, options={}, codec_tag=None):
280316 container = av .open (fate_suite ("h264/interlaced_crop.mp4" ))
281317 video_stream = container .streams .video [0 ]
282318
319+ assert video_stream .time_base is not None
320+
283321 pix_fmt = options .pop ("pix_fmt" , "yuv420p" )
284322 width = options .pop ("width" , 640 )
285323 height = options .pop ("height" , 480 )
286324 max_frames = options .pop ("max_frames" , 50 )
287325 time_base = options .pop ("time_base" , video_stream .time_base )
288326 gop_size = options .pop ("gop_size" , 20 )
289327
290- ctx = codec .create ()
328+ ctx = codec .create ("video" )
291329 ctx .width = width
292330 ctx .height = height
293331 ctx .time_base = time_base
294332 ctx .framerate = 1 / ctx .time_base
295333 ctx .pix_fmt = pix_fmt
296334 ctx .gop_size = gop_size
297- ctx .options = options # TODO
335+ ctx .options = options # type: ignore
298336 if codec_tag :
299337 ctx .codec_tag = codec_tag
300338 ctx .open ()
@@ -326,7 +364,7 @@ def video_encoding(self, codec_name, options={}, codec_tag=None):
326364 if codec_name == "libx264" :
327365 dec_codec_name = "h264"
328366
329- ctx = av .Codec (dec_codec_name , "r" ).create ()
367+ ctx = av .Codec (dec_codec_name , "r" ).create ("video" )
330368 ctx .open ()
331369
332370 keyframe_indices = []
@@ -341,7 +379,7 @@ def video_encoding(self, codec_name, options={}, codec_tag=None):
341379
342380 assert frame_count == decoded_frame_count
343381
344- self . assertIsInstance (
382+ assert isinstance (
345383 all (keyframe_index for keyframe_index in keyframe_indices ), int
346384 )
347385 decoded_gop_sizes = [
@@ -350,7 +388,7 @@ def video_encoding(self, codec_name, options={}, codec_tag=None):
350388 if codec_name in ("dvvideo" , "dnxhd" ) and all (
351389 i == 1 for i in decoded_gop_sizes
352390 ):
353- raise SkipTest ()
391+ raise SkipTest ("I'm not sure why we skip this actually." )
354392 for i in decoded_gop_sizes :
355393 assert i == gop_size
356394
0 commit comments