ts-analyzer 0.3.0

A simple library for analyzing packets in MPEG/Transport Stream files.
Documentation
use std::collections::HashSet;
use std::fs::File;
use std::io::BufReader;
use std::process::ExitCode;

use clap::Parser;
use tracing::debug;
use tracing::info;
use ts_analyzer::reader::TsReader;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    /// Get what video to scan
    #[arg(short, long)]
    path: String,
}

fn main() -> ExitCode {
    // Parse the arguments
    let args = Args::parse();
    let video = &args.path;

    info!("Starting laser video sorter");

    // List of PIDs in the video
    let mut pids = HashSet::new();

    // Boilerplate to create a TSReader object
    let f = File::open(video).expect("Couldn't open file");
    let buf_reader = BufReader::new(f);
    let mut reader = TsReader::new(buf_reader)
        .expect("Transport Stream file contains no SYNC bytes.");

    loop {
        // Check to see if any of the KLV data indicates that the laser is on
        let packet = match reader.next_packet() {
            Ok(packet) => packet,
            Err(e) => panic!("Could not get payload due to error: {}", e),
        };

        // If `None` is returned then we have finished reading the file.
        let Some(packet) = packet else {
            debug!("Finished reading file [{}]", video);
            break;
        };

        pids.insert(packet.header().pid());
    }

    let mut pids: Vec<u16> = Vec::from_iter(pids);
    pids.sort();

    println!("PIDs in video file [{}]:\n{:#?}", video, pids);

    ExitCode::from(0)
}