add support for opus files

This commit is contained in:
Yannik Böttcher 2020-08-06 17:26:50 +02:00
parent fe5ab5ba48
commit fee96e6b49
5 changed files with 43 additions and 0 deletions

7
Cargo.lock generated
View file

@ -1457,6 +1457,11 @@ dependencies = [
"vcpkg 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "opus_headers"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "parking_lot"
version = "0.9.0"
@ -1589,6 +1594,7 @@ dependencies = [
"metaflac 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"mp3-duration 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"mp4ameta 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"opus_headers 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"pbkdf2 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -3079,6 +3085,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum openssl 0.10.30 (registry+https://github.com/rust-lang/crates.io-index)" = "8d575eff3665419f9b83678ff2815858ad9d11567e082f5ac1814baba4e2bcb4"
"checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"
"checksum openssl-sys 0.9.58 (registry+https://github.com/rust-lang/crates.io-index)" = "a842db4709b604f0fe5d1170ae3565899be2ad3d9cbc72dedc789ac0511f78de"
"checksum opus_headers 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "afbb993947f111397c2bc536944f8dac7f54a4e73383d478efe1990b56404b60"
"checksum parking_lot 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a4893845fa2ca272e647da5d0e46660a314ead9c2fdd9a883aabc32e481a8733"
"checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252"
"checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b"

View file

@ -45,6 +45,7 @@ thiserror = "1.0.19"
time = "0.1"
toml = "0.5"
url = "2.1"
opus_headers = "0.1.2"
[dependencies.rocket_contrib]
version = "0.4.5"

View file

@ -9,6 +9,7 @@ use mp4ameta;
use regex::Regex;
use std::fs;
use std::path::Path;
use opus_headers;
use crate::utils;
use crate::utils::AudioFormat;
@ -33,6 +34,7 @@ pub fn read(path: &Path) -> Option<SongTags> {
Some(AudioFormat::MP4) => Some(read_mp4(path)),
Some(AudioFormat::MPC) => Some(read_ape(path)),
Some(AudioFormat::OGG) => Some(read_vorbis(path)),
Some(AudioFormat::OPUS) => Some(read_opus(path)),
_ => None,
};
match data {
@ -175,6 +177,37 @@ fn read_vorbis(path: &Path) -> Result<SongTags> {
Ok(tags)
}
#[cfg_attr(feature = "profile-index", flame)]
fn read_opus(path: &Path) -> Result<SongTags> {
let headers = opus_headers::parse_from_path(path)?;
let mut tags = SongTags {
artist: None,
album_artist: None,
album: None,
title: None,
duration: None,
disc_number: None,
track_number: None,
year: None,
};
for (key, value) in headers.comments.user_comments {
match key.as_str().to_uppercase().as_str() {
"TITLE" => tags.title = Some(value),
"ALBUM" => tags.album = Some(value),
"ARTIST" => tags.artist = Some(value),
"ALBUMARTIST" => tags.album_artist = Some(value),
"TRACKNUMBER" => tags.track_number = value.parse::<u32>().ok(),
"DISCNUMBER" => tags.disc_number = value.parse::<u32>().ok(),
"DATE" => tags.year = value.parse::<i32>().ok(),
_ => (),
}
}
Ok(tags)
}
#[cfg_attr(feature = "profile-index", flame)]
fn read_flac(path: &Path) -> Result<SongTags> {
let tag = metaflac::Tag::read_from_path(path)?;

View file

@ -30,6 +30,7 @@ pub enum AudioFormat {
MP4,
MPC,
OGG,
OPUS,
}
#[cfg_attr(feature = "profile-index", flame)]
@ -48,6 +49,7 @@ pub fn get_audio_format(path: &Path) -> Option<AudioFormat> {
"m4a" => Some(AudioFormat::MP4),
"mpc" => Some(AudioFormat::MPC),
"ogg" => Some(AudioFormat::OGG),
"opus" => Some(AudioFormat::OPUS),
_ => None,
}
}

BIN
test/sample.opus Normal file

Binary file not shown.