Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_duration() #25

Open
sloganking opened this issue Feb 18, 2023 · 3 comments
Open

set_duration() #25

sloganking opened this issue Feb 18, 2023 · 3 comments

Comments

@sloganking
Copy link

sloganking commented Feb 18, 2023

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
Copy link
Author

sloganking commented Feb 18, 2023

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?

@sloganking
Copy link
Author

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?

@sloganking
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant