TianyiShi2001/audiotags

set_duration()

Opened this issue · 3 comments

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?

I now see impl<'a> From<AnyTag<'a>> for FlacTag

impl<'a> From<AnyTag<'a>> for FlacTag {
fn from(inp: AnyTag<'a>) -> Self {
let mut t = FlacTag::default();
if let Some(v) = inp.title() {
t.set_title(v)
}
if let Some(v) = inp.artists_as_string() {
t.set_artist(&v)
}
if let Some(v) = inp.year {
t.set_year(v)
}
if let Some(v) = inp.album_title() {
t.set_album_title(v)
}
if let Some(v) = inp.album_artists_as_string() {
t.set_artist(&v)
}
if let Some(v) = inp.track_number() {
t.set_track_number(v)
}
if let Some(v) = inp.total_tracks() {
t.set_total_tracks(v)
}
if let Some(v) = inp.disc_number() {
t.set_disc_number(v)
}
if let Some(v) = inp.total_discs() {
t.set_total_discs(v)
}
t
}
}

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?

I see FlacTag::duration()

fn duration(&self) -> Option<f64> {
self.inner
.get_streaminfo()
.map(|s| s.total_samples as f64 / f64::from(s.sample_rate))
}

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?

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