This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.rs
228 lines (202 loc) · 6.12 KB
/
mod.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::collections::{HashMap, VecDeque};
use std::convert::TryInto;
use std::fs::File;
use std::io::{Cursor, Read};
use std::iter;
use std::panic;
use std::path::{Path, PathBuf};
use std::time::Duration;
use std::vec::Vec;
use mp4parse::{MediaContext, TrackScaledTime, TrackTimeScale};
use punchtop_audio::{self, Image, Tags};
use rand::distributions::Alphanumeric;
use rand::seq::SliceRandom;
use rand::{self, Rng};
use walkdir::WalkDir;
pub mod dir;
pub mod music;
const FALLBACK_PLAYLIST_SIZE: usize = 60;
pub fn playlist(root: &Path, name: &str, duration: Duration, iterations: u64) -> Playlist {
let mut vec = Vec::new();
let walker = WalkDir::new(root)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file());
for entry in walker {
vec.push(PathBuf::from(entry.path()));
}
let mut rng = rand::thread_rng();
vec.shuffle(&mut rng);
let playlist: Vec<_> = vec
.into_iter()
.filter_map(|path| {
if is_audio_media(&path) && is_sufficient_duration(&path, duration) {
Some(Track::new(path, duration))
} else {
None
}
})
.take(iterations.try_into().unwrap_or(FALLBACK_PLAYLIST_SIZE))
.collect();
Playlist {
name: name.to_owned(),
tracks: VecDeque::from(playlist),
iterations,
cursor: 0,
}
}
// https://developers.google.com/cast/docs/media#audio_codecs
fn is_audio_media(path: &Path) -> bool {
let mime: &str = &tree_magic::from_filepath(path);
match mime {
"audio/mpeg" | "audio/mp3" | "audio/aac" | "audio/mp4" | "audio/flac" | "audio/ogg"
| "application/ogg" | "audio/webm" => true,
_ => false,
}
}
fn is_sufficient_duration(path: &Path, required_duration: Duration) -> bool {
let mime: &str = &tree_magic::from_filepath(path);
match mime {
"audio/mpeg" | "audio/mp3" => {
let ok = panic::catch_unwind(|| {
mp3_duration::from_path(path)
.ok()
.and_then(|duration| duration.checked_sub(required_duration))
.is_some()
});
if let Ok(ok) = ok {
ok
} else {
warn!(
"Panic when checking duration of {} filetype at {:?}",
mime, path
);
false
}
}
"audio/aac" | "audio/mp4" => {
let mut fd = match File::open(path) {
Ok(fd) => fd,
Err(_) => return false,
};
let mut buf = Vec::new();
if fd.read_to_end(&mut buf).is_err() {
return false;
}
let mut c = Cursor::new(&buf);
let mut context = MediaContext::new();
if mp4parse::read_mp4(&mut c, &mut context).is_err() {
return false;
}
context.tracks.into_iter().all(|track| {
match scale_to_micros(track.duration, track.timescale) {
Some(duration) if duration > required_duration.as_micros() => true,
_ => false,
}
})
}
_ => false,
}
}
#[derive(Clone, Debug)]
pub struct Track {
path: PathBuf,
id: String,
duration: Duration,
}
impl Track {
pub fn new(path: PathBuf, duration: Duration) -> Self {
let mut rng = rand::thread_rng();
let id = iter::repeat(())
.map(|()| rng.sample(Alphanumeric))
.take(8)
.collect();
Self { path, id, duration }
}
}
impl punchtop_audio::Track for Track {
fn id(&self) -> &str {
&self.id
}
fn duration(&self) -> Duration {
self.duration
}
fn tags(&self) -> Option<Tags> {
let tags = neguse_taglib::get_tags(&self.path).ok()?;
Some(Tags {
artist: tags.artist.to_option(),
title: tags.title.to_option(),
album: tags.album.to_option(),
})
}
fn cover(&self) -> Option<Image> {
let cover = neguse_taglib::get_front_cover(&self.path)
.ok()
.filter(|img| img.is_some())?;
let mime = cover.mime();
let (width, height, _) = cover.dimensions()?;
Some(Image {
bytes: cover.unwrap(),
mime,
width,
height,
})
}
fn stream(&self) -> Option<Box<dyn Read>> {
let file = File::open(&self.path).ok()?;
Some(Box::new(file))
}
fn content_type(&self) -> String {
tree_magic::from_filepath(&self.path)
}
}
#[derive(Debug)]
pub struct Playlist {
name: String,
tracks: VecDeque<Track>,
iterations: u64,
cursor: u64,
}
impl Playlist {
pub fn name(&self) -> &str {
&self.name
}
pub fn registry(&self) -> HashMap<String, Box<dyn punchtop_audio::Track + Send + Sync>> {
let mut registry = HashMap::new();
for track in &self.tracks {
let track: Box<dyn punchtop_audio::Track + Send + Sync> = Box::new(track.clone());
registry.insert(track.id().to_owned(), track);
}
registry
}
}
impl Iterator for Playlist {
type Item = (u64, Track);
fn next(&mut self) -> Option<Self::Item> {
if self.cursor >= self.iterations {
return None;
}
self.cursor += 1;
let track = self.tracks.pop_front()?;
self.tracks.push_back(track.clone());
Some((self.cursor, track))
}
}
fn scale_to_micros(
duration: Option<TrackScaledTime<u64>>,
scale: Option<TrackTimeScale<u64>>,
) -> Option<u128> {
let microseconds_per_second = 1_000_000;
let numerator = duration.map(|d| d.0)?;
let denominator = scale.map(|s| s.0)?;
if denominator == 0 {
return None;
}
let integer = numerator / denominator;
let remainder = numerator % denominator;
let integer = integer.checked_mul(microseconds_per_second)?;
let remainder = remainder.checked_mul(microseconds_per_second)?;
(remainder / denominator)
.checked_add(integer)
.map(u128::from)
}