Skip to main content

selene_core/media_container/
format.rs

1use infer::audio::{is_aiff, is_ape, is_flac, is_mp3, is_ogg, is_wav};
2use serde::{Deserialize, Serialize};
3
4/// All recognized audio containers
5#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Serialize, Deserialize)]
6pub enum ContainerFormat {
7    Flac,
8    Mpa,
9    Ogg,
10    Wav,
11    Aiff,
12    Ape,
13}
14
15impl ContainerFormat {
16    /// Gets container type by reading magic bytes
17    ///
18    /// Returns none if the container type is unsupported or unrecognized, or cannot be read
19    #[must_use] 
20    pub fn from_buf(buf: &[u8; 16]) -> Option<Self> {
21        let container = if is_flac(buf) {
22            ContainerFormat::Flac
23        } else if is_ogg(buf) {
24            ContainerFormat::Ogg
25        } else if is_mp3(buf) {
26            ContainerFormat::Mpa
27        } else if is_wav(buf) {
28            ContainerFormat::Wav
29        } else if is_aiff(buf) {
30            ContainerFormat::Aiff
31        } else if is_ape(buf) {
32            ContainerFormat::Ape
33        } else {
34            return None;
35        };
36        Some(container)
37    }
38
39    /// Returns the name as of the container that ffmpeg uses for encoding (the "`format_name`")
40    #[must_use]
41    pub fn format_name(&self) -> &str {
42        match self {
43            ContainerFormat::Aiff => "aiff",
44            ContainerFormat::Ape => "ape",
45            ContainerFormat::Flac => "flac",
46            ContainerFormat::Mpa => "mp3",
47            ContainerFormat::Ogg => "ogg",
48            ContainerFormat::Wav => "wav",
49        }
50    }
51}