-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergeChunks.js
29 lines (26 loc) · 1.31 KB
/
mergeChunks.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
import { spawn } from 'node:child_process'
import { readdir } from 'node:fs/promises'
export default async function mergeChunks(dirname) {
// construct merge command
// const watermarkSize = 20
// const mergeChunksCmd = `ffmpeg -i "concat:$(ls -v ${dirname}/*.ts | tr '\n' '|')" -vf "drawtext=text='PYONERIP.COM':x=10:y=h-th-10:[email protected]:fontsize=${watermarkSize}:alpha=0.5" -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k ${dirname}/${dirname}.mp4`
const getOrderNo = (chunkName) => parseInt(chunkName.match(/video(\d+)\.ts/)[1])
const sortByNumber = (a, b) => {
const chunkA = getOrderNo(a)
const chunkB = getOrderNo(b)
return chunkA - chunkB
}
const dirContents = await readdir(dirname)
const tsFiles = dirContents
.filter(file => file.endsWith('.ts'))
.map(file => `${dirname}/${file}`)
.sort(sortByNumber)
.join('|')
const ffmpegOptions = ['-i', `concat:${tsFiles}`, '-c', 'copy', `${dirname}/${dirname}.mp4`]
// run merge command
return new Promise((resolve, reject) => {
const cp = spawn('ffmpeg', ffmpegOptions, { stdio: 'inherit' })
cp.on('spawn', () => console.log('spawned', process.pid))
cp.on('exit', (code) => code != 0 ? reject(code) : resolve(`${dirname}/${dirname}.mp4`))
})
}