0

So a little clarification i have a folder of videos that i want to combine into one. Ive looked at multiple overflows and everything i try ends up with an error. The first few videos work then on the later ones its just frozen. I can still however hear the audio. I currently have this as my video.js...

import { exec } from 'child_process';
import * as fs from 'fs';

let folders = fs.readdirSync('./videos')


folders.forEach(element => {
    if(element != '.DS_Store'){
        let videos = fs.readdirSync('./videos/'+element)

        fs.writeFileSync('./videos/'+element+'/list.txt','')
        
        try {
            fs.rmSync('./videos/'+element+'/combined.mp4')
        } catch (error) {
            //console.log(error)
        }

        let list = ''
        
        videos.forEach(video => {
            if(video != 'list.txt' && video != '.DS_Store' && video != 'combined.mp4'){
                fs.appendFileSync('./videos/'+element+'/list.txt',('file '+video+'\n'))


                list += `file ${video}`
                list += "\n"




            
            }
        });

        var writeStream = fs.createWriteStream('./videos/'+element+'/list.txt')

        writeStream.write(list)

        writeStream.end()


        exec(`ffmpeg -hwaccel d3d11va -safe 0 -ss 0 -f concat -i  ${'./videos/'+element+'/list.txt'}  -c copy -copyinkf -vsync 1 -s 1920x1080 -sws_flags lanczos -c:v h264 ${'./videos/'+element+'/combined.mp4'}`, {maxBuffer: 1024 * 100000},(error, stdout, stderr) => {
            if (error) {
                console.log(`error: ${error.message}`);
                return;
            }
            else{
                console.log("videos are successfully merged")
        }
            
        })
    }
});

Any Ideas? Tried both on Mac and Windows

bein
  • 11
  • 3
  • Post your ffmpeg command and its log – kesh May 01 '22 at 03:22
  • Please provide enough code so others can better understand or reproduce the problem. – treckstar May 01 '22 at 14:31
  • Make sure the videos all have same matching picture settings (like resolution, frame rate and H.264 Profile). Don't concat random videos of different width/height sizes. – VC.One May 01 '22 at 18:09

1 Answers1

0

Finally got around to fixing this. I was able to find a solution from another post.

The first step to fix this was to convert each MP4 file "to trans-code them into intermediate streams at first". I did this via

let list = ''
videos.forEach(video => {
    if (video != 'list.txt' && video != '.DS_Store' && video != 'combined.mp4' && video.split('.')[1] != 'ts') {
        fs.appendFileSync('./videos/'+element+'/list.txt',('file '+video+'\n'))


        list += `file ${video}.ts`
        list += "\n"

        exec('ffmpeg -i ./videos/'+element+'/'+video+' -c copy -bsf:v h264_mp4toannexb -f mpegts ./videos/'+element+'/'+video+'.ts', {maxBuffer: 1024 * 100000},(error, stdout, stderr) => {
            if (error) {
                console.log(`error: ${error.message}`);
                return;
            } else {
                console.log("Converted "+video)
            }     
        })
    }
});

Then the next step was just to concat them like normal...

exec('ffmpeg -f concat -safe 0 -i ./videos/'+element+'/list.txt'+' -c copy ./videos/'+element+'/'+element+'output.mp4', {maxBuffer: 1024 * 100000},(error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    } else {
        console.log("videos are successfully merged")
    }
})

Referenced specifically answers from T.Todua and Ed999

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
bein
  • 11
  • 3