Directory support added
This commit is contained in:
10
README.md
10
README.md
@@ -48,6 +48,7 @@ Options:
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
**Single file**
|
||||||
```shell
|
```shell
|
||||||
$ ./generator videos/27467_1_milkbots_wg_720p.mp4 2 126 73 10 thumbnails.jpg
|
$ ./generator videos/27467_1_milkbots_wg_720p.mp4 2 126 73 10 thumbnails.jpg
|
||||||
Extracting 5 frames
|
Extracting 5 frames
|
||||||
@@ -56,6 +57,15 @@ Frames extracted.
|
|||||||
Saved!
|
Saved!
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Directory**
|
||||||
|
```shell
|
||||||
|
$ ./generator videos/ 2 126 73 10 thumbnails/
|
||||||
|
Extracting 5 frames
|
||||||
|
[####################################] 100%
|
||||||
|
Frames extracted.
|
||||||
|
Saved!
|
||||||
|
```
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
|
|||||||
45
generator
45
generator
@@ -22,28 +22,63 @@ from docopt import docopt
|
|||||||
from moviepy.editor import VideoFileClip
|
from moviepy.editor import VideoFileClip
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from click import progressbar
|
from click import progressbar
|
||||||
|
from collections import namedtuple
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import shutil
|
import shutil
|
||||||
import math
|
import math
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
TMP_FRAMES_PATH = tempfile.mkdtemp()
|
TMP_FRAMES_PATH = tempfile.mkdtemp()
|
||||||
|
|
||||||
|
|
||||||
def generate_video_thumbnails(args):
|
def generate_video_thumbnails(args):
|
||||||
filepath = args['<video>']
|
input_path = args['<video>']
|
||||||
interval = int(args['<interval>'])
|
interval = int(args['<interval>'])
|
||||||
size = (int(args['<width>']), int(args['<height>']))
|
size = (int(args['<width>']), int(args['<height>']))
|
||||||
output_prefix = get_output_prefix()
|
output_prefix = get_output_prefix()
|
||||||
columns = int(args['<columns>'])
|
columns = int(args['<columns>'])
|
||||||
output = args['<output>']
|
output_path = args['<output>']
|
||||||
|
|
||||||
video_file_clip = VideoFileClip(filepath)
|
file_paths = set()
|
||||||
generate_frames(video_file_clip, interval, output_prefix, size)
|
|
||||||
generate_sprite_from_frames(output_prefix, columns, size, output)
|
if os.path.isdir(input_path):
|
||||||
|
# Ensure output path is also directory
|
||||||
|
if not os.path.isdir(output_path):
|
||||||
|
print(
|
||||||
|
"If input path is directory then "
|
||||||
|
"output path must be directory"
|
||||||
|
)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Strip seperator so contructing output is uniform
|
||||||
|
output_path = output_path.strip(os.sep)
|
||||||
|
|
||||||
|
# Add all files in directory for processing
|
||||||
|
for file_name in os.listdir(input_path):
|
||||||
|
file_path = os.path.join(input_path, file_name)
|
||||||
|
if os.path.isfile(file_path):
|
||||||
|
# Construct output path for thumbnail using
|
||||||
|
# the video files filename
|
||||||
|
single_output_path = \
|
||||||
|
"{output_path}{seperator}{file_name}.png". \
|
||||||
|
format(
|
||||||
|
output_path=output_path,
|
||||||
|
seperator=os.sep,
|
||||||
|
file_name=os.path.basename(file_path)
|
||||||
|
)
|
||||||
|
file_paths.add((file_path, single_output_path,))
|
||||||
|
else:
|
||||||
|
file_paths.add((input_path, output_path,))
|
||||||
|
|
||||||
|
# Process all files sequentially
|
||||||
|
for file_path in file_paths:
|
||||||
|
video_file_clip = VideoFileClip(file_path[0])
|
||||||
|
generate_frames(video_file_clip, interval, output_prefix, size)
|
||||||
|
generate_sprite_from_frames(output_prefix, columns, size, file_path[1])
|
||||||
|
|
||||||
|
|
||||||
def generate_frames(video_file_clip, interval, output_prefix, size):
|
def generate_frames(video_file_clip, interval, output_prefix, size):
|
||||||
|
|||||||
Reference in New Issue
Block a user