forked from serguun42/Social-Picker-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo-audio-merge.js
96 lines (84 loc) · 3.31 KB
/
video-audio-merge.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable new-cap */
import { exec } from 'child_process';
import { createHash } from 'crypto';
import { resolve as pathResolve } from 'path';
import { createWriteStream } from 'fs';
import { unlink } from 'fs/promises';
import { pipeline } from 'stream/promises';
import fetch from 'node-fetch';
import LogMessageOrError from './log.js';
const TEMP_FOLDER = process.env.TEMP || '/tmp/';
/**
* @typedef {Object} VideoAudioMergeOptions
* @property {boolean} [loopVideo]
* @property {boolean} [loopAudio]
*/
/**
* @param {string} video
* @param {string} audio
* @param {VideoAudioMergeOptions} [options]
* @returns {Promise<import('../types/social-post').VideoAudioMerged>}
*/
const VideoAudioMerge = (video, audio, options = {}) => {
if (!video) return Promise.reject(new Error('No video URL'));
if (!audio) return Promise.resolve({ externalUrl: video });
const videoBaseFilename = `socialpicker_${createHash('sha256').update(`${video}_${Date.now()}`).digest('hex')}`;
const videoFilename = pathResolve(TEMP_FOLDER, `${videoBaseFilename}_video`);
const videoFiletype = video.replace(/\?.*$/, '').match(/\.(\w+)$/)?.[1] || 'mp4';
const audioFilename = pathResolve(TEMP_FOLDER, `${videoBaseFilename}_audio`);
const mergedFilename = pathResolve(TEMP_FOLDER, `${videoBaseFilename}_out.${videoFiletype}`);
const DeleteTempFiles = () => {
unlink(videoFilename).catch(() => {});
unlink(audioFilename).catch(() => {});
};
const DeleteMergedFile = () => {
unlink(mergedFilename).catch(() => {});
};
return fetch(video)
.then((response) => {
if (response.status !== 200)
return Promise.reject(new Error(`Response status on video (${video}) is ${response.status}`));
return pipeline(response.body, createWriteStream(videoFilename));
})
.then(() => fetch(audio))
.then((response) => {
if (response.status !== 200)
return Promise.reject(new Error(`Response status on audio (${audio}) is ${response.status}`));
return pipeline(response.body, createWriteStream(audioFilename));
})
.then(
() =>
new Promise((ffmpegResolve, ffmpegReject) => {
const ffmpegCommand = `ffmpeg ${options.loopVideo ? '-stream_loop -1 ' : ''}-i "${videoFilename}" ${
options.loopAudio && !options.loopVideo ? '-stream_loop -1 ' : ''
}-i "${audioFilename}" ${
/** Limiting loop for 20MB */
options.loopAudio || options.loopVideo ? '-shortest -fs 20M ' : ''
}-c:v copy -c:a aac -qscale 0 "${mergedFilename}"`;
const ffmpegProcess = exec(ffmpegCommand, { cwd: TEMP_FOLDER }, (error, _stdout, stderr) => {
if (error || stderr) {
ffmpegProcess.kill();
ffmpegReject(error || new Error(stderr));
}
});
ffmpegProcess.on('error', (e) => ffmpegReject(e));
ffmpegProcess.on('exit', () => ffmpegResolve());
})
)
.then(() => {
DeleteTempFiles();
return Promise.resolve({
filename: mergedFilename,
fileCallback: DeleteMergedFile,
videoSource: video,
audioSource: audio,
});
})
.catch((e) => {
LogMessageOrError(e);
DeleteTempFiles();
DeleteMergedFile();
return Promise.resolve({ externalUrl: video });
});
};
export default VideoAudioMerge;