rust_ffprobe 1.0.0

Safe and idiomatic Rust wrapper for FFprobe
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
use ffmpeg_common::Duration;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

/// Sections that can be probed
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProbeSection {
    Format,
    Streams,
    Packets,
    Frames,
    Programs,
    Chapters,
    Error,
}

impl ProbeSection {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Format => "format",
            Self::Streams => "streams",
            Self::Packets => "packets",
            Self::Frames => "frames",
            Self::Programs => "programs",
            Self::Chapters => "chapters",
            Self::Error => "error",
        }
    }
}

/// Read interval specification
#[derive(Debug, Clone)]
pub struct ReadInterval {
    /// Start position (None = from beginning)
    pub start: Option<IntervalPosition>,
    /// End position (None = to end)
    pub end: Option<IntervalPosition>,
}

#[derive(Debug, Clone)]
pub enum IntervalPosition {
    /// Absolute position
    Absolute(Duration),
    /// Relative offset from current position
    Relative(Duration),
    /// Number of packets
    Packets(u64),
}

impl ReadInterval {
    /// Create interval from start to end
    pub fn new(start: Option<IntervalPosition>, end: Option<IntervalPosition>) -> Self {
        Self { start, end }
    }

    /// Read from beginning to position
    pub fn to(end: IntervalPosition) -> Self {
        Self {
            start: None,
            end: Some(end),
        }
    }

    /// Read from position to end
    pub fn from(start: IntervalPosition) -> Self {
        Self {
            start: Some(start),
            end: None,
        }
    }

    /// Read entire input
    pub fn all() -> Self {
        Self {
            start: None,
            end: None,
        }
    }

    /// Convert to FFprobe format
    pub fn to_string(&self) -> String {
        let mut result = String::new();

        if let Some(ref start) = self.start {
            match start {
                IntervalPosition::Absolute(d) => result.push_str(&d.to_ffmpeg_format()),
                IntervalPosition::Relative(d) => result.push_str(&format!("+{}", d.to_ffmpeg_format())),
                IntervalPosition::Packets(n) => result.push_str(&format!("#{}", n)),
            }
        }

        result.push('%');

        if let Some(ref end) = self.end {
            match end {
                IntervalPosition::Absolute(d) => result.push_str(&d.to_ffmpeg_format()),
                IntervalPosition::Relative(d) => result.push_str(&format!("+{}", d.to_ffmpeg_format())),
                IntervalPosition::Packets(n) => result.push_str(&format!("#{}", n)),
            }
        }

        result
    }
}

impl fmt::Display for ReadInterval {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

/// Main probe result structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProbeResult {
    /// Format information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<FormatInfo>,

    /// Stream information
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub streams: Vec<StreamInfo>,

    /// Packet information
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub packets: Vec<PacketInfo>,

    /// Frame information
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub frames: Vec<FrameInfo>,

    /// Program information
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub programs: Vec<ProgramInfo>,

    /// Chapter information
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub chapters: Vec<ChapterInfo>,

    /// Error information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<ErrorInfo>,
}

impl ProbeResult {
    /// Get video streams
    pub fn video_streams(&self) -> Vec<&StreamInfo> {
        self.streams
            .iter()
            .filter(|s| s.codec_type == Some("video".to_string()))
            .collect()
    }

    /// Get audio streams
    pub fn audio_streams(&self) -> Vec<&StreamInfo> {
        self.streams
            .iter()
            .filter(|s| s.codec_type == Some("audio".to_string()))
            .collect()
    }

    /// Get subtitle streams
    pub fn subtitle_streams(&self) -> Vec<&StreamInfo> {
        self.streams
            .iter()
            .filter(|s| s.codec_type == Some("subtitle".to_string()))
            .collect()
    }

    /// Get the primary video stream
    pub fn primary_video_stream(&self) -> Option<&StreamInfo> {
        self.video_streams().into_iter().next()
    }

    /// Get the primary audio stream
    pub fn primary_audio_stream(&self) -> Option<&StreamInfo> {
        self.audio_streams().into_iter().next()
    }

    /// Get total duration
    pub fn duration(&self) -> Option<f64> {
        self.format.as_ref()?.duration.as_ref()?.parse().ok()
    }

    /// Get format name
    pub fn format_name(&self) -> Option<&str> {
        self.format.as_ref()?.format_name.as_deref()
    }

