add verbose option
authorKostya Shishkov <kostya.shishkov@gmail.com>
Wed, 17 Nov 2021 17:40:51 +0000 (18:40 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Wed, 17 Nov 2021 17:40:51 +0000 (18:40 +0100)
src/demux.rs
src/main.rs

index dbc1447a0eddb4e76ca83e005a2f90fca7618ccf..73ed14fa503ffa36ba09d6ccaf1f595d58dedc47 100644 (file)
@@ -146,13 +146,13 @@ impl<'a> DemuxerObject<'a> {
             _ => false,
         }
     }
-    /*pub fn get_duration(&self) -> u64 {
+    pub fn get_duration(&self) -> u64 {
         match *self {
             DemuxerObject::Normal(ref dmx) => dmx.get_duration(),
             DemuxerObject::Raw(ref dmx, _, _) => dmx.get_duration(),
             _ => 0,
         }
-    }*/
+    }
     pub fn get_num_streams(&self) -> usize {
         match *self {
             DemuxerObject::None => 0,
index d0b8d4b117a9fe7eb26ee9632820a4fc81beb12e..fdab8438f8ecbcc8776eb5eb3775e64e6f5ddd19 100644 (file)
@@ -4,7 +4,7 @@ extern crate nihav_registry;
 extern crate nihav_allstuff;
 
 use std::fs::File;
-use std::io::BufReader;
+use std::io::{BufReader, Write};
 use nihav_core::io::byteio::{FileReader, ByteReader};
 use nihav_core::frame::*;
 use nihav_core::options::*;
@@ -17,11 +17,28 @@ use nihav_core::soundcvt::*;
 use nihav_registry::detect;
 use nihav_registry::register;
 use std::env;
+use std::time::{Duration, Instant};
 
 mod demux;
 use crate::demux::*;
 mod null;
 
+fn format_time(ms: u64) -> String {
+    let s = ms / 1000;
+    let ds = (ms % 1000) / 100;
+    let (min, s) = (s / 60, s % 60);
+    let (h, min) = (min / 60, min % 60);
+    if h == 0 {
+        if min == 0 {
+            format!("{}.{}", s, ds)
+        } else {
+            format!("{}:{:02}.{}", min, s, ds)
+        }
+    } else {
+        format!("{}:{:02}:{:02}.{}", h, min, s, ds)
+    }
+}
+
 fn print_options(name: &str, options: &[NAOptionDefinition]) {
     if options.is_empty() {
         println!("No custom options.");
@@ -82,6 +99,7 @@ struct Transcoder {
     no_audio:       bool,
     start:          NATimePoint,
     end:            NATimePoint,
+    verbose:        bool,
 }
 
 macro_rules! parse_and_apply_options {
@@ -931,6 +949,7 @@ fn main() {
                     return;
                 }
             },
+            "--verbose" | "-v" => transcoder.verbose = true,
             _ => {
                 if args[arg_idx].starts_with("--istream") {
                     let opt0 = &args[arg_idx];
@@ -998,6 +1017,9 @@ fn main() {
         println!("cannot find demuxer for '{}'", transcoder.input_name.as_str());
         return;
     }
+    let duration = dmx.get_duration();
+    let duration_string = if duration != 0 { format_time(duration) } else { String::new() };
+
     parse_and_apply_options!(dmx, &transcoder.demux_opts, "input");
     for i in 0..dmx.get_num_streams() {
         let s = dmx.get_stream(i).unwrap();
@@ -1078,6 +1100,8 @@ println!("stream {} - {} {}", i, s, info.get_name());
         println!(" #{}: {} {}", ostr.get_num(), ostr, ostr.get_info().get_name());
     }
 
+    let mut time = Instant::now();
+    let show_interval = Duration::from_millis(100);
     'main_loop: loop {
         let pktres = dmx.get_frame();
         if let Err(DemuxerError::EOF) = pktres { break; }
@@ -1088,6 +1112,20 @@ println!("stream {} - {} {}", i, s, info.get_name());
         let mut pkt = pktres.unwrap();
         if transcoder.start != NATimePoint::None && pkt.ts.less_than(transcoder.start) { continue; }
         let src_id = pkt.get_stream().get_num();
+        if transcoder.verbose && time.elapsed() >= show_interval {
+            if let Some(pts) = pkt.get_pts() {
+                 let cur_time = format_time(NATimeInfo::ts_to_time(pts, 1000, pkt.ts.tb_num, pkt.ts.tb_den));
+                print!(" {}", cur_time);
+            } else {
+                print!(" ???");
+            }
+            if !duration_string.is_empty() {
+                print!(" / {}", duration_string);
+            }
+            print!("\r");
+            std::io::stdout().flush().unwrap();
+            time = Instant::now();
+        }
         match transcoder.encoders[src_id] {
             OutputMode::Drop => {},
             OutputMode::Copy(dst_id) => {
@@ -1162,6 +1200,9 @@ println!("stream {} - {} {}", i, s, info.get_name());
             _ => {},
         };
     }
+    if transcoder.verbose {
+        println!();
+    }
 
     let ret = mux.end();
     if ret.is_err() {