Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to cut videos automatically using FFMPEG
#!/usr/bin/env python
"""
Cut videos specified in the description file using FFMPEG.
"""
import os
import sys
import subprocess
import argparse
def ffmpeg_is_present():
try:
subprocess.Popen('ffmpeg', stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
except OSError:
return False
else:
return True
def output_video_filename(input_video_filename, output_directory):
name, extension = os.path.splitext(os.path.basename(input_video_filename))
for i in range(0, 100):
filename = os.path.join(output_directory, '{0}_{1:02d}{2}'.format(name, i, extension))
if not os.path.exists(filename):
return filename
raise ValueError('More than 100 cut videos from a single input file!')
if __name__ == '__main__':
if not ffmpeg_is_present():
print('ffmpeg is not in PATH')
sys.exit(1)
argparser = argparse.ArgumentParser(description='Cut videos in the specified description file using FFMPEG')
argparser.add_argument('-o', '--output_directory', help='Directory for the cut videos')
argparser.add_argument('description_file', help='File with time interval description')
args = argparser.parse_args()
desc = args.description_file
if not os.path.isfile(desc):
print('Description file {0} does not exist!'.format(desc))
sys.exit(1)
with open(desc) as f:
for line in map(str.rstrip, f):
if not line:
continue
fn, start, length = line.split(',')
video_filename = os.path.join(os.path.dirname(desc), fn)
if not os.path.isfile(video_filename):
raise FileNotFoundError(video_filename)
print('Processing file ', video_filename, '...')
cut_video_filename = output_video_filename(video_filename, args.output_directory)
command = 'ffmpeg -i {0} -ss {1}.0 -vcodec copy -t {2}.0 {3}'.format(video_filename, start, length, cut_video_filename)
print(command)
subprocess.run(command, shell=True, check=True)
Capture01.mpg,00:00:06,01:14:46
Capture01.mpg,01:14:54,01:02:30
Capture01.mpg,02:17:24,01:29:49
Capture01.mpg,03:47:13,00:29:08
Capture02.mpg,00:00:13,00:28:24
Capture03.mpg,00:00:00,00:00:34
Capture03.mpg,00:00:34,00:53:06
Capture03.mpg,00:53:40,00:35:44
Capture03.mpg,01:29:22,00:43:49
Capture03.mpg,02:13:11,00:07:24
Capture03.mpg,02:20:35,00:05:15
Capture03.mpg,02:25:50,00:05:19
Capture03.mpg,02:32:34,00:58:37
Capture03.mpg,03:31:11,00:29:04
Capture03.mpg,04:00:15,00:21:37
Capture04.mpg,00:00:00,00:07:17
Capture04.mpg,00:07:17,00:44:44
Capture04.mpg,00:52:01,00:44:36
Capture04.mpg,01:36:37,00:29:02
Capture04.mpg,02:05:39,00:43:55
Capture05.mpg,00:00:31,00:29:58
Capture06.mpg,00:00:06,01:28:01
Capture06.mpg,01:28:07,01:26:22
Capture06.mpg,02:54:29,01:28:59
Capture07.mpg,00:00:06,00:43:28
Capture08.mpg,00:00:22,00:59:15
Capture09.mpg,00:00:00,00:21:35
Capture09.mpg,00:21:35,00:18:10
Capture10.mpg,00:00:00,00:08:17
Capture10.mpg,00:08:17,00:12:46
Capture10.mpg,00:21:09,00:29:41
Capture11.mpg,00:00:07,00:46:35
Capture11.mpg,00:46:42,00:12:28
Capture11.mpg,00:59:10,00:13:17
Capture11.mpg,01:12:27,00:24:35
Capture11.mpg,01:37:02,00:34:44
Capture12.mpg,00:00:02,00:00:09
Capture12.mpg,00:00:11,00:02:43
Capture12.mpg,00:02:54,00:00:35
Capture12.mpg,00:03:29,00:07:54
Capture12.mpg,00:11:23,00:03:03
Capture12.mpg,00:14:31,00:05:50
Capture13.mpg,00:00:00,00:03:52
Capture13.mpg,00:03:52,00:01:08
Capture13.mpg,00:05:00,00:06:47
Capture13.mpg,00:11:47,00:05:00
Capture13.mpg,00:12:31,00:00:44
Capture13.mpg,00:17:59,00:05:28
Capture13.mpg,00:21:35,00:01:03
Capture13.mpg,00:22:42,00:32:42
Capture13.mpg,00:55:24,00:02:39
Capture13.mpg,00:58:03,00:21:03
Capture13.mpg,01:19:06,00:00:47
Capture13.mpg,01:19:53,00:01:47
Capture13.mpg,01:21:40,00:03:42
Capture13.mpg,01:25:22,00:03:20
Capture13.mpg,01:28:42,00:03:21
Capture13.mpg,01:32:03,00:00:06
Capture13.mpg,01:32:09,00:06:19
Capture13.mpg,01:38:28,00:00:06
Capture13.mpg,01:38:34,00:00:29
Capture13.mpg,01:39:03,00:02:48
Capture13.mpg,01:41:51,00:04:39
Capture13.mpg,01:46:30,00:02:05
Capture15.mpg,00:00:04,01:29:32
Capture15.mpg,01:29:36,01:19:28
Capture16.mpg,00:00:00,01:09:55
Capture17.mpg,00:00:05,00:03:40
Capture18.mpg,00:00:04,02:58:54
Capture19.mpg,00:00:03,02:25:14
Capture20.mpg,00:00:01,01:15:06
Capture21.mpg,00:00:00,00:00:15
Capture22.mpg,00:00:00,00:00:40
Capture23.mpg,00:00:00,00:00:40
Capture24.mpg,00:00:00,00:00:05
Capture25.mpg,00:00:00,00:00:06
Capture27.mpg,00:00:00,00:00:04
Capture28.mpg,00:00:00,00:00:15
Capture29.mpg,00:00:00,00:00:04
Capture30.mpg,00:00:00,00:01:17
Capture31.mpg,00:00:00,00:00:21
Capture32.mpg,00:00:00,00:01:52
Capture33.mpg,00:00:00,00:43:09
Capture34.mpg,00:00:00,00:17:14
Capture35.mpg,00:00:00,00:02:46
Capture35.mpg,00:02:46,00:00:52
Capture35.mpg,00:03:38,00:00:44
Capture35.mpg,00:04:22,00:08:18
Capture35.mpg,00:12:43,00:03:18
Capture35.mpg,00:16:07,00:03:42
Capture35.mpg,00:19:49,00:04:02
Capture35.mpg,00:23:55,00:13:54
Capture35.mpg,00:37:50,00:01:24
Capture36.mpg,00:00:00,00:38:32
Capture36.mpg,00:38:35,00:08:14
Capture36.mpg,00:46:50,00:58:33
Capture36.mpg,01:44:18,00:02:43
Capture37.mpg,00:00:00,00:02:35
Capture39.mpg,00:00:00,00:05:06
Capture39.mpg,00:05:07,00:00:20
Capture39.mpg,00:05:27,00:50:16
Capture39.mpg,00:55:44,00:12:02
Capture39.mpg,01:07:46,00:04:12
Capture40.mpg,00:00:00,00:01:18
Capture40.mpg,00:01:20,00:14:28
Capture41.mpg,00:00:00,00:08:50
Capture41.mpg,00:08:53,00:03:42
Capture42.mpg,00:00:00,06:20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment