Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express')
- const mongoose = require('mongoose')
- const path = require('path')
- const { spawn } = require('child_process')
- const { v1: uuidv1 } = require('uuid')
- const fs = require('fs')
- const allVideoMergeRouter = express.Router()
- allVideoMergeRouter.post('/', (req, res) =>
- {
- const videoPaths = req.body['video_file_path_list']
- if(!videoPaths || videoPaths.length === 0)
- {
- return res.json({
- status: "error",
- message: "File paths missing"
- })
- }
- const gridfsbucket = new mongoose.mongo.GridFSBucket(mongoose.connection.db, {
- chunkSizeBytes: 1024,
- bucketName: 'filesBucket'
- })
- const mergedVideoName = uuidv1() + '.mp4'
- const mergedVideoPath = 'public/upload/' + mergedVideoName
- let myList = ''
- let pipeCount = 3
- const pipes = ['pipe', 'pipe', 'pipe']
- const finalVideoStreams = videoPaths.map(videoPath => {
- if(!videoPath.startsWith('public/upload/'))
- {
- return res.json({
- status: "error",
- message: "File not found"
- })
- }
- else if(path.extname(videoPath) !== '.mp4')
- {
- return res.json({
- status: "error",
- message: "Video File is not a .mp4 file"
- })
- }
- const videoStream = gridfsbucket.openDownloadStreamByName(videoPath.substring(14))
- const cmd1 = spawn('ffmpeg', ['-i', 'pipe:0', '-acodec', 'aac', '-vcodec', 'libx264', '-s', '1920x1080', '-r', '60', '-video_track_timescale', '90000', '-f', 'ismv', 'pipe:1'],
- {stdio: ['pipe', 'pipe', 'pipe']})
- videoStream.pipe(cmd1.stdin)
- myList = myList + "file 'pipe:" + pipeCount + "'\n"
- pipeCount++
- pipes.push('pipe')
- return cmd1.stdout
- })
- fs.writeFileSync(__dirname + '/../data/' + mergedVideoName + '.txt', myList)
- const command = spawn('ffmpeg', ['-f', 'concat', '-safe', '0', '-protocol_whitelist', 'file,pipe', '-i', 'data/' + mergedVideoName + '.txt', '-c', 'copy', '-f', 'ismv', 'pipe:1'], {
- stdio: pipes
- })
- for(let i = 3; i<pipeCount; i++)
- {
- finalVideoStreams[i-3].pipe(command.stdio[i])
- }
- command.stdout.pipe(gridfsbucket.openUploadStream(mergedVideoName)).
- on('error', err => {
- return res.json({
- status: 'error',
- message: err.message
- })
- }).
- on('finish', () => {
- return res.json({
- status: "ok",
- message: "Merged All Video Successfully",
- file_path: mergedVideoPath
- })
- })
- command.stderr.on('data', (dt) => {
- console.log('data: ' + dt)
- })
- })
- module.exports = allVideoMergeRouter
Add Comment
Please, Sign In to add comment