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
#![deny(
    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features,
    unused_import_braces,
    unused_qualifications,
    unused_assignments
)]
#![allow(unused_assignments, unused_variables)]

//! mpn main lib
extern crate chrono;
extern crate clap;
extern crate filetime;
extern crate mp4parse;

use self::chrono::prelude::TimeZone;
use clap::ArgMatches;
use mp4parse::read_mp4;
use mp4parse::AudioCodecSpecific;
use mp4parse::SampleEntry;
use mp4parse::TrackType;
use mp4parse::VideoCodecSpecific;
use no_color::is_no_color;
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fs;
use std::fs::File;
use std::io::{Cursor, Read};

/// MEDIAFILE Argument constant
pub const ARG_MEDIAFILE: &str = "MEDIAFILE";

/// Media struct which holds file metadata
pub struct Media {
    /// filename
    pub filename: String,
    /// file creation time
    pub creation_time: i64,
    /// file last accessed time
    pub last_accessed_time: i64,
    /// file last modified time
    pub last_modified_time: i64,
    /// file preview in bytes
    pub preview: [u8; 256],
}

impl Debug for Media {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self.preview[0])
    }
}

/// Media implementation
impl Media {
    /// constructor
    pub fn new(filename: String) -> Result<Media, Box<dyn Error>> {
        let preview: [u8; 256] = [0x0; 256];
        let metadata = fs::metadata(filename.clone()).unwrap();
        let ctime = filetime::FileTime::from_creation_time(&metadata).unwrap();
        let mtime = filetime::FileTime::from_last_modification_time(&metadata);
        let atime = filetime::FileTime::from_last_access_time(&metadata);

        Ok(Media {
            filename,
            creation_time: ctime.seconds_relative_to_1970() as i64,
            last_accessed_time: atime.seconds_relative_to_1970() as i64,
            last_modified_time: mtime.seconds_relative_to_1970() as i64,
            preview,
        })
    }
}

