Spaces:
Paused
Paused
Julian Bilcke
commited on
Commit
·
dd77f63
1
Parent(s):
e2472ff
addAudioToVideo
Browse files
src/services/addAudioToVideo.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import path from 'node:path'
|
| 2 |
+
import { promises as fs } from 'node:fs'
|
| 3 |
+
|
| 4 |
+
import tmpDir from 'temp-dir'
|
| 5 |
+
import ffmpeg from 'fluent-ffmpeg'
|
| 6 |
+
|
| 7 |
+
export const addAudioToVideo = async (videoFilePath: string, audioFilePath: string): Promise<string> => {
|
| 8 |
+
|
| 9 |
+
const tempOutputFilePath = `${videoFilePath.split('.')[0]}-temp.mp4`
|
| 10 |
+
|
| 11 |
+
await new Promise((resolve, reject) => {
|
| 12 |
+
ffmpeg(videoFilePath)
|
| 13 |
+
.input(audioFilePath)
|
| 14 |
+
.outputOptions('-c:v copy') // use video copy codec
|
| 15 |
+
.outputOptions('-c:a aac') // use audio codec
|
| 16 |
+
.outputOptions('-map 0:v:0') // map video from 0th to 0th
|
| 17 |
+
.outputOptions('-map 1:a:0') // map audio from 1st to 0th
|
| 18 |
+
.outputOptions('-shortest') // finish encoding when shortest input stream ends
|
| 19 |
+
.output(tempOutputFilePath)
|
| 20 |
+
.on('end', resolve)
|
| 21 |
+
.on('error', reject)
|
| 22 |
+
.run()
|
| 23 |
+
})
|
| 24 |
+
|
| 25 |
+
// Now we want to replace the original video file with the new file that has been created
|
| 26 |
+
await fs.rename(tempOutputFilePath, videoFilePath)
|
| 27 |
+
|
| 28 |
+
return videoFilePath
|
| 29 |
+
};
|