termusic 0.6.1

Terminal Music Player written in Rust. Can download music from youtube(netease/migu/kugou) and then embed lyrics and album photos into mp3/m4a/flac/wav/ogg vorbis files. Need GStreamer installed to play the music.
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use anyhow::{bail, Result};
use std::borrow::Cow;
use std::convert::TryFrom;
use std::io::{Cursor, Read};

use byteorder::{BigEndian, ReadBytesExt};
use std::io::{Seek, SeekFrom};

#[allow(unused)]
pub const APE_PICTYPES: [&str; 21] = [
    "Other",
    "Png Icon",
    "Icon",
    "Front",
    "Back",
    "Leaflet",
    "Media",
    "Lead Artist",
    "Artist",
    "Conductor",
    "Band",
    "Composer",
    "Lyricist",
    "Recording Location",
    "During Recording",
    "During Performance",
    "Video Capture",
    "Fish",
    "Illustration",
    "Band Logotype",
    "Publisher Logotype",
];

/// Mime types for covers.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum MimeType {
    /// PNG image
    Png,
    /// JPEG image
    Jpeg,
    /// TIFF image
    Tiff,
    /// BMP image
    Bmp,
    /// GIF image
    Gif,
}

impl MimeType {
    /// Converts the `MimeType` to an ape str
    pub const fn as_ape(self) -> &'static [u8; 4] {
        match self {
            MimeType::Png => b"PNG\0",
            MimeType::Jpeg => b"JPEG",
            MimeType::Tiff => b"TIFF",
            MimeType::Bmp => b"BMP\0",
            MimeType::Gif => b"GIF\0",
        }
    }
}

impl TryFrom<&str> for MimeType {
    type Error = anyhow::Error;

    fn try_from(inp: &str) -> Result<Self> {
        Ok(match inp {
            "image/jpeg" => Self::Jpeg,
            "image/png" => Self::Png,
            "image/tiff" => Self::Tiff,
            "image/bmp" => Self::Bmp,
            "image/gif" => Self::Gif,
            _ => bail!("Unsupported ext: {}", inp),
        })
    }
}

impl From<MimeType> for &'static str {
    fn from(mt: MimeType) -> Self {
        match mt {
            MimeType::Jpeg => "image/jpeg",
            MimeType::Png => "image/png",
            MimeType::Tiff => "image/tiff",
            MimeType::Bmp => "image/bmp",
            MimeType::Gif => "image/gif",
        }
    }
}

impl From<MimeType> for String {
    fn from(mt: MimeType) -> Self {
        <MimeType as Into<&'static str>>::into(mt).to_owned()
    }
}

pub trait PicType {
    fn as_u32(&self) -> u32;
    fn from_u32(bytes: u32) -> PictureType;
    fn as_ape_key(&self) -> &str;
    fn from_ape_key(key: &str) -> PictureType;
}

/// The picture type
#[allow(missing_docs, unused)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum PictureType {
    Other,
    Icon,
    OtherIcon,
    CoverFront,
    CoverBack,
    Leaflet,
    Media,
    LeadArtist,
    Artist,
    Conductor,
    Band,
    Composer,
    Lyricist,
    RecordingLocation,
    DuringRecording,
    DuringPerformance,
    ScreenCapture,
    BrightFish,
    Illustration,
    BandLogo,
    PublisherLogo,
    Undefined(u8),
}

// /// Alias for PictureType
// #[cfg(feature = "format-id3")]
// pub type PictureType = id3::frame::PictureType;
#[allow(unused)]
impl PicType for PictureType {
    // ID3/OGG specific methods

