Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 2,354 Bytes
a88efe5 1cea837 a88efe5 1cea837 a88efe5 1cea837 350665d 1cea837 350665d 1cea837 a88efe5 1cea837 a88efe5 1cea837 a88efe5 1cea837 b66c635 1cea837 b66c635 1cea837 |
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 |
import { tmpdir } from "node:os"
import { writeFile, rm } from "node:fs/promises"
import { join } from "node:path"
import ffmpeg from "fluent-ffmpeg"
export type MediaMetadata = {
durationInSec: number;
durationInMs: number;
hasAudio: boolean;
};
/**
* Get the media info of a base64 or file path
* @param input
* @returns
*/
export async function getMediaInfo(input: string): Promise<MediaMetadata> {
// If the input is a base64 string
if (input.startsWith("data:")) {
// Extract the base64 content
// Extract the base64 content
const [head, tail] = input.split(";base64,")
if (!tail) {
throw new Error("Invalid base64 data");
}
const extension = head.split("/").pop() || ""
const base64Content = tail || ""
// Decode the base64 content to a buffer
const buffer = Buffer.from(base64Content, 'base64')
// Generate a temporary file name
const tempFileName = join(tmpdir(), `temp-media-${Date.now()}.${extension}`);
// Write the buffer to a temporary file
await writeFile(tempFileName, buffer);
// Get metadata from the temporary file then delete the file
try {
return await getMetaDataFromPath(tempFileName);
} finally {
await rm(tempFileName);
}
}
// If the input is a path to the file
return await getMetaDataFromPath(input);
}
async function getMetaDataFromPath(filePath: string): Promise<MediaMetadata> {
return new Promise((resolve, reject) => {
ffmpeg.ffprobe(filePath, (err, metadata) => {
let results = {
durationInSec: 0,
durationInMs: 0,
hasAudio: false,
}
if (err) {
console.error("getMediaInfo(): failed to analyze the source (might happen with empty files)", err)
// reject(err);
resolve(results);
return;
}
try {
results.durationInSec = metadata?.format?.duration || 0;
results.durationInMs = results.durationInSec * 1000;
results.hasAudio = (metadata?.streams || []).some((stream) => stream.codec_type === 'audio');
} catch (err) {
console.error(`getMediaInfo(): failed to analyze the source (might happen with empty files)`, err)
results.durationInSec = 0
results.durationInMs = 0
results.hasAudio = false
}
resolve(results);
});
});
} |