Commit 8033b96c authored by Niels-Oliver Walkowski's avatar Niels-Oliver Walkowski
Browse files

feat(mod) Add frame extractor method

Adds a method to the corpus class that uses ffmpeg in order to extract
frame images from a given movie file in the corpus.
parent a8ee0646
from pathlib import Path
from subprocess import Popen, PIPE, CalledProcessError, run
from cv2 import imread, imwrite
import numpy as np
from .movie import Movie
......@@ -100,6 +101,8 @@ class Corpus(object):
"""
tasks = self._extraction_tasks()
# TODO Extractors implementieren
return tasks
def _extraction_tasks(self):
......@@ -167,10 +170,50 @@ class Corpus(object):
return tasks
def _extract_frames(self):
# TODO Die _extract Methoden gehören eigentlich zur Movie Klasse
def _extract_frames(self, movie='in_the_loop', fps=4, scale=240):
"""Extract frames from movie files
The implementation uses ffmpeg in order to extract frames from a movie
file in a Matroska container.
Keyword Arguments:
movie {str} -- the filename of the movie file without the file
extension.add() (default: {'in_the_loop'})
fps {int} -- how many frames per second are extracted from the
movie file.add() (default: {4})
scale {int} -- the resulting size of the movie measured in vertical
hight (default: {240})
"""
# Create frame images directory
Path(self.basedir / movie / 'frames' / (str(scale) + 'p30')).mkdir(
parents=True, exist_ok=True)
# build shell string for popen
i = '"' + str(self.basedir) + '/' + movie + '/movie/' +\
movie + '.mkv' + '"'
vf = '"scale=iw*sar:ih,scale=-1:' + str(scale) + ',fps=4"'
o = '"' + str(self.basedir) + '/' + movie + '/frames/' +\
str(scale) + 'p30/' + movie + '_' + '%' + '05d.png' + '"'
cmd = 'ffmpeg ' + '-i ' + i + ' -vf ' + vf + ' ' + o
# -vf 'fps=1' = 1 pro Sekunde; `fps=1` 2 pro Sekunde; `fps=1/60` alle
# 60 Sekunden 1 Bild
# %05d Namingpattern für die Ausgabe Dateien
print('Extracting frames from {0}'.format(movie))
# Spawn ffmpeg process
response = run(cmd, shell=True, stdout=PIPE, stderr=PIPE,
encoding='utf-8')
# Evaluate the result
if response.returncode != 0:
print(response.stderr)
else:
print('Finished!')
def _extract_data(self):
pass
def _extract_contrast(self):
def _extract_visuals(self):
pass
def tableau(self, mode='contrast',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment