1 extern crate nihav_core;
2 extern crate nihav_allstuff;
7 use nihav_core::io::byteio::{FileReader, ByteReader};
8 use nihav_core::frame::*;
9 use nihav_core::codecs::*;
10 use nihav_core::demuxers::*;
11 use nihav_core::detect;
12 use nihav_allstuff::*;
16 use crate::frmwriter::*;
19 use crate::wavwriter::WavWriter;
21 #[derive(Clone,Copy,PartialEq)]
36 fn output_frame(&mut self, pkt: &NAPacket, frm: NAFrameRef) {
37 if frm.get_frame_type() != FrameType::Skip {
38 let pts = match self.nmode {
39 NumberMode::Counter => { self.frameno },
40 NumberMode::PktPTS => { pkt.get_pts().unwrap() },
41 NumberMode::FrmPTS => { if let Some(pt) = frm.get_pts() { pt } else { pkt.get_pts().unwrap() } },
43 let vinfo = frm.get_buffer().get_video_info().unwrap();
44 if vinfo.get_format().is_paletted() {
45 write_palppm(self.prefix, self.streamno, pts, frm);
46 } else if vinfo.get_format().get_model().is_yuv() {
47 write_pgmyuv(self.prefix, self.streamno, pts, frm);
48 } else if vinfo.get_format().get_model().is_rgb() {
49 write_ppm(self.prefix, self.streamno, pts, frm);
51 panic!(" unknown format");
59 wwr: WavWriter<'static>,
64 fn new(name: &String) -> Self { Self { wwr: WavWriter::new(name), wrote_header: false } }
65 fn output_frame(&mut self, _pkt: &NAPacket, frm: NAFrameRef) {
66 if !self.wrote_header {
67 self.wwr.write_header(frm.get_info().as_ref().get_properties().get_audio_info().unwrap()).unwrap();
68 self.wrote_header = true;
70 self.wwr.write_frame(frm.get_buffer()).unwrap();
81 let args: Vec<_> = env::args().collect();
84 println!("usage: nihav-tool [-noout] [-vn] [-an] input [lastpts]");
87 let mut lastpts: Option<u64> = None;
88 let mut cur_arg: usize = 1;
89 let mut noout = false;
90 let mut decode_video = true;
91 let mut decode_audio = true;
92 let mut nmode = NumberMode::FrmPTS;
94 while (cur_arg < args.len()) && (args[cur_arg].chars().next().unwrap() == '-') {
95 match args[cur_arg].as_str() {
97 "-noout" => { noout = true; },
98 "-an" => { decode_audio = false; },
99 "-vn" => { decode_video = false; },
100 "-nm=count" => { nmode = NumberMode::Counter; },
101 "-nm=pktpts" => { nmode = NumberMode::PktPTS; },
102 "-nm=frmpts" => { nmode = NumberMode::FrmPTS; },
103 _ => { println!("unknown option {}", args[cur_arg]); return; },
107 let name = args[cur_arg].as_str();
109 if cur_arg < args.len() {
110 lastpts = Some(u64::from_str_radix(args[cur_arg].as_str(), 10).unwrap());
113 let path = Path::new(name);
114 let mut file = File::open(path).unwrap();
116 let mut fr = FileReader::new_read(&mut file);
117 let mut br = ByteReader::new(&mut fr);
118 let res = detect::detect_format(name, &mut br);
120 println!("cannot detect format for {}", name);
123 let (dmx_name, _) = res.unwrap();
124 println!("trying demuxer {} on {}", dmx_name, name);
126 let mut dmx_reg = RegisteredDemuxers::new();
127 nihav_register_all_demuxers(&mut dmx_reg);
128 let mut dec_reg = RegisteredDecoders::new();
129 nihav_register_all_codecs(&mut dec_reg);
131 dmx_fact = dmx_reg.find_demuxer(dmx_name).unwrap();
132 br.seek(SeekFrom::Start(0)).unwrap();
133 let mut dmx = create_demuxer(dmx_fact, &mut br).unwrap();
135 let mut decs: Vec<Option<(Box<NADecoderSupport>, Box<NADecoder>)>> = Vec::new();
136 let mut sids: Vec<u32> = Vec::new();
137 let mut writers: Vec<Outputter> = Vec::new();
138 for i in 0..dmx.get_num_streams() {
139 let s = dmx.get_stream(i).unwrap();
140 let info = s.get_info();
141 let decfunc = dec_reg.find_decoder(info.get_name());
142 println!("stream {} - {} {}", i, s, info.get_name());
143 let str_id = s.get_id();
144 let mut has_out = false;
148 if decfunc.is_none() {
149 println!("no video decoder found!");
152 let mut dec = (decfunc.unwrap())();
153 let mut dsupp = Box::new(NADecoderSupport::new());
154 dec.init(&mut dsupp, info).unwrap();
155 decs.push(Some((dsupp, dec)));
157 writers.push(Outputter::Video(FrameOutput{prefix: "", streamno: i, frameno: 1, nmode: nmode}));
163 } else if info.is_audio() {
165 if decfunc.is_none() {
166 println!("no audio decoder found!");
169 let mut dec = (decfunc.unwrap())();
170 let mut dsupp = Box::new(NADecoderSupport::new());
171 dec.init(&mut dsupp, info).unwrap();
172 decs.push(Some((dsupp, dec)));
174 let name = format!("out{:02}.wav", i);
175 writers.push(Outputter::Audio(AudioOutput::new(&name)));
183 panic!("decoder {} not found", info.get_name());
186 writers.push(Outputter::None);
191 let pktres = dmx.get_frame();
192 if let Err(e) = pktres {
193 if e == DemuxerError::EOF { break; }
195 let pkt = pktres.unwrap();
196 let streamno = pkt.get_stream().get_id();
197 let sr = sids.iter().position(|x| *x == streamno);
198 let idx = sr.unwrap();
199 if let Some((ref mut dsupp, ref mut dec)) = decs[idx] {
200 let frm = dec.decode(dsupp, &pkt).unwrap();
203 Outputter::Video(ref mut wr) => { wr.output_frame(&pkt, frm); },
204 Outputter::Audio(ref mut wr) => { wr.output_frame(&pkt, frm); },
209 if pkt.get_pts() != None && lastpts.is_some() && pkt.get_pts() >= lastpts { break; }