    /// Get format long name
    pub fn format_long_name(&self) -> Option<&str> {
        self.format.as_ref()?.format_long_name.as_deref()
    }
}

/// Format information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormatInfo {
    /// Filename
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename: Option<String>,

    /// Number of streams
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nb_streams: Option<u32>,

    /// Number of programs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nb_programs: Option<u32>,

    /// Format name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format_name: Option<String>,

    /// Format long name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format_long_name: Option<String>,

    /// Start time in seconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_time: Option<String>,

    /// Duration in seconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration: Option<String>,

    /// File size in bytes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<String>,

    /// Bit rate
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bit_rate: Option<String>,

    /// Probe score
    #[serde(skip_serializing_if = "Option::is_none")]
    pub probe_score: Option<u32>,

    /// Tags/metadata
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub tags: HashMap<String, String>,
}

/// Stream information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamInfo {
    /// Stream index
    pub index: u32,

    /// Codec name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub codec_name: Option<String>,

    /// Codec long name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub codec_long_name: Option<String>,

    /// Profile
    #[serde(skip_serializing_if = "Option::is_none")]
    pub profile: Option<String>,

    /// Codec type (video/audio/subtitle/data)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub codec_type: Option<String>,

    /// Codec tag string
    #[serde(skip_serializing_if = "Option::is_none")]
    pub codec_tag_string: Option<String>,

    /// Codec tag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub codec_tag: Option<String>,

    // Video specific
    /// Width
    #[serde(skip_serializing_if = "Option::is_none")]
    pub width: Option<u32>,

    /// Height
    #[serde(skip_serializing_if = "Option::is_none")]
    pub height: Option<u32>,

    /// Coded width
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coded_width: Option<u32>,

    /// Coded height
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coded_height: Option<u32>,

    /// Has B frames
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_b_frames: Option<u32>,

    /// Sample aspect ratio
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sample_aspect_ratio: Option<String>,

    /// Display aspect ratio
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_aspect_ratio: Option<String>,

    /// Pixel format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pix_fmt: Option<String>,

    /// Level
    #[serde(skip_serializing_if = "Option::is_none")]
    pub level: Option<i32>,

    /// Color range
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color_range: Option<String>,

    /// Color space
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color_space: Option<String>,

    /// Color transfer
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color_transfer: Option<String>,

    /// Color primaries
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color_primaries: Option<String>,

    /// Chroma location
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chroma_location: Option<String>,

    /// Field order
    #[serde(skip_serializing_if = "Option::is_none")]
    pub field_order: Option<String>,

    /// References
    #[serde(skip_serializing_if = "Option::is_none")]
    pub refs: Option<u32>,

    /// Is AVC
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_avc: Option<String>,

    /// NAL length size
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nal_length_size: Option<String>,

    // Audio specific
    /// Sample format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sample_fmt: Option<String>,

    /// Sample rate
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sample_rate: Option<String>,

    /// Number of channels
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channels: Option<u32>,

    /// Channel layout
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channel_layout: Option<String>,

    /// Bits per sample
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bits_per_sample: Option<u32>,

    // Common
    /// Stream ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// Frame rate ratio
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r_frame_rate: Option<String>,

    /// Average frame rate
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avg_frame_rate: Option<String>,

    /// Time base
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_base: Option<String>,

    /// Start PTS
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_pts: Option<i64>,

    /// Start time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_time: Option<String>,

    /// Duration timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration_ts: Option<i64>,

    /// Duration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration: Option<String>,

    /// Bit rate
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bit_rate: Option<String>,

    /// Max bit rate
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_bit_rate: Option<String>,

    /// Bits per raw sample
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bits_per_raw_sample: Option<u32>,

    /// Number of frames
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nb_frames: Option<String>,

    /// Number of read frames
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nb_read_frames: Option<String>,

    /// Number of read packets
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nb_read_packets: Option<String>,

    /// Disposition flags
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disposition: Option<HashMap<String, u8>>,

    /// Tags/metadata
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub tags: HashMap<String, String>,
}

impl StreamInfo {
    /// Check if this is a video stream
    pub fn is_video(&self) -> bool {
        self.codec_type.as_deref() == Some("video")
    }

    /// Check if this is an audio stream
    pub fn is_audio(&self) -> bool {
        self.codec_type.as_deref() == Some("audio")
    }

    /// Check if this is a subtitle stream
    pub fn is_subtitle(&self) -> bool {
        self.codec_type.as_deref() == Some("subtitle")
    }

