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    pub fn from_buf(buf: &[u8; 16]) -> Option<Self> {
20        let container = if is_flac(buf) {
21            ContainerFormat::Flac
22        } else if is_ogg(buf) {
23            ContainerFormat::Ogg
24        } else if is_mp3(buf) {
25            ContainerFormat::Mpa
26        } else if is_wav(buf) {
27            ContainerFormat::Wav
28        } else if is_aiff(buf) {
29            ContainerFormat::Aiff
30        } else if is_ape(buf) {
31            ContainerFormat::Ape
32        } else {
33            return None;
34        };
35        Some(container)
36    }
37
38    /// Returns the name as of the container that ffmpeg uses for encoding (the "`format_name`")
39    #[must_use]
40    pub fn format_name(&self) -> &str {
41        match self {
42            ContainerFormat::Aiff => "aiff",
43            ContainerFormat::Ape => "ape",
44            ContainerFormat::Flac => "flac",
45            ContainerFormat::Mpa => "mp3",
46            ContainerFormat::Ogg => "ogg",
47            ContainerFormat::Wav => "wav",
48        }
49    }
50}