4

I try to merge lots of mp4 files from a directory test into one output.mp4 using ffmpeg in Python.

import os

path = '/Users/x/Documents/test'

for filename in os.listdir(path):
    if filename.endswith(".mp4"):
        print(filename)

Output:

4. 04-unix,minix,Linux.mp4
6. 05-Linux.mp4
7. 06-ls.mp4
5. 04-unix.mp4
9. 08-command.mp4
1. 01-intro.mp4
3. 03-os.mp4
8. 07-minux.mp4
2. 02-os.mp4
10. 09-help.mp4

I have tried with the solution below from the reference here: ffmpy concatenate multiple files with a file list

import os
import subprocess
import time


base_dir = "/path/to/the/files"
video_files = "video_list.txt"
output_file = "output.avi"

# where to seek the files
file_list = open(video_files, "w")

# remove prior output
try:
    os.remove(output_file)
except OSError:
    pass

# scan for the video files
start = time.time()
for root, dirs, files in os.walk(base_dir):
    for video in files:
        if video.endswith(".avi"):
            file_list.write("file './%s'\n" % video)
file_list.close()

# merge the video files
cmd = ["ffmpeg",
       "-f",
       "concat",
       "-safe",
       "0",
       "-loglevel",
       "quiet",
       "-i",
       "%s" % video_files,
       "-c",
       "copy",
       "%s" % output_file
       ]

p = subprocess.Popen(cmd, stdin=subprocess.PIPE)

fout = p.stdin
fout.close()
p.wait()

print(p.returncode)
if p.returncode != 0:
    raise subprocess.CalledProcessError(p.returncode, cmd)

end = time.time()
print("Merging the files took", end - start, "seconds.")

I have merged them and get an output.mp4 but the files are not merged in order with the first number split by point (1, 2, 3, ...): which I can get by filename.split(".")[0]:

1. 01-intro.mp4
2. 02-os.mp4
3. 03-os.mp4
4. 04-unix,minix,Linux.mp4
5. 04-unix.mp4
6. 05-Linux.mp4
7. 06-ls.mp4
8. 07-minux.mp4
9. 08-command.mp4
10. 09-help.mp4

How can I merge them correctly and concisely in Python? Thanks.

ah bon
  • 9,293
  • 12
  • 65
  • 148
  • OK what order are the merged files in? Is it random? Oh and how are expecting your process to handle the two files numbered '04'? – Paula Thomas Jul 07 '19 at 08:41
  • I hope to merge files based on order only by the number in front of the first period from filenames (at this case, it's from 1 to 10) and they are unique. – ah bon Jul 07 '19 at 08:48
  • Yeah you have answered the second part of my question, the more important part is the first part concerning the order you are getting now. – Paula Thomas Jul 07 '19 at 08:50
  • 1
    Am I missing something or are you literally asking how to sort a list? – Aran-Fey Jul 07 '19 at 08:52
  • @Paula Thomas, it's not random, it's order same as output from `for filename in os.listdir(path): if filename.endswith(".mp4"): print(filename)` – ah bon Jul 07 '19 at 08:55
  • @Aran-Fey Yes, kind of. I want merge files into one file and its content is in order `1. 01-intro.mp4 2. 02-os.mp4 3. 03-os.mp4 4. 04-unix,minix,Linux.mp4 5. 04-unix.mp4 6. 05-Linux.mp4 7. 06-ls.mp4 8. 07-minux.mp4 9. 08-command.mp4 10. 09-help.mp4` – ah bon Jul 07 '19 at 08:58
  • OK so the problem with that is? Sorry cut that! Me being daft! – Paula Thomas Jul 07 '19 at 09:58
  • The actual file's content order is: `4. 04-unix,minix,Linux.mp4 6. 05-Linux.mp4 7. 06-ls.mp4 5. 04-unix.mp4 9. 08-command.mp4 1. 01-intro.mp4 3. 03-os.mp4 8. 07-minux.mp4 2. 02-os.mp4 10. 09-help.mp4` – ah bon Jul 07 '19 at 09:59
  • OK this is me not being daft. Where do you think you've sorted the list of files? I can't see it! – Paula Thomas Jul 07 '19 at 10:01
  • I haven't add it. Maybe integrate at here is appropriate: `start = time.time() for root, dirs, files in os.walk(base_dir): for video in files: if video.endswith(".avi"): file_list.write("file './%s'\n" % video) file_list.close()` – ah bon Jul 07 '19 at 10:03
  • 1
    Can't see a sort in there... – Paula Thomas Jul 07 '19 at 10:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196098/discussion-between-ahbon-and-paula-thomas). – ah bon Jul 07 '19 at 10:09

3 Answers3

12

This solution works:

from moviepy.editor import *
import os
from natsort import natsorted

L = []

for root, dirs, files in os.walk("/path/to/the/files"):

    #files.sort()
    files = natsorted(files)
    for file in files:
        if os.path.splitext(file)[1] == '.mp4':
            filePath = os.path.join(root, file)
            video = VideoFileClip(filePath)
            L.append(video)

final_clip = concatenate_videoclips(L)
final_clip.to_videofile("output.mp4", fps=24, remove_temp=False)
ah bon
  • 9,293
  • 12
  • 65
  • 148
1
import glob
import os
 
def concatenate():
  global stringa
  stringa = "ffmpeg -i \"concat:"
  elenco_video = glob.glob("*.mp4")
  elenco_file_temp = []
  for f in elenco_video:
    file = "temp" + str(elenco_video.index(f) + 1) + ".ts"
    os.system("ffmpeg -i " + f + " -c copy -bsf:v h264_mp4toannexb -f mpegts " + file)
    elenco_file_temp.append(file)
  print(elenco_file_temp)
  for f in elenco_file_temp:
    stringa += f
    if elenco_file_temp.index(f) != len(elenco_file_temp)-1:
      stringa += "|"
    else:
      stringa += "\" -c copy  -bsf:a aac_adtstoasc output.mp4"
  print(stringa)
  os.system(stringa)

this will create a concatenated video file named output.mp4 in the working directory,, code source

yazan sayed
  • 777
  • 7
  • 24
1

My videos got crossed up and blurred due to different frame sizes.. to avoid that you can use this. It was only a change to this line:

final_clip = concatenate_videoclips(L,method='compose')

which adds all the clips together despite the different frame sizes. Heres the full code:

from moviepy.editor import *
import os
from natsort import natsorted

def combine_videos_folder_to_one(folder_path):
    L =[]
    for root, dirs, files in os.walk(folder_path):
        #files.sort()
        files = natsorted(files)
        for file in files:
            if os.path.splitext(file)[1] == '.mp4':
                filePath = os.path.join(root, file)
                video = VideoFileClip(filePath)
                L.append(video)

    final_clip = concatenate_videoclips(L,method='compose')
    final_clip.to_videofile("compilation_output.mp4", fps=60, remove_temp=False)
    

combine_videos_folder_to_one(folder_videos)
rachwa
  • 1,805
  • 1
  • 14
  • 17
John Carr
  • 69
  • 3