Last active
November 28, 2023 15:46
-
-
Save workingenius/ab4d9a858135dc3eecdd835d616ae655 to your computer and use it in GitHub Desktop.
clip vedio clip file in terms of time points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf8 -*- | |
import re | |
import click | |
@click.command() | |
@click.argument('movie') | |
@click.option('-S', '--split-file', 'split_file') | |
@click.option('-0', '--start-num', 'start_num', default=0, type=click.INT) | |
@click.option('-p', '--prefix', 'prefix', default='a') | |
def mclip(movie, split_file, prefix, start_num): | |
u"""clicp vedio clip file in terms of time points in a file""" | |
from moviepy.editor import VideoFileClip | |
def verify_time_point(tp): | |
ptn = re.compile(r'^\d{2}:\d{2}:\d{2}.\d{2}$') | |
return isinstance(tp, basestring) and ptn.match(tp) | |
with open(split_file, 'r') as split_file: | |
time_points = map(lambda x: x[:-1], list(split_file)) # remove ending newline char | |
assert all(map(verify_time_point, time_points)), 'invalid split file format' | |
def do_clip(span, num=None): | |
c = VideoFileClip(movie).subclip(span[0], span[1]) | |
c_name = prefix + str(num) + '.mp4' | |
c.write_videofile(c_name) | |
for i, sp in enumerate(zip(time_points[:-1], time_points[1:])): | |
do_clip(sp, num=i + start_num) | |
click.echo('clip done') | |
if __name__ == '__main__': | |
mclip() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment