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?
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?
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