TianyiShi2001 / audiotags

Unified IO for different types of audio metadata
https://tianyishi2001.github.io/audiotags
MIT License
41 stars 29 forks source link

set_duration() #25

Open sloganking opened 1 year ago

sloganking commented 1 year ago

I see Trait audiotags::traits::AudioTagEdit provides duration() but no way to edit it.

And Struct audiotags::anytag::AnyTag provides a mutable duration field but no way to write an AnyTag back to a file.

How can I assign a value to a file's duration metadata?

sloganking commented 1 year ago

I now see impl<'a> From<AnyTag<'a>> for FlacTag https://github.com/TianyiShi2001/audiotags/blob/2fa3f6c0d2d45d471a90b4c68ffa5c5cc8fd36a8/src/components/flac_tag.rs#L8-L40

Which I tried to use via

fn set_duration(file: &Path, duration: f64) {
    use audiotags::*;

    let tag = Tag::new().read_from_path(file).unwrap();
    let mut any_tag = tag.to_anytag();
    any_tag.duration = Some(duration);
    let mut flac_tag: FlacTag = any_tag.into();

    flac_tag
        .write_to_path(file.to_str().unwrap())
        .expect("Fail to save");
}

But that still does not work because impl<'a> From<AnyTag<'a>> for FlacTag throws away the duration field in the AnyTag, and replaces it with None provided by the FlacTag::default().

So I ask again, why does this library have no method for setting the duration?

sloganking commented 1 year ago

I see FlacTag::duration()

https://github.com/TianyiShi2001/audiotags/blob/2fa3f6c0d2d45d471a90b4c68ffa5c5cc8fd36a8/src/components/flac_tag.rs#L127-L131

Is returned from dividing the number of samples from the sample rate

s.total_samples as f64 / f64::from(s.sample_rate)

So it might make since that you can't set duration directly.

That being said, my file's total_samples is incorrectly read as 0. Perhaps total_samples is metadata I can manually count and change?

sloganking commented 1 year ago

ffmpeg seems to be able to do that via ffmpeg -i input.flac -c:v copy -c:a flac output.flac.

https://stackoverflow.com/questions/60653531/ffmpeg-flac-audio-file-duration-in-metadata-is-0