velfnr

Passing Pipe Streams to Concat - Demux

May 17th, 2022 (edited)
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express')
  2. const mongoose = require('mongoose')
  3. const path = require('path')
  4. const { spawn } = require('child_process')
  5. const { v1: uuidv1 } = require('uuid')
  6. const fs = require('fs')
  7.  
  8. const allVideoMergeRouter = express.Router()
  9.  
  10. allVideoMergeRouter.post('/', (req, res) =>
  11. {
  12.     const videoPaths = req.body['video_file_path_list']
  13.     if(!videoPaths || videoPaths.length === 0)
  14.     {
  15.         return res.json({
  16.             status: "error",
  17.             message: "File paths missing"
  18.         })
  19.     }
  20.  
  21.     const gridfsbucket = new mongoose.mongo.GridFSBucket(mongoose.connection.db, {
  22.         chunkSizeBytes: 1024,
  23.         bucketName: 'filesBucket'
  24.     })
  25.  
  26.     const mergedVideoName = uuidv1() + '.mp4'
  27.     const mergedVideoPath = 'public/upload/' +  mergedVideoName
  28.  
  29.     let myList = ''
  30.     let pipeCount = 3
  31.     const pipes = ['pipe', 'pipe', 'pipe']
  32.  
  33.     const finalVideoStreams = videoPaths.map(videoPath => {
  34.         if(!videoPath.startsWith('public/upload/'))
  35.         {
  36.             return res.json({
  37.                 status: "error",
  38.                 message: "File not found"
  39.             })
  40.         }
  41.         else if(path.extname(videoPath) !== '.mp4')
  42.         {
  43.             return res.json({
  44.                 status: "error",
  45.                 message: "Video File is not a .mp4 file"
  46.             })
  47.         }
  48.         const videoStream = gridfsbucket.openDownloadStreamByName(videoPath.substring(14))
  49.         const cmd1 = spawn('ffmpeg', ['-i', 'pipe:0', '-acodec', 'aac', '-vcodec', 'libx264', '-s', '1920x1080', '-r', '60', '-video_track_timescale', '90000', '-f', 'ismv', 'pipe:1'],
  50.         {stdio: ['pipe', 'pipe', 'pipe']})
  51.         videoStream.pipe(cmd1.stdin)
  52.         myList = myList + "file 'pipe:" + pipeCount + "'\n"
  53.         pipeCount++
  54.         pipes.push('pipe')
  55.         return cmd1.stdout
  56.     })
  57.  
  58.     fs.writeFileSync(__dirname + '/../data/' + mergedVideoName + '.txt', myList)
  59.  
  60.     const command = spawn('ffmpeg', ['-f', 'concat', '-safe', '0', '-protocol_whitelist', 'file,pipe', '-i', 'data/' + mergedVideoName + '.txt', '-c', 'copy', '-f', 'ismv', 'pipe:1'], {
  61.         stdio: pipes
  62.     })
  63.     for(let i = 3; i<pipeCount; i++)
  64.     {
  65.         finalVideoStreams[i-3].pipe(command.stdio[i])
  66.     }
  67.  
  68.     command.stdout.pipe(gridfsbucket.openUploadStream(mergedVideoName)).
  69.     on('error', err => {
  70.         return res.json({
  71.             status: 'error',
  72.             message: err.message
  73.         })
  74.     }).
  75.     on('finish', () => {
  76.         return res.json({
  77.             status: "ok",
  78.             message: "Merged All Video Successfully",
  79.             file_path: mergedVideoPath
  80.         })
  81.     })
  82.  
  83.     command.stderr.on('data', (dt) => {
  84.         console.log('data: ' + dt)
  85.     })
  86. })
  87.  
  88. module.exports = allVideoMergeRouter
Add Comment
Please, Sign In to add comment