1

I want to merge 2 or more videos in short time.

I think that concat-demuxer is best, but it need TXT file.

In python, is it possible to use ffmpeg concat-demuxer without TXT file?

rut
  • 79
  • 1
  • 10
  • 1
    if `ffmpeg` needs text file then python will also need this text file - but with Python it can be simpler to generate this file instead of writing it manually. – furas Jul 20 '22 at 14:43
  • maybe check modules like [ffmpeg-python](https://github.com/kkroening/ffmpeg-python) or [MoviePy](https://github.com/Zulko/moviepy) – furas Jul 20 '22 at 14:45
  • 1
    A text file is required. ffmpeg-python usage example: `ffmpeg.input('mylist.txt', f='concat', safe=0).output('output.mp4', codec='copy').overwrite_output().run()`. – Rotem Jul 20 '22 at 16:43
  • 1
    You can try [`ffmpegio`'s `FFConcat` class](https://python-ffmpegio.github.io/python-ffmpegio/concat.html) to pipe in the file list. (Not heavily tested, but I'm the dev and can address any issues you have asap) – kesh Jul 20 '22 at 21:19

1 Answers1

0

Run the program and give input to a target directory containing all the videos.

The video will be sequenced in alphanumeric order of their names and merged in the order.

Input the name for the final video and the merged video will be saved in the target directory.

# Video Merging Code using FFMPEG in python using ffmpeg concat-demuxer in safe mode

# Mimicing the linux command : ffmpeg -f concat -safe 0 -i video_list.txt -c copy output.mp4

# Un-install and re-install correct python supported ffmpeg
# !pip uninstall ffmpeg
# !pip uninstall ffmpeg-python
# !pip install ffmpeg-python

import os
import re
import glob
import ffmpeg

video_files_path = input("Enter path of directory for video list")

video_file_list = glob.glob(f"{video_files_path}/*.mp4")

loaded_video_list = []

def sorted_nicely(l):
    """ Sort the given iterable in the way that humans expect."""
    def convert(text): return int(text) if text.isdigit() else text

    def alphanum_key(key): return [convert(c)
                                   for c in re.split('([0-9]+)', key)]
    return sorted(l, key=alphanum_key)


video_file_list = sorted_nicely(video_file_list)

merged_video_name = input("Enter name for the new video")

merged_video_path = os.path.join(
    video_files_path, f"{merged_video_name}.mp4")

merged_video_list_name = os.path.join(
    video_files_path, f"{merged_video_name}_video_list.txt")

with open(merged_video_list_name, 'w') as f:
    for video in video_file_list:
        print(f"file {video}", file=f)
        print(f"file {video}")

f.close()

ffmpeg.input(merged_video_list_name,
             format='concat', safe=0).output(merged_video_path, c='copy').run()

Please refer to following links for more info:

https://github.com/kkroening/ffmpeg-python/issues/137

https://stackoverflow.com/a/11175851/14950576

https://stackoverflow.com/a/2669120/14950576

https://trac.ffmpeg.org/wiki/Concatenate