forked from Serial-ATA/lofty-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag_reader.rs
58 lines (48 loc) · 1.84 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
use lofty::{Accessor, AudioFile, Probe, TaggedFileExt};
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(&lofty::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}");
}