|
|
@@ -0,0 +1,86 @@ |
|
|
|
import os |
|
|
|
import sys |
|
|
|
import json |
|
|
|
import ffmpeg |
|
|
|
|
|
|
|
if sys.argv[1] == 'grid' or sys.argv[1] == 'file': |
|
|
|
mode = sys.argv[1] |
|
|
|
|
|
|
|
if (mode == 'grid'): |
|
|
|
cols = sys.argv[2] |
|
|
|
rows = sys.argv[3] |
|
|
|
inputpath = os.path.abspath(sys.argv[4]) |
|
|
|
outputdir = os.path.abspath(sys.argv[5]) |
|
|
|
if (mode == 'file'): |
|
|
|
layoutpath = os.path.abspath(sys.argv[2]) |
|
|
|
inputpath = os.path.abspath(sys.argv[3]) |
|
|
|
outputdir = os.path.abspath(sys.argv[4]) |
|
|
|
else: |
|
|
|
print('Videoshredder splits a video into multiple tiles') |
|
|
|
print('There are two layout input modes available: grid and file') |
|
|
|
print('') |
|
|
|
print('Examples:') |
|
|
|
print('') |
|
|
|
print('./video-shredder.py grid 4 3 path/to/inputfile.mp4 output_dirctory') |
|
|
|
print(' ... splits the video into 4x3 equally distributed tiles') |
|
|
|
print('') |
|
|
|
print('./video-shredder.py file layout.json path/to/inputfile.mp4 output_dirctory') |
|
|
|
print(' ... splits into tiles defined in the layout file') |
|
|
|
print('') |
|
|
|
exit() |
|
|
|
|
|
|
|
try: |
|
|
|
os.stat(outputdir) |
|
|
|
except: |
|
|
|
print('Creating new directory', outputdir) |
|
|
|
os.mkdir(outputdir) |
|
|
|
|
|
|
|
def generateGrid (source, h_tiles, v_tiles): |
|
|
|
probe = ffmpeg.probe(source) |
|
|
|
video = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None) |
|
|
|
width = int(video['width']) |
|
|
|
height = int(video['height']) |
|
|
|
|
|
|
|
layout = [] |
|
|
|
|
|
|
|
tile_width = width/int(h_tiles) |
|
|
|
tile_height = height/int(v_tiles) |
|
|
|
|
|
|
|
for row in range(int(v_tiles)): |
|
|
|
for col in range(int(h_tiles)): |
|
|
|
print('row col', row, col) |
|
|
|
layout.append( |
|
|
|
{ |
|
|
|
"name": str(row+1) + "-" + str(col+1), |
|
|
|
"geomentry": { |
|
|
|
"x": str(tile_width*col).split('.')[0], |
|
|
|
"y": str(tile_height*row).split('.')[0], |
|
|
|
"width": str(tile_width).split('.')[0], |
|
|
|
"height": str(tile_height).split('.')[0] |
|
|
|
} |
|
|
|
}) |
|
|
|
return layout |
|
|
|
|
|
|
|
def processLayout (layout): |
|
|
|
print(layout) |
|
|
|
for item in layout: |
|
|
|
outputpath = os.path.join(outputdir, item["name"] + '.mp4') |
|
|
|
print('processing: ' +item["name"]) |
|
|
|
print(' input path: ' + inputpath) |
|
|
|
print(' output path: ' + outputpath) |
|
|
|
stream = ( |
|
|
|
ffmpeg |
|
|
|
.input(inputpath) |
|
|
|
.crop(**item['geomentry']) |
|
|
|
.output(outputpath) |
|
|
|
.run() |
|
|
|
) |
|
|
|
print('Finished processing all tiles!') |
|
|
|
print('Output directory: ' + str(outputdir)) |
|
|
|
|
|
|
|
if (mode == 'file'): |
|
|
|
with open(layoutpath, "r") as read_file: |
|
|
|
processLayout(json.load(read_file)) |
|
|
|
if (mode == 'grid'): |
|
|
|
layout = generateGrid(inputpath, cols, rows) |
|
|
|
processLayout(layout) |