pub fn subtitle_extract(
    mp4: &MP4,
    track: &TrackBox,
    format: Format,
    mut output: impl Write,
) -> io::Result<()> {
    let iter = track.sample_info_iter();
    let timescale = iter.timescale();
    let mut seq = 1;

    if format == Format::Vtt {
        write!(output, "WEBVTT\n")?;
        write!(output, "\n")?;
        if let Some(filename) = mp4.input_file.as_ref() {
            write!(output, "NOTE extracted from {}\n", filename)?;
        } else {
            write!(output, "NOTE extracted from mp4 file\n")?;
        }
        let lang = track.media().media_header().language.to_string();
        if lang != "und" && lang != "unk" {
            write!(output, "NOTE language: {}\n", lang)?;
        }
        write!(output, "\n")?;
    }
    let eol = if format == Format::Vtt { "\n" } else { "\r\n" };

    for sample in iter {
        let mut data = mp4.data_ref(sample.fpos, sample.size as u64);
        let subt = match Tx3GTextSample::from_bytes(&mut data) {
            Ok(subt) => subt,
            Err(_) => continue,
        };
        if format == Format::Tx3g {
            output.write(&data)?;
            continue;
        }
        if subt.text.as_str() == "" {
            continue;
        }
        let cue = cue(format, timescale, Some(seq), sample, subt);
        write!(output, "{}{}", cue, eol)?;
        seq += 1;
    }

    Ok(())
}
/// Extract a subtitle track into VTT / SRT or 3GPP.
Extract a subtitle track into VTT / SRT or 3GPP.
Extract a subtitle track into VTT / SRT or 3GPP .
pub fn subtitle_extract(
    mp4: &MP4,
    track: &TrackBox,
    format: Format,
    mut output: impl Write,
) -> io::Result<()> {
    let iter = track.sample_info_iter();
    let timescale = iter.timescale();
    let mut seq = 1;
    if format == Format::Vtt {
        write!(output, "WEBVTT\n")?;
        write!(output, "\n")?;
        if let Some(filename) = mp4.input_file.as_ref() {
            write!(output, "NOTE extracted from {}\n", filename)?;
        } else {
            write!(output, "NOTE extracted from mp4 file\n")?;
        }
        let lang = track.media().media_header().language.to_string();
        if lang != "und" && lang != "unk" {
            write!(output, "NOTE language: {}\n", lang)?;
        }
        write!(output, "\n")?;
    }
    let eol = if format == Format::Vtt { "\n" } else { "\r\n" };
    for sample in iter {
        let mut data = mp4.data_ref(sample.fpos, sample.size as u64);
        let subt = match Tx3GTextSample::from_bytes(&mut data) {
            Ok(subt) => subt,
            Err(_) => continue,
        };
        if format == Format::Tx3g {
            output.write(&data)?;
            continue;
        }
        if subt.text.as_str() == "" {
            continue;
        }
        let cue = cue(format, timescale, Some(seq), sample, subt);
        write!(output, "{}{}", cue, eol)?;
        seq += 1;
    }
    Ok(())
}
pub fn subtitle_extract ( mp4 : & MP4 , track : & TrackBox , format : Format , mut output : impl Write , ) -> io :: Result < ( ) > { let iter = track . sample_info_iter ( ) ; let timescale = iter . timescale ( ) ; let mut seq = 1 ; if format == Format :: Vtt { write ! ( output " \n " ) ? ; write ! ( output " \n " ) ? ; if let Some ( filename ) = mp4 . input_file . as_ref ( ) { write ! ( output " \n " filename ) ? ; } else { write ! ( output " \n " ) ? ; } let lang = track . media ( ) . media_header ( ) . language . to_string ( ) ; if lang != " " && lang != " " { write ! ( output " \n " lang ) ? ; } write ! ( output " \n " ) ? ; } let eol = if format == Format :: Vtt { " \n " } else { " \r \n " } ; for sample in iter { let mut data = mp4 . data_ref ( sample . fpos , sample . size as u64 ) ; let subt = match Tx3GTextSample :: from_bytes ( & mut data ) { Ok ( subt ) => subt , Err ( _ ) => continue , } ; if format == Format :: Tx3g { output . write ( & data ) ? ; continue ; } if subt . text . as_str ( ) == " " { continue ; } let cue = cue ( format , timescale , Some ( seq ) , sample , subt ) ; write ! ( output " " cue eol ) ? ; seq += 1 ; } Ok ( ( ) ) }
Extract a subtitle track into VTT / SRT or 3GPP.
Extract a subtitle track into VTT / SRT or 3GPP .
mp4
&MP4
track
&TrackBox
format
Format
output
impl Write
mp4
&MP4
track
&TrackBox
format
Format
output
impl Write