I am using FFMPEG and MOVIEPY For segmentation of a video, Basically I want to cut and remove some parts from vide:
here is my code :
import os
import argparse
import subprocess
from datetime import timedelta
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Trim sections from input videos.')
parser.add_argument('--input', type=str, default='input', help='Input folder path')
parser.add_argument('--output', type=str, default='output', help='Output folder path')
parser.add_argument('--sections', type=str, default='00:00:00-00:00:03', help='Comma-separated list of sections to remove (in HH:MM:SS format)')
args = parser.parse_args()
# Create output directory if it doesn't exist
if not os.path.exists(args.output):
os.makedirs(args.output)
# Loop over all video files in input directory
for file in os.listdir(args.input):
if file.endswith('.mp4'):
# Build input and output file paths
input_file = os.path.join(args.input, file)
output_file = os.path.join(args.output, file)
# Build ffmpeg command to trim the video
cmd = ['ffmpeg', '-i', input_file]
# Add filter_complex argument to remove specified sections
filter_complex = ''
duration = subprocess.check_output(['ffprobe', '-i', input_file, '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")])
duration = float(duration)
sections = args.sections.split(',')
sections_to_remove = []
for section in sections:
start, end = section.split('-')
end_time = duration if end == 'end' else timedelta(hours=int(end[:2]), minutes=int(end[3:5]), seconds=int(end[6:]))
sections_to_remove.append((start, end_time))
for section in sections_to_remove:
start_time = section[0]
end_time = section[1]
filter_complex += f'[0:v]trim=start={start_time}:end={end_time},setpts=PTS-STARTPTS[v{sections_to_remove.index(section)}];'
filter_complex += f'[0:a]atrim=start={start_time}:end={end_time},asetpts=PTS-STARTPTS[a{sections_to_remove.index(section)}];'
for i in range(len(sections_to_remove)):
filter_complex += f'[v{i}] [a{i}] '
# Add output file argument and run ffmpeg command
cmd += ['-filter_complex', filter_complex[:-1], '-map', '[v{}]'.format(len(sections_to_remove)), '-map', '[a{}]'.format(len(sections_to_remove)), '-c:v', 'libx264', '-c:a', 'aac', '-movflags', '+faststart', output_file]
subprocess.call(cmd)
I get this error : [AVFilterGraph @ 000001f80c0b3ac0] No such filter: '' Error initializing complex filters. Invalid argument
Example of the command I use to run : python mo.py --sections "00:00:01-00:00:03,00:00:20-00:00:30"