-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
tag_reader.rs
61 lines (49 loc) · 1.85 KB
/
tag_reader.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![allow(missing_docs)]
use lofty::prelude::*;
use lofty::probe::Probe;
use std::path::Path;
fn main() {
let path_str = std::env::args().nth(1).expect("ERROR: No path specified!");
let path = Path::new(&path_str);
if !path.is_file() {
panic!("ERROR: Path is not a file!");
}
let tagged_file = Probe::open(path)
.expect("ERROR: Bad path provided!")
.read()
.expect("ERROR: Failed to read file!");
let tag = match tagged_file.primary_tag() {
Some(primary_tag) => primary_tag,
// If the "primary" tag doesn't exist, we just grab the
// first tag we can find. Realistically, a tag reader would likely
// iterate through the tags to find a suitable one.
None => tagged_file.first_tag().expect("ERROR: No tags found!"),
};
println!("--- Tag Information ---");
println!("Title: {}", tag.title().as_deref().unwrap_or("None"));
println!("Artist: {}", tag.artist().as_deref().unwrap_or("None"));
println!("Album: {}", tag.album().as_deref().unwrap_or("None"));
println!("Genre: {}", tag.genre().as_deref().unwrap_or("None"));
// import keys from https://docs.rs/lofty/latest/lofty/enum.ItemKey.html
println!(
"Album Artist: {}",
tag.get_string(&ItemKey::AlbumArtist).unwrap_or("None")
);
let properties = tagged_file.properties();
let duration = properties.duration();
let seconds = duration.as_secs() % 60;
let duration_display = format!("{:02}:{:02}", (duration.as_secs() - seconds) / 60, seconds);
println!("--- Audio Properties ---");
println!(
"Bitrate (Audio): {}",
properties.audio_bitrate().unwrap_or(0)
);
println!(
"Bitrate (Overall): {}",
properties.overall_bitrate().unwrap_or(0)
);
println!("Sample Rate: {}", properties.sample_rate().unwrap_or(0));
println!("Bit depth: {}", properties.bit_depth().unwrap_or(0));
println!("Channels: {}", properties.channels().unwrap_or(0));
println!("Duration: {duration_display}");
}