Skip to content

Instantly share code, notes, and snippets.

@sunoru
Created September 3, 2019 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunoru/1a21d887d98c1a476c402e65a003ced2 to your computer and use it in GitHub Desktop.
Save sunoru/1a21d887d98c1a476c402e65a003ced2 to your computer and use it in GitHub Desktop.
import os
import subprocess
import sys
from tkinter import Tk
from tkinter.filedialog import askopenfilenames
import shutil
VLC_PATH = 'C:/Program Files (x86)/VideoLAN/VLC/vlc.exe'
def input_clip_names():
clip_names = []
print('Clip Names (empty line as the end):')
while True:
tmp = input()
if tmp == '':
return clip_names
clip_names.append(tmp)
def get_time(p):
s = p.find(':')
if s > 0:
return int(p[:s]) * 60 + float(p[s+1:])
return float(p)
def escape(s):
for x in "\\() /":
s = s.replace(x, '\\' + x)
return s
def main():
Tk().withdraw()
input_files = list(askopenfilenames())
input_files.sort()
project_name = input('Project Name: ')
output_dir = os.path.join('.', project_name)
os.makedirs(output_dir, exist_ok=True)
clip_names = input_clip_names()
print('\n'.join('%3d: %s' % (i, x) for (i, x) in enumerate(clip_names)))
results = {i: [] for i in range(len(clip_names))}
for filename in input_files:
subprocess.Popen([VLC_PATH, '--one-instance', 'file:///%s' % filename])
print(filename)
tmp = input('clip_number start_time end_time: ')
if tmp == '':
continue
tmp = tmp.split()
i = int(tmp[0])
start, end = (get_time(x) for x in tmp[1:])
results[i].append((os.path.relpath(filename, output_dir), start, end))
with open(os.path.join(output_dir, 'run.sh'), 'w', encoding='utf-8') as script_file:
script_file.write('#!/bin/bash\n')
for i in results:
output_file = os.path.join(output_dir, clip_names[i] + '.txt')
with open(output_file, 'w', encoding='utf-8') as fo:
fo.write('\n'.join("file '%s'\ninpoint %f\noutpoint %f\n" % x for x in results[i]))
escaped_name = escape(clip_names[i])
script_file.write('ffmpeg -safe 0 -f concat -i ./%s.txt -c copy ./%s.mp4\n' % (escaped_name, escaped_name))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment