add support for extracting track number
This commit is contained in:
parent
88275bd35f
commit
07c3aa7edc
|
@ -46,13 +46,13 @@ impl TagLibFile {
|
|||
|
||||
pub fn oggtag(&self) -> Result<TagLibOggTag, TagLibError> {
|
||||
if let Some(taglib_type) = &self.taglib_type {
|
||||
let supported = match taglib_type {
|
||||
let supported = matches!(
|
||||
taglib_type,
|
||||
TagLibFileType::OggFLAC
|
||||
| TagLibFileType::OggOpus
|
||||
| TagLibFileType::OggSpeex
|
||||
| TagLibFileType::OggVorbis => true,
|
||||
_ => false,
|
||||
};
|
||||
| TagLibFileType::OggOpus
|
||||
| TagLibFileType::OggSpeex
|
||||
| TagLibFileType::OggVorbis
|
||||
);
|
||||
|
||||
if !supported {
|
||||
panic!("ogg tag not supported")
|
||||
|
|
|
@ -27,4 +27,13 @@ impl Tag for TagLibTag {
|
|||
|
||||
unsafe { bindings::wrap_taglib_tag_set_artist(self.ctx, artist.as_ptr()) };
|
||||
}
|
||||
fn track(&self) -> Option<u64> {
|
||||
let track = unsafe { bindings::wrap_taglib_tag_track(self.ctx) };
|
||||
|
||||
if track == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(track as u64)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ pub trait File {
|
|||
pub trait Tag {
|
||||
fn title(&self) -> Option<String>;
|
||||
fn artist(&self) -> Option<String>;
|
||||
fn track(&self) -> Option<u64>;
|
||||
fn set_title(&mut self, title: String);
|
||||
fn set_artist(&mut self, artist: String);
|
||||
}
|
||||
|
|
|
@ -70,6 +70,11 @@ char* wrap_taglib_tag_artist(TagLib_Tag *tag) {
|
|||
return stringToCharArray(t->artist());
|
||||
}
|
||||
|
||||
unsigned int wrap_taglib_tag_track(TagLib_Tag *tag) {
|
||||
const TagLib::Tag *t = reinterpret_cast<const TagLib::Tag *>(tag);
|
||||
return t->track();
|
||||
}
|
||||
|
||||
void wrap_taglib_tag_set_title(TagLib_Tag *tag, const char *title) {
|
||||
TagLib::Tag *t = reinterpret_cast<TagLib::Tag *>(tag);
|
||||
t->setTitle(charArrayToString(title));
|
||||
|
|
|
@ -17,6 +17,7 @@ bool wrap_taglib_file_save(TagLib_File *file);
|
|||
|
||||
char* wrap_taglib_tag_title(TagLib_Tag *tag);
|
||||
char* wrap_taglib_tag_artist(TagLib_Tag *tag);
|
||||
unsigned int wrap_taglib_tag_track(TagLib_Tag *tag);
|
||||
void wrap_taglib_tag_set_title(TagLib_Tag *tag, const char *title);
|
||||
void wrap_taglib_tag_set_artist(TagLib_Tag *tag, const char *artist);
|
||||
|
||||
|
|
|
@ -18,12 +18,14 @@ pub struct GetTagsCommandArgs {
|
|||
struct Tags {
|
||||
title: String,
|
||||
artist: String,
|
||||
track_number: Option<u64>,
|
||||
}
|
||||
|
||||
fn from_main_tags(tags: &crate::types::Tags) -> Tags {
|
||||
Tags {
|
||||
title: tags.title.clone(),
|
||||
artist: tags.artist.clone(),
|
||||
track_number: tags.track_number,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ use crate::utils::format_detection::FileFormat;
|
|||
pub struct Tags {
|
||||
pub title: String,
|
||||
pub artist: String,
|
||||
pub track_number: Option<u64>,
|
||||
}
|
||||
|
||||
impl Default for Tags {
|
||||
|
@ -13,6 +14,7 @@ impl Default for Tags {
|
|||
Tags {
|
||||
title: "".to_string(),
|
||||
artist: "".to_string(),
|
||||
track_number: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,9 @@ pub struct FFProbeOutputFormat {
|
|||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct FFProbeOutputTags {
|
||||
#[serde(alias = "TRACK", alias = "track")]
|
||||
pub track_number: Option<String>,
|
||||
|
||||
#[serde(alias = "TITLE")]
|
||||
pub title: String,
|
||||
#[serde(default, alias = "ARTIST")]
|
||||
|
@ -42,6 +45,7 @@ impl Default for FFProbeOutputTags {
|
|||
artist: "".to_string(),
|
||||
replaygain_track_peak: None,
|
||||
replaygain_track_gain: None,
|
||||
track_number: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,15 +8,31 @@ pub struct FFProbeTags {
|
|||
pub artist: String,
|
||||
pub replaygain_track_peak: Option<String>,
|
||||
pub replaygain_track_gain: Option<String>,
|
||||
pub track_number: Option<u64>,
|
||||
}
|
||||
|
||||
impl From<ffprobe_output::FFProbeOutputTags> for FFProbeTags {
|
||||
fn from(val: ffprobe_output::FFProbeOutputTags) -> Self {
|
||||
let mut track_number: Option<u64> = None;
|
||||
if let Some(number) = val.track_number {
|
||||
let mut number = number.clone();
|
||||
if number.contains('/') {
|
||||
let mut split = number.split('/');
|
||||
number = split.next().unwrap().to_string()
|
||||
}
|
||||
|
||||
track_number = match number.parse::<u64>() {
|
||||
Ok(n) => Some(n),
|
||||
Err(_e) => None,
|
||||
}
|
||||
}
|
||||
|
||||
FFProbeTags {
|
||||
title: val.title,
|
||||
artist: val.artist,
|
||||
replaygain_track_peak: val.replaygain_track_peak,
|
||||
replaygain_track_gain: val.replaygain_track_gain,
|
||||
track_number,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@ impl GenericFFMpegAudioFormat {
|
|||
self.extracted_data.tags = Tags {
|
||||
title: output.tags.title,
|
||||
artist: output.tags.artist,
|
||||
track_number: output.tags.track_number,
|
||||
};
|
||||
|
||||
if output.tags.replaygain_track_gain.is_some()
|
||||
|
@ -72,7 +73,7 @@ impl FormatHandler for GenericFFMpegAudioFormat {
|
|||
tags.title = title.clone();
|
||||
}
|
||||
|
||||
if let Some(artist) = &self.changes.title {
|
||||
if let Some(artist) = &self.changes.artist {
|
||||
tags.artist = artist.clone();
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ impl FormatHandler for FLACAudioFormat {
|
|||
fn get_tags(&self, allow_missing: bool) -> Result<Tags, BoxedError> {
|
||||
let title = flac_get_first(&self.flac_tags, "TITLE");
|
||||
let artist = flac_get_first(&self.flac_tags, "ARTIST");
|
||||
let mut track_number = flac_get_first(&self.flac_tags, "TRACKNUMBER");
|
||||
|
||||
if !allow_missing {
|
||||
if title.is_none() {
|
||||
|
@ -37,9 +38,23 @@ impl FormatHandler for FLACAudioFormat {
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(number) = &track_number {
|
||||
if number.contains('/') {
|
||||
let mut split = number.split('/');
|
||||
track_number = Some(split.next().unwrap().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Tags {
|
||||
title: title.unwrap(),
|
||||
artist: artist.unwrap(),
|
||||
track_number: match track_number {
|
||||
Some(num) => match num.parse::<u64>() {
|
||||
Ok(n) => Some(n),
|
||||
Err(_e) => None,
|
||||
},
|
||||
None => None,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ impl FormatHandler for ID3AudioFormat {
|
|||
Ok(Tags {
|
||||
title: String::from(title.unwrap()),
|
||||
artist: String::from(artist.unwrap()),
|
||||
track_number: self.id3_tags.track().map(|track_number| track_number as u64),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -116,8 +117,8 @@ impl FormatHandler for ID3AudioFormat {
|
|||
) -> Result<AudioFileInfo, BoxedError> {
|
||||
Ok(AudioFileInfo {
|
||||
tags: self.get_tags(allow_missing_tags)?,
|
||||
supports_replaygain: self.supports_replaygain(),
|
||||
format: Some(FileFormat::MP3),
|
||||
supports_replaygain: self.supports_replaygain(),
|
||||
contains_replaygain: self.contains_replaygain_tags(),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ impl FormatHandler for TaglibAudioFormat {
|
|||
Ok(Tags {
|
||||
title: title.unwrap(),
|
||||
artist: artist.unwrap(),
|
||||
track_number: tags.track(),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue