cont refract and multiprocesing

This commit is contained in:
2022-02-17 08:20:05 +03:00
parent 06e08f452a
commit 2f15ccfbb9
2 changed files with 41 additions and 44 deletions

View File

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