cont refract and multiprocesing
This commit is contained in:
84
prepare.py
84
prepare.py
@@ -2,7 +2,7 @@
|
|||||||
import random
|
import random
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import moviepy.editor
|
from moviepy import editor
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
@@ -35,54 +35,49 @@ class Head:
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
procs = []
|
procs = []
|
||||||
bname: str
|
self.bname: str
|
||||||
|
|
||||||
bname = moviepy.editor.os.path.basename(Args.args.input)
|
self.bname = os.path.basename(Args.args.input)
|
||||||
fname = moviepy.editor.os.path.splitext(bname)[0]
|
self.fold = str(pathlib.Path.home()) + "/samples/" + os.path.splitext(self.bname)[0]
|
||||||
exname = moviepy.editor.os.path.splitext(bname)[1]
|
logging.info("Filename is %s" % self.bname)
|
||||||
fold = str(pathlib.Path.home()) + "/samples/" + fname
|
|
||||||
logging.info("Filename is %s" % bname)
|
|
||||||
|
|
||||||
if os.path.isdir(fold):
|
if os.path.isdir(self.fold):
|
||||||
logging.info("Directory %s is exist" % fold)
|
logging.info("Directory %s is exist" % self.fold)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
moviepy.editor.os.makedirs(fold)
|
os.makedirs(self.fold)
|
||||||
except OSError:
|
except OSError:
|
||||||
logging.error("Creation of the directory %s failed" % fold)
|
logging.error("Creation of the directory %s failed" % self.fold)
|
||||||
else:
|
else:
|
||||||
logging.info("Successfully created the directory %s " % fold)
|
logging.info("Successfully created the directory %s " % self.fold)
|
||||||
logging.info("Files will be located at %s" % fold)
|
logging.info("Files will be located at %s" % self.fold)
|
||||||
|
|
||||||
media_info = MediaInfo.parse(Args.args.input, output="", parse_speed=2)
|
self.max_time = round(MediaInfo.parse(Args.args.input).tracks[0].duration / 1000)
|
||||||
mi = MediaInfo.parse(Args.args.input)
|
|
||||||
mfile = fold + "/mediainfo.txt"
|
|
||||||
duration = mi.tracks[0]
|
|
||||||
max_time = duration.duration / 1000
|
|
||||||
|
|
||||||
if Args.args.sample:
|
if Args.args.sample:
|
||||||
thread = threading.Thread(name="sample",
|
thread = threading.Thread(name="sample", target=Head.sample, args=(self, ),
|
||||||
target=Head.sample, args=(fold, exname, max_time,))
|
kwargs={'fold': self.fold,
|
||||||
|
'bname': self.bname,
|
||||||
|
'max_time': self.max_time})
|
||||||
procs.append(thread)
|
procs.append(thread)
|
||||||
thread.start()
|
thread.start()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
if Args.args.screens:
|
if Args.args.screens:
|
||||||
max_time = round(max_time)
|
logging.info("Rounded duration %ss" % self.max_time)
|
||||||
logging.info("Rounded duration %ss" % max_time)
|
|
||||||
logging.info("Creating %s screenshots" % Args.args.count)
|
logging.info("Creating %s screenshots" % Args.args.count)
|
||||||
mintimescr = round(max_time * 0.05)
|
for self.num_screen in range(Args.args.count):
|
||||||
maxtimescr = round(max_time * 0.95)
|
thread = threading.Thread(name="screens", target=Head.screens, args=(self, ),
|
||||||
for i in range(Args.args.count):
|
kwargs={'fold': self.fold,
|
||||||
thread = threading.Thread(name="screens",
|
'max_time': self.max_time,
|
||||||
target=Head.screens, args=(fold, mintimescr, maxtimescr, i,))
|
'num_screen': self.num_screen})
|
||||||
procs.append(thread)
|
procs.append(thread)
|
||||||
thread.start()
|
thread.start()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
if Args.args.mediainfo:
|
if Args.args.mediainfo:
|
||||||
thread = threading.Thread(name="mediainfo",
|
thread = threading.Thread(name="mediainfo", target=Head.info(self, ),
|
||||||
target=Head.info, args=(mfile, media_info,))
|
kwargs={'fold': self.fold})
|
||||||
procs.append(thread)
|
procs.append(thread)
|
||||||
thread.start()
|
thread.start()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@@ -90,32 +85,33 @@ class Head:
|
|||||||
for proc in procs:
|
for proc in procs:
|
||||||
proc.join()
|
proc.join()
|
||||||
|
|
||||||
def sample(fold, exname, max_time):
|
def sample(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
Create sample with duration 2m if file longer then 6m
|
Create sample with duration 2m if file longer then 6m
|
||||||
"""
|
"""
|
||||||
|
|
||||||
sam = fold + "/sample" + exname
|
sam = self.fold + "/sample" + os.path.splitext(self.bname)[1]
|
||||||
|
|
||||||
if max_time <= 360:
|
if self.max_time <= 360:
|
||||||
logging.info(f"Creating Sample with duration {(max_time * 2 / 3) - (max_time / 3)}s")
|
logging.info(f"Creating Sample with duration {(self.max_time * 2 / 3) - (self.max_time / 3)}s")
|
||||||
ffmpeg_extract_subclip(Args.args.input, max_time / 3, max_time * 2 / 3, targetname=sam)
|
ffmpeg_extract_subclip(Args.args.input, self.max_time / 3, self.max_time * 2 / 3, targetname=sam)
|
||||||
else:
|
else:
|
||||||
logging.info(f"Creating Sample with duration {(max_time / 2 + 60) - (max_time / 2 - 60)}s")
|
logging.info(f"Creating Sample with duration {(self.max_time / 2 + 60) - (self.max_time / 2 - 60)}s")
|
||||||
ffmpeg_extract_subclip(Args.args.input, max_time / 2 - 60, max_time / 2 + 60, targetname=sam)
|
ffmpeg_extract_subclip(Args.args.input, self.max_time / 2 - 60, self.max_time / 2 + 60, targetname=sam)
|
||||||
|
|
||||||
def info(file, media_info):
|
def info(self, **kwargs):
|
||||||
logging.info(f"Creating MediaInfo at {file}")
|
media_info = MediaInfo.parse(Args.args.input, output="")
|
||||||
f = open(file, "w")
|
logging.info(f"Creating MediaInfo at {self.fold + '/mediainfo.txt'}")
|
||||||
|
f = open(self.fold + "/mediainfo.txt", 'w')
|
||||||
f.write(str(media_info))
|
f.write(str(media_info))
|
||||||
f.close()
|
f.close()
|
||||||
logging.info("Created MediaInfo")
|
logging.info("Created MediaInfo")
|
||||||
|
|
||||||
def screens(fold, mintimescr, maxtimescr, num_screen):
|
def screens(self, **kwargs):
|
||||||
sec = random.randint(mintimescr, maxtimescr)
|
sec = random.randint(round(self.max_time * 0.05), round(self.max_time * 0.95))
|
||||||
imgname = fold + "/" + str(sec) + ".png"
|
imgname = self.fold + "/" + str(sec) + ".png"
|
||||||
logging.info(f"{num_screen + 1}st screen time {sec}s\tthread is {threading.get_ident()}")
|
logging.info(f"{self.num_screen + 1}st screen time {sec}s\tthread is {threading.get_ident()}")
|
||||||
screen_create = moviepy.editor.VideoFileClip(Args.args.input)
|
screen_create = editor.VideoFileClip(Args.args.input)
|
||||||
screen_create.save_frame(imgname, t=sec)
|
screen_create.save_frame(imgname, t=sec)
|
||||||
screen_create.audio.reader.close_proc()
|
screen_create.audio.reader.close_proc()
|
||||||
screen_create.reader.close()
|
screen_create.reader.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user