Last active
April 13, 2021 16:33
-
-
Save Hyuto/b614b2ba19f46700ab1f8b71ff93da47 to your computer and use it in GitHub Desktop.
Cut video using python from terminal
This file contains 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
# Created by Hyuto, 2021 | |
# Usage: | |
# - python video-cutter.py VIDEO-PATH END-TIME | |
# - python video-cutter.py VIDEO-PATH -s=START-TIME -e=END-TIME | |
import sys, os, subprocess, logging | |
try: | |
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip | |
except: | |
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'moviepy']) | |
def get_second(s): | |
second = 0 | |
if ':' in s: | |
s = list(reversed(list(map(int, s.split(':'))))) | |
for i in range(len(s)): | |
second += (s[i] * (60 ** i)) | |
else: | |
second = int(s) | |
return second | |
def preprocess(args): | |
name = args.pop(0) | |
_name, ext = os.path.splitext(name) | |
config = { | |
'name' : name, | |
'edited': _name + ' [CROPPED]' + ext, | |
'start-time' : 0, | |
'end-time': None | |
} | |
striped = {x[:2] : x[3:] for x in args if '-' in x} | |
if len(striped) != 0: | |
if '-s' in striped: | |
config['start-time'] = get_second(striped['-s']) | |
if '-e' in striped: | |
config['end-time'] = get_second(striped['-e']) | |
else: | |
config['end-time'] = get_second(args[0]) | |
if config['end-time'] == None: | |
logging.error('Invalid arguments : Must include name and end time') | |
sys.exit(1) | |
return config | |
if __name__ == '__main__': | |
args = sys.argv[1:] | |
if len(args) < 2: | |
logging.error('Invalid arguments : Must include name and end time') | |
sys.exit(1) | |
CONFIG = preprocess(args) | |
ffmpeg_extract_subclip(CONFIG['name'], CONFIG['start-time'], | |
CONFIG['end-time'], targetname = CONFIG['edited']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can pass minutes or hours time format too