    fn as_u32(&self) -> u32 {
        match self {
            Self::Other => 0,
            Self::Icon => 1,
            Self::OtherIcon => 2,
            Self::CoverFront => 3,
            Self::CoverBack => 4,
            Self::Leaflet => 5,
            Self::Media => 6,
            Self::LeadArtist => 7,
            Self::Artist => 8,
            Self::Conductor => 9,
            Self::Band => 10,
            Self::Composer => 11,
            Self::Lyricist => 12,
            Self::RecordingLocation => 13,
            Self::DuringRecording => 14,
            Self::DuringPerformance => 15,
            Self::ScreenCapture => 16,
            Self::BrightFish => 17,
            Self::Illustration => 18,
            Self::BandLogo => 19,
            Self::PublisherLogo => 20,
            Self::Undefined(i) => u32::from(i.to_owned()),
        }
    }

    #[allow(clippy::cast_possible_truncation)]
    fn from_u32(bytes: u32) -> Self {
        match bytes {
            0 => Self::Other,
            1 => Self::Icon,
            2 => Self::OtherIcon,
            3 => Self::CoverFront,
            4 => Self::CoverBack,
            5 => Self::Leaflet,
            6 => Self::Media,
            7 => Self::LeadArtist,
            8 => Self::Artist,
            9 => Self::Conductor,
            10 => Self::Band,
            11 => Self::Composer,
            12 => Self::Lyricist,
            13 => Self::RecordingLocation,
            14 => Self::DuringRecording,
            15 => Self::DuringPerformance,
            16 => Self::ScreenCapture,
            17 => Self::BrightFish,
            18 => Self::Illustration,
            19 => Self::BandLogo,
            20 => Self::PublisherLogo,
            i => Self::Undefined(i as u8),
        }
    }

    // APE specific methods

    fn as_ape_key(&self) -> &str {
        match self {
            Self::Other => "Cover Art (Other)",
            Self::Icon => "Cover Art (Png Icon)",
            Self::OtherIcon => "Cover Art (Icon)",
            Self::CoverFront => "Cover Art (Front)",
            Self::CoverBack => "Cover Art (Back)",
            Self::Leaflet => "Cover Art (Leaflet)",
            Self::Media => "Cover Art (Media)",
            Self::LeadArtist => "Cover Art (Lead Artist)",
            Self::Artist => "Cover Art (Artist)",
            Self::Conductor => "Cover Art (Conductor)",
            Self::Band => "Cover Art (Band)",
            Self::Composer => "Cover Art (Composer)",
            Self::Lyricist => "Cover Art (Lyricist)",
            Self::RecordingLocation => "Cover Art (Recording Location)",
            Self::DuringRecording => "Cover Art (During Recording)",
            Self::DuringPerformance => "Cover Art (During Performance)",
            Self::ScreenCapture => "Cover Art (Video Capture)",
            Self::BrightFish => "Cover Art (Fish)",
            Self::Illustration => "Cover Art (Illustration)",
            Self::BandLogo => "Cover Art (Band Logotype)",
            Self::PublisherLogo => "Cover Art (Publisher Logotype)",
            Self::Undefined(_) => "",
        }
    }

    fn from_ape_key(key: &str) -> Self {
        match key {
            "Cover Art (Other)" => Self::Other,
            "Cover Art (Png Icon)" => Self::Icon,
            "Cover Art (Icon)" => Self::OtherIcon,
            "Cover Art (Front)" => Self::CoverFront,
            "Cover Art (Back)" => Self::CoverBack,
            "Cover Art (Leaflet)" => Self::Leaflet,
            "Cover Art (Media)" => Self::Media,
            "Cover Art (Lead Artist)" => Self::LeadArtist,
            "Cover Art (Artist)" => Self::Artist,
            "Cover Art (Conductor)" => Self::Conductor,
            "Cover Art (Band)" => Self::Band,
            "Cover Art (Composer)" => Self::Composer,
            "Cover Art (Lyricist)" => Self::Lyricist,
            "Cover Art (Recording Location)" => Self::RecordingLocation,
            "Cover Art (During Recording)" => Self::DuringRecording,
            "Cover Art (During Performance)" => Self::DuringPerformance,
            "Cover Art (Video Capture)" => Self::ScreenCapture,
            "Cover Art (Fish)" => Self::BrightFish,
            "Cover Art (Illustration)" => Self::Illustration,
            "Cover Art (Band Logotype)" => Self::BandLogo,
            "Cover Art (Publisher Logotype)" => Self::PublisherLogo,
            _ => Self::Undefined(0),
        }
    }
}