    /// Get the language tag
    pub fn language(&self) -> Option<&str> {
        self.tags.get("language").map(|s| s.as_str())
    }

    /// Get the title tag
    pub fn title(&self) -> Option<&str> {
        self.tags.get("title").map(|s| s.as_str())
    }

    /// Get resolution as (width, height)
    pub fn resolution(&self) -> Option<(u32, u32)> {
        match (self.width, self.height) {
            (Some(w), Some(h)) => Some((w, h)),
            _ => None,
        }
    }

    /// Get frame rate as f64
    pub fn frame_rate(&self) -> Option<f64> {
        self.avg_frame_rate
            .as_ref()
            .or(self.r_frame_rate.as_ref())
            .and_then(|r| parse_rational(r))
    }

    /// Get sample rate as u32
    pub fn sample_rate_hz(&self) -> Option<u32> {
        self.sample_rate.as_ref()?.parse().ok()
    }

    /// Get duration as f64 seconds
    pub fn duration_seconds(&self) -> Option<f64> {
        self.duration.as_ref()?.parse().ok()
    }

    /// Get bit rate as u64
    pub fn bit_rate_bps(&self) -> Option<u64> {
        self.bit_rate.as_ref()?.parse().ok()
    }
}

/// Packet information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PacketInfo {
    /// Codec type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub codec_type: Option<String>,

    /// Stream index
    pub stream_index: u32,

    /// Presentation timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pts: Option<i64>,

    /// Presentation time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pts_time: Option<String>,

    /// Decoding timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dts: Option<i64>,

    /// Decoding time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dts_time: Option<String>,

    /// Duration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration: Option<i64>,

    /// Duration time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration_time: Option<String>,

    /// Size in bytes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<String>,

    /// Position
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pos: Option<String>,

    /// Flags
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flags: Option<String>,

    /// Data (if show_data enabled)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<String>,

    /// Data hash (if show_data_hash enabled)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_hash: Option<String>,
}

/// Frame information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FrameInfo {
    /// Media type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media_type: Option<String>,

    /// Stream index
    pub stream_index: u32,

    /// Key frame flag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_frame: Option<u8>,

    /// Presentation timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pts: Option<i64>,

    /// Presentation time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pts_time: Option<String>,

    /// Packet timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_pts: Option<i64>,

    /// Packet time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_pts_time: Option<String>,

    /// Packet DTS
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_dts: Option<i64>,

    /// Packet DTS time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_dts_time: Option<String>,

    /// Best effort timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub best_effort_timestamp: Option<i64>,

    /// Best effort time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub best_effort_timestamp_time: Option<String>,

    /// Packet duration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_duration: Option<i64>,

    /// Packet duration time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_duration_time: Option<String>,

    /// Packet position
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_pos: Option<String>,

    /// Packet size
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_size: Option<String>,

    // Video specific
    /// Width
    #[serde(skip_serializing_if = "Option::is_none")]
    pub width: Option<u32>,

    /// Height
    #[serde(skip_serializing_if = "Option::is_none")]
    pub height: Option<u32>,

    /// Pixel format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pix_fmt: Option<String>,

    /// Picture type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pict_type: Option<String>,

    /// Coded picture number
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coded_picture_number: Option<u32>,

    /// Display picture number
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_picture_number: Option<u32>,

    /// Interlaced frame
    #[serde(skip_serializing_if = "Option::is_none")]
    pub interlaced_frame: Option<u8>,

    /// Top field first
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_field_first: Option<u8>,

    /// Repeat picture
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repeat_pict: Option<u8>,

    // Audio specific
    /// Sample format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sample_fmt: Option<String>,

    /// Number of samples
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nb_samples: Option<u32>,

    /// Channels
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channels: Option<u32>,

    /// Channel layout
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channel_layout: Option<String>,
}

/// Program information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProgramInfo {
    /// Program ID
    pub program_id: u32,

    /// Program number
    #[serde(skip_serializing_if = "Option::is_none")]
    pub program_num: Option<u32>,

    /// Number of streams
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nb_streams: Option<u32>,

    /// PMT PID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pmt_pid: Option<u32>,

    /// PCR PID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pcr_pid: Option<u32>,

    /// Start PTS
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_pts: Option<i64>,

    /// Start time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_time: Option<String>,

    /// End PTS
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_pts: Option<i64>,

    /// End time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_time: Option<String>,

    /// Tags
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub tags: HashMap<String, String>,

    /// Streams in this program
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub streams: Vec<StreamInfo>,
}

