use anyhow::{anyhow, bail, Result}; use jellycommon::{ItemInfo, SourceTrack, SourceTrackKind}; use jellymatroska::{ block::Block, matroska::MatroskaTag, read::EbmlReader, unflatten::{Unflat, Unflatten}, }; use log::{debug, error, info, trace, warn}; pub fn import_read(input: &mut EbmlReader, iteminfo: &mut ItemInfo) -> Result<()> { // TODO dont traverse the entire file, if the tracks are listed at the end while let Some(item) = input.next() { let item = match item { Ok(item) => item, Err(e) => { warn!("{e}"); break; } }; match item { MatroskaTag::Ebml(_) => { let mut iter = Unflatten::new_with_end(input, item); while let Some(Ok(Unflat { children: _, item })) = iter.next() { match item { MatroskaTag::DocType(t) => { if !matches!(t.as_str(), "matroska" | "webm") { error!("file is neither matroska nor webm but {:?}", t) } } _ => debug!("(re) tag ignored: {item:?}"), } } } MatroskaTag::Segment(_) => { info!("segment start"); let mut children = Unflatten::new_with_end(input, item); import_read_segment(&mut children, iteminfo)?; info!("segment end"); } _ => debug!("(r) tag ignored: {item:?}"), } } Ok(()) } fn import_read_segment(children: &mut Unflatten, iteminfo: &mut ItemInfo) -> Result<()> { let (mut timestamp_scale, mut duration) = (None, None); while let Some(Ok(Unflat { children, item })) = children.next() { match item { MatroskaTag::SeekHead(_) => {} MatroskaTag::Info(_) => { let mut children = children.unwrap(); while let Some(Ok(Unflat { children: _, item })) = children.next() { match item { MatroskaTag::TimestampScale(v) => timestamp_scale = Some(v), MatroskaTag::Duration(v) => duration = Some(v), _ => debug!("(rsi) tag ignored: {item:?}"), } } } MatroskaTag::Cluster(_) => { info!("start of cluster found"); let mut children = children.unwrap(); while let Some(Ok(Unflat { children, item })) = children.next() { match item { MatroskaTag::BlockGroup(_) => { debug!("group"); let mut children = children.unwrap(); while let Some(Ok(Unflat { children: _, item })) = children.next() { match item { MatroskaTag::Block(buf) => { let block = Block::parse(&buf)?; debug!( "block: track={} tso={}", block.track, block.timestamp_off ) } _ => trace!("{item:?}"), } } } MatroskaTag::SimpleBlock(buf) => { let block = Block::parse(&buf)?; debug!( "simple block: track={} tso={}", block.track, block.timestamp_off ) } _ => debug!("(rsc) tag ignored: {item:?}"), } } } MatroskaTag::Tags(_) => {} MatroskaTag::Cues(_) => { let mut children = children.unwrap(); while let Some(Ok(Unflat { children, item })) = children.next() { match item { MatroskaTag::CuePoint(_) => { let mut children = children.unwrap(); while let Some(Ok(Unflat { children: _, item: _ })) = children.next() { // error!("{item:?}") } } _ => (), } } } MatroskaTag::Chapters(_) => {} MatroskaTag::Tracks(_) => { let mut children = children.unwrap(); while let Some(Ok(Unflat { children, item })) = children.next() { match item { MatroskaTag::TrackEntry(_) => { 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, mut fps, mut bit_depth, mut codec_private, mut default_duration, ) = ( None, None, None, None, 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::CodecPrivate(v) => codec_private = Some(v), MatroskaTag::DefaultDuration(v) => default_duration = 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::BitDepth(v) => bit_depth = 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), MatroskaTag::FrameRate(v) => fps = Some(v), _ => (), } } } _ => (), } } // let itrack_index = iteminfo.tracks.len(); let mtrack_index = index.unwrap(); let kind = match kind.ok_or(anyhow!("track type required"))? { 1 => SourceTrackKind::Video { fps: fps.unwrap_or(0.0), // TODO width: width.unwrap(), height: height.unwrap(), }, 2 => SourceTrackKind::Audio { bit_depth: bit_depth.unwrap_or(0) as usize, // TODO channels: channels.unwrap(), sample_rate: sample_rate.unwrap(), }, 17 => SourceTrackKind::Subtitles, _ => bail!("invalid track type"), }; // track_mapping.insert(mtrack_index, itrack_index); iteminfo.tracks.insert( mtrack_index, SourceTrack { default_duration, codec_private, name: name.unwrap_or_else(|| "unnamed".to_string()), codec: codec.unwrap(), language: language.unwrap_or_else(|| "none".to_string()), kind, }, ); } _ => debug!("(rst) tag ignored: {item:?}"), } } } _ => debug!("(rs) tag ignored: {item:?}"), } } iteminfo.duration = (duration.unwrap() * timestamp_scale.unwrap() as f64) as f64 / 1_000_000_000 as f64; Ok(()) }