/// Represents a picture, with its data and mime type.
///
/// NOTE: The width, height, `color_depth`, and `num_color` fields can only be read from formats
/// supporting FLAC pictures
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct OggPicture {
    /// The picture type
    pub pic_type: PictureType,
    /// The picture's mimetype
    pub mime_type: MimeType,
    /// The picture's description
    pub description: Option<Cow<'static, str>>,
    /// The picture's width in pixels
    pub width: u32,
    /// The picture's height in pixels
    pub height: u32,
    /// The picture's color depth in bits per pixel
    pub color_depth: u32,
    /// The number of colors used
    pub num_colors: u32,
    /// The binary data of the picture
    pub data: Cow<'static, [u8]>,
}

#[allow(unused)]
impl OggPicture {
    /// Create a new `Picture`
    pub fn new(
        pic_type: PictureType,
        mime_type: MimeType,
        description: Option<String>,
        dimensions: (u32, u32),
        color_depth: u32,
        num_colors: u32,
        data: Vec<u8>,
    ) -> Self {
        Self {
            pic_type,
            mime_type,
            description: description.map(Cow::from),
            width: dimensions.0,
            height: dimensions.1,
            color_depth,
            num_colors,
            data: Cow::from(data),
        }
    }

    /// Convert the [`Picture`] back to an APIC byte vec:
    ///
    /// * FLAC `METADATA_BLOCK_PICTURE`
    #[allow(clippy::cast_possible_truncation)]
    pub fn as_apic_bytes(&self) -> Vec<u8> {
        let mut data: Vec<u8> = Vec::new();

        let picture_type = self.pic_type.as_u32().to_be_bytes();

        let mime_str = String::from(self.mime_type);
        let mime_len = mime_str.len() as u32;

        data.extend(picture_type.iter());
        data.extend(mime_len.to_be_bytes().iter());
        data.extend(mime_str.as_bytes().iter());

        if let Some(desc) = self.description.clone() {
            let desc_str = desc.to_string();
            let desc_len = desc_str.len() as u32;

            data.extend(desc_len.to_be_bytes().iter());
            data.extend(desc_str.as_bytes().iter());
        }

        data.extend(self.width.to_be_bytes().iter());
        data.extend(self.height.to_be_bytes().iter());
        data.extend(self.color_depth.to_be_bytes().iter());
        data.extend(self.num_colors.to_be_bytes().iter());

        let pic_data = &self.data;
        let pic_data_len = pic_data.len() as u32;

        data.extend(pic_data_len.to_be_bytes().iter());
        data.extend(pic_data.iter());

        data
    }