/// Chapter information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChapterInfo {
    /// Chapter ID
    pub id: i64,

    /// Time base
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_base: Option<String>,

    /// Start time
    pub start: i64,

    /// Start time string
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_time: Option<String>,

    /// End time
    pub end: i64,

    /// End time string
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_time: Option<String>,

    /// Tags
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub tags: HashMap<String, String>,
}

impl ChapterInfo {
    /// Get chapter title
    pub fn title(&self) -> Option<&str> {
        self.tags.get("title").map(|s| s.as_str())
    }
}

/// Error information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorInfo {
    /// Error code
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<i32>,

    /// Error string
    #[serde(skip_serializing_if = "Option::is_none")]
    pub string: Option<String>,
}

/// Parse a rational number string (e.g., "30/1" or "30000/1001")
fn parse_rational(s: &str) -> Option<f64> {
    let parts: Vec<&str> = s.split('/').collect();
    if parts.len() == 2 {
        let num: f64 = parts[0].parse().ok()?;
        let den: f64 = parts[1].parse().ok()?;
        if den != 0.0 {
            Some(num / den)
        } else {
            None
        }
    } else {
        s.parse().ok()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_read_interval() {
        let interval = ReadInterval::all();
        assert_eq!(interval.to_string(), "%");

        let interval = ReadInterval::to(IntervalPosition::Absolute(Duration::from_secs(30)));
        assert_eq!(interval.to_string(), "%00:00:30");

        let interval = ReadInterval::from(IntervalPosition::Relative(Duration::from_secs(10)));
        assert_eq!(interval.to_string(), "+00:00:10%");

        let interval = ReadInterval::new(
            Some(IntervalPosition::Absolute(Duration::from_secs(10))),
            Some(IntervalPosition::Packets(100)),
        );
        assert_eq!(interval.to_string(), "00:00:10%#100");
    }

    #[test]
    fn test_stream_info_helpers() {
        let mut stream = StreamInfo {
            index: 0,
            codec_type: Some("video".to_string()),
            width: Some(1920),
            height: Some(1080),
            avg_frame_rate: Some("30/1".to_string()),
            bit_rate: Some("5000000".to_string()),
            tags: HashMap::new(),
            ..Default::default()
        };

        assert!(stream.is_video());
        assert!(!stream.is_audio());
        assert_eq!(stream.resolution(), Some((1920, 1080)));
        assert_eq!(stream.frame_rate(), Some(30.0));
        assert_eq!(stream.bit_rate_bps(), Some(5000000));

        stream.tags.insert("language".to_string(), "eng".to_string());
        assert_eq!(stream.language(), Some("eng"));
    }

    #[test]
    fn test_parse_rational() {
        assert_eq!(parse_rational("30/1"), Some(30.0));
        assert_eq!(parse_rational("30000/1001"), Some(29.97002997002997));
        assert_eq!(parse_rational("25"), Some(25.0));
        assert_eq!(parse_rational("0/1"), Some(0.0));
        assert_eq!(parse_rational("1/0"), None);
    }
}

impl Default for StreamInfo {
    fn default() -> Self {
        Self {
            index: 0,
            codec_name: None,
            codec_long_name: None,
            profile: None,
            codec_type: None,
            codec_tag_string: None,
            codec_tag: None,
            width: None,
            height: None,
            coded_width: None,
            coded_height: None,
            has_b_frames: None,
            sample_aspect_ratio: None,
            display_aspect_ratio: None,
            pix_fmt: None,
            level: None,
            color_range: None,
            color_space: None,
            color_transfer: None,
            color_primaries: None,
            chroma_location: None,
            field_order: None,
            refs: None,
            is_avc: None,
            nal_length_size: None,
            sample_fmt: None,
            sample_rate: None,
            channels: None,
            channel_layout: None,
            bits_per_sample: None,
            id: None,
            r_frame_rate: None,
            avg_frame_rate: None,
            time_base: None,
            start_pts: None,
            start_time: None,
            duration_ts: None,
            duration: None,
            bit_rate: None,
            max_bit_rate: None,
            bits_per_raw_sample: None,
            nb_frames: None,
            nb_read_frames: None,
            nb_read_packets: None,
            disposition: None,
            tags: HashMap::new(),
        }
    }
}