@@ -225,6 +225,7 @@ export interface VideoMetadata {
225225 colorRange ?: string ;
226226 pixelFormat ?: string ;
227227 sampleAspectRatio ?: number ;
228+ rotation ?: number ;
228229 /** Decoded frame count from the video stream's `nb_frames`. Omitted when the
229230 * container does not surface a reliable count (still images, malformed
230231 * streams, or muxes that require `-count_packets` to populate). Callers
@@ -274,6 +275,7 @@ interface FFProbeStream {
274275 color_primaries ?: string ;
275276 color_space ?: string ;
276277 tags ?: Record < string , string > ;
278+ side_data_list ?: { rotation ?: number } [ ] ;
277279 disposition ?: { attached_pic ?: number } ;
278280}
279281
@@ -855,6 +857,24 @@ export function parseFrameRate(frameRateStr: string | undefined): number {
855857 return rounded > 0 ? rounded : 0.01 ;
856858}
857859
860+ function displayRotation ( stream : FFProbeStream ) : { rotation ?: number } {
861+ const turn = Number (
862+ stream . side_data_list ?. find ( ( data ) => data . rotation !== undefined ) ?. rotation ??
863+ stream . tags ?. rotate ??
864+ 0 ,
865+ ) ;
866+ return Number . isFinite ( turn ) && turn % 360 !== 0 ? { rotation : turn } : { } ;
867+ }
868+
869+ export function displaySize (
870+ metadata : Pick < VideoMetadata , "width" | "height" | "sampleAspectRatio" | "rotation" > ,
871+ ) : { width : number ; height : number } {
872+ const width = Math . round ( metadata . width * ( metadata . sampleAspectRatio ?? 1 ) ) ;
873+ return Math . abs ( metadata . rotation ?? 0 ) % 180 === 90
874+ ? { width : metadata . height , height : width }
875+ : { width, height : metadata . height } ;
876+ }
877+
858878export function parseSampleAspectRatio ( value : string | undefined ) : number | undefined {
859879 const [ num , den ] = ( value ?? "" ) . split ( ":" ) . map ( Number ) ;
860880 if ( ! num || ! den || ! Number . isFinite ( num ) || ! Number . isFinite ( den ) ) return undefined ;
@@ -1052,6 +1072,7 @@ export async function extractMediaMetadata(filePath: string): Promise<VideoMetad
10521072 colorRange : videoStream . color_range ,
10531073 pixelFormat,
10541074 ...( sampleAspectRatio === undefined ? { } : { sampleAspectRatio } ) ,
1075+ ...displayRotation ( videoStream ) ,
10551076 frames,
10561077 } ;
10571078 } ) ( ) ;
0 commit comments