    /// Get a [`Picture`] from APIC bytes:
    ///
    /// * FLAC `METADATA_BLOCK_PICTURE`
    ///
    /// # Errors
    ///
    /// This function will return [`NotAPicture`][`LoftyError::NotAPicture`] if at any point it's
    /// unable to parse the data
    #[allow(clippy::cast_possible_truncation)]
    pub fn from_apic_bytes(bytes: &[u8]) -> Result<Self> {
        let data = match base64::decode(bytes) {
            Ok(o) => o,
            Err(_) => bytes.to_vec(),
        };

        let mut cursor = Cursor::new(data);

        if let Ok(bytes) = cursor.read_u32::<BigEndian>() {
            let picture_type = PictureType::from_u32(bytes);

            if let Ok(mime_len) = cursor.read_u32::<BigEndian>() {
                let mut buf = vec![0; mime_len as usize];
                cursor.read_exact(&mut buf)?;

                if let Ok(mime_type_str) = String::from_utf8(buf) {
                    if let Ok(mime_type) = MimeType::try_from(&*mime_type_str) {
                        let mut description = None;

                        if let Ok(desc_len) = cursor.read_u32::<BigEndian>() {
                            if cursor.get_ref().len()
                                >= (cursor.position() as u32 + desc_len) as usize
                            {
                                let mut buf = vec![0; desc_len as usize];
                                cursor.read_exact(&mut buf)?;

                                if let Ok(desc) = String::from_utf8(buf) {
                                    description = Some(Cow::from(desc));
                                }
                            } else {
                                cursor.set_position(cursor.position() - 4);
                            }
                        }

                        if let (Ok(width), Ok(height), Ok(color_depth), Ok(num_colors)) = (
                            cursor.read_u32::<BigEndian>(),
                            cursor.read_u32::<BigEndian>(),
                            cursor.read_u32::<BigEndian>(),
                            cursor.read_u32::<BigEndian>(),
                        ) {
                            if let Ok(data_len) = cursor.read_u32::<BigEndian>() {
                                let mut binary = vec![0; data_len as usize];

                                if let Ok(()) = cursor.read_exact(&mut binary) {
                                    return Ok(Self {
                                        pic_type: picture_type,
                                        mime_type,
                                        description,
                                        width,
                                        height,
                                        color_depth,
                                        num_colors,
                                        data: Cow::from(binary.clone()),
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }

        bail!("Not a picture");
    }

    // #[cfg(feature = "format-ape")]
    /// Convert the [`Picture`] back to an `APEv2` byte vec:
    ///
    /// * `APEv2` Cover Art
    pub fn as_ape_bytes(&self) -> Vec<u8> {
        let mut data: Vec<u8> = Vec::new();

        if let Some(desc) = &self.description {
            data.extend(desc.as_bytes().iter());
        }

        data.extend([0].iter());
        data.extend(self.data.iter());

        data
    }

    // #[cfg(feature = "format-ape")]
    /// Get a [`Picture`] from an `APEv2` binary item:
    ///
    /// * `APEv2` Cover Art
    ///
    /// NOTES:
    ///
    /// * This function expects the key and its trailing null byte to have been removed
    /// * Since APE tags only store the binary data, the width, height, `color_depth`, and
    ///   `num_colors` fields will be zero.
    ///
    /// # Errors
    ///
    /// This function will return [`NotAPicture`][`LoftyError::NotAPicture`] if at any point it's
    /// unable to parse the data
    #[allow(clippy::cast_possible_truncation)]
    pub fn from_ape_bytes(key: &str, bytes: &[u8]) -> Result<Self> {
        if !bytes.is_empty() {
            let pic_type = PictureType::from_ape_key(key);

            let mut cursor = Cursor::new(bytes);

            let description = {
                let mut text = String::new();

                while let Ok(ch) = cursor.read_u8() {
                    if ch != b'\0' {
                        text.push(char::from(ch));
                        continue;
                    }

                    break;
                }

                (!text.is_empty()).then(|| Cow::from(text))
            };

            let mime_type = {
                let mut identifier = [0; 4];
                cursor.read_exact(&mut identifier)?;

                cursor.seek(SeekFrom::Current(-4))?;

                match identifier {
                    [0x89, b'P', b'N', b'G'] => MimeType::Png,
                    _ if [0xFF, 0xD8] == identifier[..2] => MimeType::Jpeg,
                    _ if b"GIF" == &identifier[..3] => MimeType::Gif,
                    _ if b"BM" == &identifier[..2] => MimeType::Bmp,
                    _ if b"II" == &identifier[..2] => MimeType::Tiff,
                    _ => bail!("Not a picture"),
                }
            };

            let pos = cursor.position() as usize;
            let data = Cow::from(cursor.into_inner()[pos..].to_vec());

            return Ok(Self {
                pic_type,
                mime_type,
                description,
                width: 0,
                height: 0,
                color_depth: 0,
                num_colors: 0,
                data,
            });
        }

        bail!("Not a picture")
    }
}