Spaces:
Sleeping
Sleeping
Commit
·
d66ad20
1
Parent(s):
44b5f05
add mime type detection
Browse files
src/core/ffmpeg/concatenateVideosAndMergeAudio.mts
CHANGED
@@ -9,6 +9,7 @@ import { writeBase64ToFile } from "../files/writeBase64ToFile.mts";
|
|
9 |
import { getMediaInfo } from "./getMediaInfo.mts";
|
10 |
import { removeTemporaryFiles } from "../files/removeTmpFiles.mts";
|
11 |
import { addBase64Header } from "../base64/addBase64.mts";
|
|
|
12 |
|
13 |
type ConcatenateVideoAndMergeAudioOptions = {
|
14 |
output?: string;
|
@@ -39,10 +40,11 @@ export const concatenateVideosAndMergeAudio = async ({
|
|
39 |
await fs.mkdir(tempDir);
|
40 |
|
41 |
let i = 0
|
42 |
-
for (const
|
43 |
-
if (!
|
44 |
-
const
|
45 |
-
|
|
|
46 |
audioFilePaths.push(audioFilePath);
|
47 |
}
|
48 |
audioFilePaths = audioFilePaths.filter((audio) => existsSync(audio))
|
@@ -50,11 +52,13 @@ export const concatenateVideosAndMergeAudio = async ({
|
|
50 |
|
51 |
// Decode and concatenate base64 video tracks to temporary file
|
52 |
i = 0
|
53 |
-
for (const
|
54 |
-
if (!
|
55 |
-
const videoFilePath = path.join(tempDir, `video${++i}.mp4`);
|
56 |
|
57 |
-
|
|
|
|
|
|
|
58 |
|
59 |
videoFilePaths.push(videoFilePath);
|
60 |
}
|
|
|
9 |
import { getMediaInfo } from "./getMediaInfo.mts";
|
10 |
import { removeTemporaryFiles } from "../files/removeTmpFiles.mts";
|
11 |
import { addBase64Header } from "../base64/addBase64.mts";
|
12 |
+
import { extractBase64 } from "../base64/extractBase64.mts";
|
13 |
|
14 |
type ConcatenateVideoAndMergeAudioOptions = {
|
15 |
output?: string;
|
|
|
40 |
await fs.mkdir(tempDir);
|
41 |
|
42 |
let i = 0
|
43 |
+
for (const audioTrack of audioTracks) {
|
44 |
+
if (!audioTrack) { continue }
|
45 |
+
const analysis = extractBase64(audioTrack)
|
46 |
+
const audioFilePath = path.join(tempDir, `audio${++i}.${analysis.extension}`);
|
47 |
+
await writeBase64ToFile(addBase64Header(audioTrack, analysis.extension), audioFilePath);
|
48 |
audioFilePaths.push(audioFilePath);
|
49 |
}
|
50 |
audioFilePaths = audioFilePaths.filter((audio) => existsSync(audio))
|
|
|
52 |
|
53 |
// Decode and concatenate base64 video tracks to temporary file
|
54 |
i = 0
|
55 |
+
for (const videoTrack of videoTracks) {
|
56 |
+
if (!videoTrack) { continue }
|
|
|
57 |
|
58 |
+
const analysis = extractBase64(videoTrack)
|
59 |
+
const videoFilePath = path.join(tempDir, `video${++i}.${analysis.extension}`);
|
60 |
+
|
61 |
+
await writeBase64ToFile(addBase64Header(videoTrack, analysis.extension), videoFilePath);
|
62 |
|
63 |
videoFilePaths.push(videoFilePath);
|
64 |
}
|
src/core/ffmpeg/concatenateVideosWithAudio.mts
CHANGED
@@ -9,6 +9,7 @@ import { writeBase64ToFile } from "../files/writeBase64ToFile.mts";
|
|
9 |
import { getMediaInfo } from "./getMediaInfo.mts";
|
10 |
import { removeTemporaryFiles } from "../files/removeTmpFiles.mts";
|
11 |
import { addBase64Header } from "../base64/addBase64.mts";
|
|
|
12 |
|
13 |
export type SupportedExportFormat = "mp4" | "webm"
|
14 |
export const defaultExportFormat = "mp4"
|
@@ -44,9 +45,10 @@ export const concatenateVideosWithAudio = async ({
|
|
44 |
await fs.mkdir(tempDir);
|
45 |
|
46 |
if (audioTrack && audioTrack.length > 0) {
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
50 |
}
|
51 |
|
52 |
// Decode and concatenate base64 video tracks to temporary file
|
@@ -55,11 +57,12 @@ export const concatenateVideosWithAudio = async ({
|
|
55 |
if (!track) { continue }
|
56 |
// note: here we assume the input video is in mp4
|
57 |
|
58 |
-
const
|
|
|
59 |
|
60 |
-
console.log(
|
61 |
|
62 |
-
await writeBase64ToFile(addBase64Header(track,
|
63 |
|
64 |
videoFilePaths.push(videoFilePath);
|
65 |
}
|
@@ -70,7 +73,7 @@ export const concatenateVideosWithAudio = async ({
|
|
70 |
const tempFilePath = await concatenateVideos({
|
71 |
videoFilePaths,
|
72 |
})
|
73 |
-
console.log(`concatenateVideosWithAudio: tempFilePath = ${tempFilePath}`)
|
74 |
|
75 |
// Check if the concatenated video has audio or not
|
76 |
const tempMediaInfo = await getMediaInfo(tempFilePath.filepath);
|
|
|
9 |
import { getMediaInfo } from "./getMediaInfo.mts";
|
10 |
import { removeTemporaryFiles } from "../files/removeTmpFiles.mts";
|
11 |
import { addBase64Header } from "../base64/addBase64.mts";
|
12 |
+
import { extractBase64 } from "../base64/extractBase64.mts";
|
13 |
|
14 |
export type SupportedExportFormat = "mp4" | "webm"
|
15 |
export const defaultExportFormat = "mp4"
|
|
|
45 |
await fs.mkdir(tempDir);
|
46 |
|
47 |
if (audioTrack && audioTrack.length > 0) {
|
48 |
+
const analysis = extractBase64(audioTrack)
|
49 |
+
console.log(`concatenateVideosWithAudio: writing down an audio file (${analysis.extension}) from the supplied base64 track`)
|
50 |
+
audioFilePath = path.join(tempDir, `audio.${analysis.extension}`);
|
51 |
+
await writeBase64ToFile(addBase64Header(audioTrack, analysis.extension), audioFilePath);
|
52 |
}
|
53 |
|
54 |
// Decode and concatenate base64 video tracks to temporary file
|
|
|
57 |
if (!track) { continue }
|
58 |
// note: here we assume the input video is in mp4
|
59 |
|
60 |
+
const analysis = extractBase64(audioTrack)
|
61 |
+
const videoFilePath = path.join(tempDir, `video${++i}.${analysis.extension}`);
|
62 |
|
63 |
+
console.log(`concatenateVideosWithAudio: writing down a video file (${analysis.extension}) from the supplied base64 track`)
|
64 |
|
65 |
+
await writeBase64ToFile(addBase64Header(track, analysis.extension), videoFilePath);
|
66 |
|
67 |
videoFilePaths.push(videoFilePath);
|
68 |
}
|
|
|
73 |
const tempFilePath = await concatenateVideos({
|
74 |
videoFilePaths,
|
75 |
})
|
76 |
+
console.log(`concatenateVideosWithAudio: tempFilePath = ${JSON.stringify(tempFilePath, null, 2)}`)
|
77 |
|
78 |
// Check if the concatenated video has audio or not
|
79 |
const tempMediaInfo = await getMediaInfo(tempFilePath.filepath);
|