/// Inspect mp4 file and output box metadata.
/// # Arguments
/// * `matches` - Argument matches from the command line input
pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> {
    let mut colorize_outout = true;
    if is_no_color() {
        colorize_outout = false;
    }
    if let Some(file) = matches.get_one::<String>(ARG_MEDIAFILE).map(|s| s.as_str()) {
        println!("[media]");
        println!("uri = \"{}\"", file);
        let mut fd = File::open(file)?;
        let mut buf = Vec::new();
        let size = fd.read_to_end(&mut buf)?;
        let metadata = fs::metadata(file)?;
        println!("bytes = {}", size);
        if let Ok(time) = metadata.modified() {
            println!(
                "modified = {:?}",
                chrono::Utc
                    .timestamp_opt(
                        time.duration_since(std::time::UNIX_EPOCH)
                            .unwrap()
                            .as_secs()
                            .try_into()
                            .unwrap(),
                        0
                    )
                    .unwrap()
            );
        } else {
            println!(
                "modified = {:?}",
                "\"error: not supported on this platform.\""
            );
        }
        if let Ok(time) = metadata.created() {
            println!(
                "created = {:?}",
                chrono::Utc
                    .timestamp_opt(
                        time.duration_since(std::time::UNIX_EPOCH)
                            .unwrap()
                            .as_secs()
                            .try_into()
                            .unwrap(),
                        0
                    )
                    .unwrap()
            );
        } else {
            println!(
                "created = {:?}",
                "\"error: not supported on this platform.\""
            );
        }
        if let Ok(time) = metadata.accessed() {
            println!(
                "accessed = {:?}",
                chrono::Utc
                    .timestamp_opt(
                        time.duration_since(std::time::UNIX_EPOCH)
                            .unwrap()
                            .as_secs()
                            .try_into()
                            .unwrap(),
                        0
                    )
                    .unwrap()
            );
        } else {
            println!(
                "accessed = {:?}",
                "\"error: not supported on this platform.\""
            );
        }
        let mut c = Cursor::new(buf);
        let context = read_mp4(&mut c).expect("read_mp4 failed");
        for track in context.tracks {
            match track.track_type {
                // see https://docs.rs/mp4parse/latest/mp4parse/struct.Track.html
                TrackType::Video => {
                    println!("[media.track.video]");
                    println!("track_id = {:?}", track.track_id.unwrap());
                    println!("duration = {:?}", track.duration.unwrap());
                    println!("empty_duration = \"{:?}\"", track.empty_duration.unwrap());
                    println!("media_time = \"{:?}\"", track.media_time.unwrap()); // 1 = 64 bit creation and modification times. 0 = 64 bit creation and modification times.
                    println!("timescale = \"{:?}\"", track.timescale.unwrap());

                    let thb = track.tkhd.unwrap(); // TrackHeaderBox
                    println!("[media.track.video.header]");
                    println!("disabled = {:?}", thb.disabled);
                    println!("duration = {:?}", thb.duration);
                    println!("width = {:?}", thb.width);
                    println!("height = {:?}", thb.height);

                    let stsd = track
                        .stsd
                        .expect("TrackType::Video missing SampleDescriptionBox");
                    let v = match stsd
                        .descriptions
                        .first()
                        .expect("TrackType::Video missing SampleEntry")
                    {
                        SampleEntry::Video(v) => v,
                        _ => panic!("TrackType::Video missing VideoSampleEntry"),
                    };
                    println!("[media.track.video.sample.entry]");
                    println!("width = {:?}", v.width);
                    println!("height = {:?}", v.height);

                    let mut vcsd = HashMap::new(); // VideoCodecSpecific data
                    let codec = match v.codec_specific {
                        VideoCodecSpecific::AV1Config(ref _av1c) => "AV1",
                        VideoCodecSpecific::AVCConfig(ref avc) => {
                            // vcsd.insert(String::from("avc.bytes_length"), avc.len());
                            "AVC"
                        }
                        VideoCodecSpecific::VPxConfig(ref vpx) => {
                            vcsd.insert(String::from("vpx.bit_depth"), vpx.bit_depth);
                            vcsd.insert(String::from("vpx.colour_primaries"), vpx.colour_primaries);
                            vcsd.insert(
                                String::from("vpx.chroma_subsampling"),
                                vpx.chroma_subsampling,
                            );
                            "VPx"
                        }
                        VideoCodecSpecific::ESDSConfig(ref mp4v) => "MP4V",
                        VideoCodecSpecific::H263Config(ref _h263) => "H263",
                    };
                    println!("[media.track.video.codec]");
                    println!("codec_name = \"{}\"", codec);
                    for (key, value) in &vcsd {
                        println!("{} = {:?}", key, value);
                    }
                }
                TrackType::Audio => {
                    println!("[media.track.audio]");
                    println!("track_id = {:?}", track.track_id.unwrap());
                    println!("duration = \"{:?}\"", track.duration.unwrap());
                    println!("empty_duration = \"{:?}\"", track.empty_duration.unwrap());
                    println!("media_time = \"{:?}\"", track.media_time.unwrap());
                    println!("timescale = \"{:?}\"", track.timescale.unwrap());

                    let thb = track.tkhd.unwrap();
                    println!("[media.track.audio.header]");
                    println!("disabled = {:?}", thb.disabled);
                    println!("duration = {:?}", thb.duration);
                    println!("width = {:?}", thb.width);
                    println!("height = {:?}", thb.height);

                    let stsd = track
                        .stsd
                        .expect("TrackType::Audio missing SampleDescriptionBox");
                    let a = match stsd
                        .descriptions
                        .first()
                        .expect("TrackType::Audio missing SampleEntry")
                    {
                        SampleEntry::Audio(a) => a,
                        _ => panic!("TrackType::Audio missing AudioSampleEntry"),
                    };

                    println!("[media.track.audio.sample.entry]");
                    println!("channelcount = {:?}", a.channelcount);
                    println!("samplesize = {:?}", a.samplesize);
                    println!("samplerate = {:?}", a.samplerate);

                    let mut acsd = HashMap::new(); // AudioCodecSpecific data
                    let codec = match &a.codec_specific {
                        AudioCodecSpecific::ES_Descriptor(esds) => {
                            acsd.insert(
                                String::from("esds.audio_sample_rate"),
                                esds.audio_sample_rate.unwrap(),
                            );
                            acsd.insert(
                                String::from("esds.audio_object_type"),
                                esds.audio_object_type.unwrap() as u32,
                            );
                            "ES"
                        }
                        AudioCodecSpecific::FLACSpecificBox(flac) => {
                            acsd.insert(
                                String::from("flac.blocks[0].block_type"),
                                flac.blocks[0].block_type as u32,
                            );
                            acsd.insert(
                                String::from("flac.blocks[0].data.len()"),
                                flac.blocks[0].data.len() as u32,
                            );
                            "FLAC"
                        }
                        AudioCodecSpecific::OpusSpecificBox(opus) => {
                            acsd.insert(String::from("opus.version"), opus.version as u32);
                            "Opus"
                        }
                        AudioCodecSpecific::ALACSpecificBox(alac) => {
                            acsd.insert(String::from("alac.data.len()"), alac.data.len() as u32);
                            "ALAC"
                        }
                        AudioCodecSpecific::MP3 => "MP3",
                        AudioCodecSpecific::LPCM => "LPCM",
                    };

                    println!("[media.track.audio.codec]");
                    println!("codec_name = \"{}\"", codec);
                    for (key, value) in &acsd {
                        println!("{} = {:?}", key, value);
                    }
                }
                TrackType::Picture => {
                    println!("[media.track.picture]");
                    println!(
                        "error = {:?}",
                        "TrackType::Picture found, but not supported by this application."
                    );
                }
                TrackType::AuxiliaryVideo => {
                    println!("[media.track.auxiliaryvideo]");
                    println!(
                        "error = {:?}",
                        "TrackType::AuxiliaryVideo found, but not supported by this application."
                    );
                }
                TrackType::Metadata => {
                    println!("[media.track.metadata]");
                    println!(
                        "error = {:?}",
                        "TrackType::Metadata found, but not supported by this application."
                    );
                }
                TrackType::Unknown => {
                    println!("[media.track.unknown]");
                    println!("error = {:?}", "TrackType::Unknown.");
                }
            }
        }
    }
    println!();
    Ok(())
}

/// bit array for testing
//  pub const TESTS_SMALL: [u8; 8] = [0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70];
/// @see (https://doc.rust-lang.org/book/second-edition/ch11-03-test-organization.html)
#[cfg(test)]
mod tests {
    extern crate assert_cmd;
    extern crate tempfile;

    #[test]
    fn unit_args() {
        let filename = String::from("tests/files/test-bokeh-au-0t-vd-30f-854x480.mp4");
        let args: Vec<String> = vec![String::from("mpn"), filename.clone()];
        assert_eq!(args.len(), 2);
    }
}