Add Detect Media Type by File

This commit is contained in:
MengYX
2020-02-11 15:51:07 +08:00
parent 10f09958c4
commit c314a251c2
7 changed files with 40 additions and 33 deletions

View File

@@ -1,4 +1,7 @@
export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43, 0x00];
export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43];
export const MP3_HEADER = [0x49, 0x44, 0x33];
export const OGG_HEADER = [0x4F, 0x67, 0x67, 0x53];
export const M4A_HEADER = [0x66, 0x74, 0x79, 0x70];
export const AudioMimeType = {
mp3: "audio/mpeg",
flac: "audio/flac",
@@ -45,7 +48,19 @@ export function GetCoverURL(metadata) {
}
export function IsBytesEqual(first, second) {
// if want wholly check, should length first>=second
return first.every((val, idx) => {
return val === second[idx];
})
}
/**
* @return {string}
*/
export function DetectAudioExt(data, fallbackExt) {
if (IsBytesEqual(MP3_HEADER, data.slice(0, MP3_HEADER.length))) return "mp3";
if (IsBytesEqual(FLAC_HEADER, data.slice(0, FLAC_HEADER.length))) return "flac";
if (IsBytesEqual(OGG_HEADER, data.slice(0, OGG_HEADER.length))) return "ogg";
if (IsBytesEqual(M4A_HEADER, data.slice(4, 8))) return "m4a";
return fallbackExt;
}