Browse Source

shredder it

master
Andreas Demmelbauer 3 years ago
commit
467f414ace
4 changed files with 175 additions and 0 deletions
  1. +32
    -0
      README.md
  2. +56
    -0
      layout.json
  3. +1
    -0
      requirements.txt
  4. +86
    -0
      video-shredder.py

+ 32
- 0
README.md View File

@@ -0,0 +1,32 @@
# Python script for splitting videos into tiles with ffmpeg

It supports simple grid definitions and advanced layout definitions from a json layout definition file.

## Installation

### Install git, ffmpeg and python3 with pip on your system
For Debian:
```
sudo apt install git ffmpeg python3 python3-pip
```

### Clone this git repository
```
git clone https://git.notice.at/redplanet/video-shredder.git
```

### Enter directory and install python requirements
```
pip3 install -r requirements.txt
```

## Usage
grid mode:
```
python3 videoshredder.py grid <columns> <rows> <input_file> <output_path>
```

file mode:
```
python3 videoshredder.py file <layout_path> <input_file> <output_path>
```

+ 56
- 0
layout.json View File

@@ -0,0 +1,56 @@
[
{
"name": "1-1",
"geomentry": {
"x": "0",
"y": "0",
"width": "1280/3",
"height": "720/2"
}
},
{
"name": "1-2",
"geomentry": {
"x": "1280/3",
"y": "0",
"width": "1280/3",
"height": "720/2"
}
},
{
"name": "1-3",
"geomentry": {
"x": "1280/3*2",
"y": "0",
"width": "1280/3",
"height": "720/2"
}
},
{
"name": "2-1",
"geomentry": {
"x": "0",
"y": "720/2",
"width": "1280/3",
"height": "720/2"
}
},
{
"name": "2-2",
"geomentry": {
"x": "1280/3",
"y": "720/2",
"width": "1280/3",
"height": "720/2"
}
},
{
"name": "2-3",
"geomentry": {
"x": "1280/3*2",
"y": "720/2",
"width": "1280/3",
"height": "720/2"
}
}
]

+ 1
- 0
requirements.txt View File

@@ -0,0 +1 @@
ffmpeg-python

+ 86
- 0
video-shredder.py View File

@@ -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)

Loading…
Cancel
Save