22from pathlib import Path
33from urllib .parse import urlparse
44
5+ from ffmpeg_media_type .exceptions import FFMpegMediaCorruptedError
6+
57from .schema import FFMpegSupport , MediaInfo
68from .utils .cache import load
79from .utils .ffprobe import ffprobe
@@ -52,7 +54,8 @@ def detect(uri: str | Path) -> MediaInfo:
5254 the media type information
5355
5456 Raises:
55- FfmpegMediaTypeError: If the ffmpeg command fails.
57+ FFmpegMediaTypeError: If the ffmpeg command fails.
58+ FFMpegMediaCorruptedError: If the media file is corrupted.
5659 """
5760 uri = str (uri )
5861 info = ffprobe (uri )
@@ -63,7 +66,8 @@ def detect(uri: str | Path) -> MediaInfo:
6366
6467 # NOTE: handle ffmpeg's image compatibility
6568 if format_name == "image2" :
66- assert info .streams [0 ].codec_name
69+ if not info .streams [0 ].codec_name :
70+ raise FFMpegMediaCorruptedError (f"Corrupted image file { uri } " )
6771 format_name = info .streams [0 ].codec_name
6872
6973 # NOTE: detect file extension
@@ -81,8 +85,11 @@ def detect(uri: str | Path) -> MediaInfo:
8185 else :
8286 suggest_ext = None
8387
84- # NOTE: we classify gif as image
88+ # NOTE: we classify gif and mjpeg as imageg
8589 if not duration or format_name in ("gif" , "mjpeg" ):
90+ if not (info .streams [0 ].width and info .streams [0 ].height ):
91+ raise FFMpegMediaCorruptedError (f"Corrupted image file { uri } " )
92+
8693 return MediaInfo (
8794 type = "image" ,
8895 width = info .streams [0 ].width or 0 ,
@@ -99,6 +106,9 @@ def detect(uri: str | Path) -> MediaInfo:
99106 width = stream .width
100107 height = stream .height
101108
109+ if not (width and height ):
110+ raise FFMpegMediaCorruptedError (f"Corrupted image file { uri } " )
111+
102112 return MediaInfo (
103113 type = "video" ,
104114 width = width or 0 ,
0 commit comments