0
votes

I'm trying to concatenate 3 videos at time from a list.txt, i can concatenate all the videos in the list in a single long video, but i wish to concatenate x videos from the list in a single output, then concatenate the next x videos from the same list in another single output and so on.

The script i'm developing it's written in python, it fetches some videos from a server and save them locally in a folder and write my concat.txt, then ffmpeg read the concat.txt and create a single output.mp4


#this merge all video in concat file in a single output
os.system("ffmpeg -f concat -i downloaded/concat.txt -safe 1 -r 30 -fflags +genpts -c:a copy downloaded/output.mp4")

I'm looking for a way to make ffmpeg read the first x row from the concat.txt , concatenate them in output1.mp4, then read the next x row from cancat.txt , concatenate them in output2.mp4 and so on.

Thank you for your time to help me, i really appreciate that!

---EDIT Thank to @Tejas for the reply, i solved how to split the concat.txt file, now i'm trying to apply a filter to every clip

import os
x = 3 #Number of files you want to concatenate

#Making directories so that the working directory stays organized
txtFileName = "./splits/output{}.txt"
outputFile = "./clip/output{}.mp4"
postFile = "./media/post{}.mp4"
os.makedirs(os.path.dirname(txtFileName), exist_ok=True)
os.makedirs(os.path.dirname(outputFile), exist_ok=True)
os.makedirs(os.path.dirname(postFile), exist_ok=True)


#While splitting the files store their path to a list
listofSplitFiles = []
with open('./downloaded/concat.txt','r') as concat:
    lines = concat.readlines()
    for i in range(0,lines.__len__()//x):
        with open(txtFileName.format(i+1),'w') as split:
            listofSplitFiles.append(txtFileName.format(i+1))
            for j in range(0,x):
                if( (i*x)+j < lines.__len__() ):
                    split.write(lines[(i*x)+j])

#Call ffmpeg on the list
for i in listofSplitFiles:
    outputBaseName = os.path.basename(i)
    outputFileName = os.path.splitext(outputBaseName)[0]
    postFileName = os.path.splitext(outputBaseName)[0]
    os.system("ffmpeg -f concat -i {} -safe 1 -r 30 -fflags +genpts -c:a copy ./clip/{}.mp4".format(i,outputFileName))
    os.system('''ffmpeg -loglevel error -r 30 -i sfondo/bkg.png -i ./clip/output{}.mp4 -b:v 1M -filter_complex ''' + '''"[1:v]scale=''' + "750" + ''':''' + "1080" + ''' [ovrl], [0:v][ovrl]overlay=(main_w-overlay_w)/2:((main_h-overlay_h)/2)"''' + ''' ./media/{}.mp4''' .format(outputFileName,postFileName)) 

Unfortunately i have this error ./clip/{}.mp4: No such file or directory

1

1 Answers

1
votes

From the documentation of ffmpeg concat it looks like it'll read through the entire list of files in the txt file and concatenate them into a single file. So what you could do is use python to read your concat.txt, put three lines into a different text file and then you could run ffmpeg on those files. Like so:

import os
x = 3 #Number of files you want to concatenate

#Making directories so that the working directory stays organized
txtFileName = "./splits/output{}.txt"
outputFile = "./output/output{}.mp4"
os.makedirs(os.path.dirname(txtFileName), exist_ok=True)
os.makedirs(os.path.dirname(outputFile), exist_ok=True)

#While splitting the files store their path to a list
listofSplitFiles = []
with open('./concat.txt','r') as concat:
    lines = concat.readlines()
    for i in range(0,lines.__len__()//x):
        with open(txtFileName.format(i+1),'w') as split:
            listofSplitFiles.append(txtFileName.format(i+1))
            for j in range(0,x):
                if( (i*x)+j < lines.__len__() ):
                    split.write(lines[(i*x)+j])

#Call ffmpeg on the list
for i in listofSplitFiles:
    outputBaseName = os.path.basename(i)
    outputFileName = os.path.splitext(outputBaseName)[0]
    os.system("ffmpeg -f concat -i {} -safe 1 -r 30 -fflags +genpts -c:a copy ./output/{}.mp4".format(i,outputFileName))

Something to note though, there are better ways to read a file but from what I see I think you need a quick script to concatenate multiple files together. So f.readlines() should not be problem