generate_seektable

Function generate_seektable 

Source
pub fn generate_seektable<R: Read>(
    r: R,
    interval: SeekTableInterval,
) -> Result<SeekTable, Error>
Expand description

Given a FLAC stream, generates new seek table

Though encoders should add seek tables by default, sometimes one isn’t present. This function takes an existing FLAC file stream and generates a new seek table suitable for adding to the file’s metadata via the crate::metadata::update function.

The stream should be rewound to the beginning of the file.

§Errors

Returns any error from the underlying stream.

§Example

use flac_codec::{
    encode::{FlacSampleWriter, Options, SeekTableInterval, generate_seektable},
    metadata::{SeekTable, read_block},
};
use std::io::{Cursor, Seek};

let mut flac = Cursor::new(vec![]);  // a FLAC file in memory

// add a seekpoint every second
let options = Options::default().seektable_seconds(1);

let mut writer = FlacSampleWriter::new(
    &mut flac,         // our wrapped writer
    options,           // our seektable options
    44100,             // sample rate
    16,                // bits-per-sample
    1,                 // channel count
    Some(60 * 44100),  // one minute's worth of samples
).unwrap();

// write one minute's worth of samples
writer.write(vec![0; 60 * 44100].as_slice()).unwrap();

// finalize writing file
assert!(writer.finalize().is_ok());

flac.rewind().unwrap();

// get existing seektable
let original_seektable = match read_block::<_, SeekTable>(&mut flac) {
    Ok(Some(seektable)) => seektable,
    _ => panic!("seektable not found"),
};

flac.rewind().unwrap();

// generate new seektable, also with seekpoints every second
let new_seektable = generate_seektable(
    flac,
    SeekTableInterval::Seconds(1.try_into().unwrap())
).unwrap();

// ensure both seektables are identical
assert_eq!(original_seektable, new_seektable);