aboutsummaryrefslogtreecommitdiff
path: root/tools/src/bin/gen_meta.rs
blob: 4abbb8bf59aa8601e1c29080ed72fc243df6ef25 (plain)
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
use anyhow::{anyhow, bail};
use clap::Parser;
use jellycommon::{ItemInfo, Source, SourceTrack, SourceTrackKind};
use jellyebml::{
    matroska::MatroskaTag,
    read::EbmlReader,
    unflatten::{Unflat, Unflatten},
    Master,
};
use log::error;
use std::{collections::BTreeMap, fs::File, io::Write, path::PathBuf};

#[derive(Parser)]
struct Args {
    #[clap(short = 'I', long)]
    identifier: String,
    #[clap(short = 'O', long)]
    write_json: bool,
    #[clap(short, long)]
    title: String,
    #[clap(short = 'i', long)]
    input: PathBuf,
}

fn main() -> anyhow::Result<()> {
    env_logger::init_from_env("LOG");
    let args = Args::parse();

    let mut tracks = BTreeMap::new();
    let input = File::open(args.input.clone()).unwrap();
    let mut input = EbmlReader::new(input);

    // TODO dont traverse the entire file, if the tracks are listed at the end
    while let Some(item) = input.next() {
        let item = item?;
        match item {
            MatroskaTag::Tracks(_) => {
                let mut iter = Unflatten::new(&mut input);
                while let Some(Ok(Unflat { children, item })) = iter.next() {
                    if !matches!(item, MatroskaTag::TrackEntry(_)) {
                        panic!("no")
                    }
                    let mut children = children.unwrap();
                    let (
                        mut index,
                        mut language,
                        mut codec,
                        mut kind,
                        mut sample_rate,
                        mut channels,
                        mut width,
                        mut height,
                        mut name,
                    ) = (None, None, None, None, None, None, None, None, None);
                    while let Some(Ok(Unflat { children, item })) = children.next() {
                        match item {
                            MatroskaTag::CodecID(b) => codec = Some(b),
                            MatroskaTag::Language(v) => language = Some(v),
                            MatroskaTag::TrackNumber(v) => index = Some(v),
                            MatroskaTag::TrackType(v) => kind = Some(v),
                            MatroskaTag::Name(v) => name = Some(v),
                            MatroskaTag::Audio(_) => {
                                let mut children = children.unwrap();
                                while let Some(Ok(Unflat { item, .. })) = children.next() {
                                    match item {
                                        MatroskaTag::Channels(v) => channels = Some(v as usize),
                                        MatroskaTag::SamplingFrequency(v) => sample_rate = Some(v),
                                        _ => (),
                                    }
                                }
                            }
                            MatroskaTag::Video(_) => {
                                let mut children = children.unwrap();
                                while let Some(Ok(Unflat { item, .. })) = children.next() {
                                    match item {
                                        MatroskaTag::PixelWidth(v) => width = Some(v),
                                        MatroskaTag::PixelHeight(v) => height = Some(v),
                                        _ => (),
                                    }
                                }
                            }
                            _ => (),
                        }
                    }
                    tracks.insert(
                        index.unwrap(),
                        SourceTrack {
                            name: name.unwrap_or_else(|| "unnamed".to_string()),
                            codec: codec.unwrap(),
                            language: language.unwrap_or_else(|| "none".to_string()),
                            kind: match kind.ok_or(anyhow!("track type required"))? {
                                1 => SourceTrackKind::Video {
                                    fps: 0.0, // TODO
                                    width: width.unwrap(),
                                    height: height.unwrap(),
                                },
                                2 => SourceTrackKind::Audio {
                                    bit_depth: 0, // TODO
                                    channels: channels.unwrap(),
                                    sample_rate: sample_rate.unwrap(),
                                },
                                17 => SourceTrackKind::Subtitles,
                                _ => bail!("invalid track type"),
                            },
                        },
                    );
                }
                error!("break!");
                drop(iter);
                error!("break done!");
                break;
            }
            _ => (),
        }
    }

    let k = serde_json::to_string_pretty(&ItemInfo {
        title: args.title,
        source: Source {
            path: args.input.clone(),
            tracks,
        },
    })?;

    if args.write_json {
        let mut f = File::create(format!("{}.json", args.identifier))?;
        f.write_all(k.as_bytes())?;
    } else {
        println!("{k}")
    }

    Ok(())
}