Python to Split a Video File by Given Time Period with Full Source Code For Beginners

This script will split the video into two files when valid time periods are given.


  • Install python in the host system Install ffmpeg-python
  • ffmpeg==1.4
pip install ffmpeg-python

Run the Script:

python test.mp4 0 50 out1.mp4 out2.mp4
Code language: CSS (css)


python -h
Code language: CSS (css)

Source Code:

import ffmpeg
import argparse

parser = argparse.ArgumentParser(description='''Split A media file
                                            into two chunks''')
parser.add_argument('inputfile', help="Input filename")
parser.add_argument('starttime', type=float, help="Start time in seconds")
parser.add_argument('endtime', type=float, help="End time in seconds")
parser.add_argument('outputfile1', help="Output filename")
parser.add_argument('outputfile2', help="Output filename")

args = parser.parse_args()

in1 = ffmpeg.input(args.inputfile)

v1 = in1.filter('trim', start=float(args.starttime), end=(args.endtime))
v2 = in1.filter('trim', start=float(args.endtime))

out1 = ffmpeg.output(v1, args.outputfile1)
out2 = ffmpeg.output(v2, args.outputfile2) language: PHP (php)

Leave a Comment