cont refract and multiprocesing
This commit is contained in:
118
prepare.py
118
prepare.py
@@ -1,12 +1,18 @@
|
||||
#!/usr/bin/python3
|
||||
import argparse
|
||||
import random
|
||||
import os
|
||||
import pathlib
|
||||
import moviepy.editor
|
||||
import threading
|
||||
import time
|
||||
import logging
|
||||
import argparse
|
||||
|
||||
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
|
||||
from pymediainfo import MediaInfo
|
||||
import threading
|
||||
|
||||
logging.basicConfig(filename='prepare.log', filemode='w', format='%(asctime)s \t %(threadName)s \t %(levelname)s \t '
|
||||
'%(message)s', level=logging.INFO)
|
||||
|
||||
|
||||
class Args:
|
||||
@@ -19,39 +25,7 @@ class Args:
|
||||
help="number of screenshots, default 10", type=int)
|
||||
parser.add_argument('-i', action='store', dest='input', help="input file", required=True)
|
||||
args = parser.parse_args()
|
||||
args.input = "D:/ReLive/Escape from Tarkov/2021.02.18-22.15.mp4"
|
||||
args.screens = True
|
||||
|
||||
|
||||
def sample(fold, exname, max_time):
|
||||
"""
|
||||
Create sample with duration 2m if file longer then 6m
|
||||
"""
|
||||
|
||||
print("sample")
|
||||
sam = fold + "/sample/" + exname
|
||||
if max_time <= 360:
|
||||
ffmpeg_extract_subclip(Args.args.input, max_time / 3, max_time * 2 / 3, targetname=sam)
|
||||
else:
|
||||
ffmpeg_extract_subclip(Args.args.input, max_time / 2 - 60, max_time / 2 + 60, targetname=sam)
|
||||
|
||||
|
||||
def info(mfile, media_info):
|
||||
print("Creating MediaInfo")
|
||||
f = open(mfile, "w")
|
||||
f.write(str(media_info))
|
||||
f.close()
|
||||
print("Created MediaInfo")
|
||||
|
||||
|
||||
def screens(fold, mintimescr, maxtimescr, screen_create, num_screen):
|
||||
time = random.randint(mintimescr, maxtimescr)
|
||||
imgname = fold + "/" + str(time) + ".png"
|
||||
print(f"{num_screen + 1} time {time}s")
|
||||
|
||||
screen_create.save_frame(imgname, t=time)
|
||||
screen_create.reader.close()
|
||||
screen_create.audio.reader.close_proc()
|
||||
logging.info(' '.join(f'{k}={v}' for k, v in vars(args).items()))
|
||||
|
||||
|
||||
class Head:
|
||||
@@ -59,25 +33,26 @@ class Head:
|
||||
Creates mediainfo, screenshots and sample from file
|
||||
"""
|
||||
|
||||
def body():
|
||||
def __init__(self):
|
||||
procs = []
|
||||
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]
|
||||
print("Filename is %s" % bname)
|
||||
fold = str(pathlib.Path.home()) + "/samples/" + fname
|
||||
logging.info("Filename is %s" % bname)
|
||||
|
||||
if os.path.isdir(fold):
|
||||
print("Directory %s is exist" % fold)
|
||||
logging.info("Directory %s is exist" % fold)
|
||||
else:
|
||||
try:
|
||||
moviepy.editor.os.makedirs(fold)
|
||||
except OSError:
|
||||
print("Creation of the directory %s failed" % fold)
|
||||
logging.error("Creation of the directory %s failed" % fold)
|
||||
else:
|
||||
print("Successfully created the directory %s " % fold)
|
||||
print("\nFiles will be located at %s" % fold)
|
||||
logging.info("Successfully created the directory %s " % fold)
|
||||
logging.info("Files will be located at %s" % fold)
|
||||
|
||||
media_info = MediaInfo.parse(Args.args.input, output="", parse_speed=2)
|
||||
mi = MediaInfo.parse(Args.args.input)
|
||||
@@ -86,22 +61,65 @@ class Head:
|
||||
max_time = duration.duration / 1000
|
||||
|
||||
if Args.args.sample:
|
||||
sample(fold, exname, max_time)
|
||||
thread = threading.Thread(name="sample",
|
||||
target=Head.sample, args=(fold, exname, max_time,))
|
||||
procs.append(thread)
|
||||
thread.start()
|
||||
time.sleep(1)
|
||||
|
||||
if Args.args.screens:
|
||||
print("Creating %s screenshots" % Args.args.count)
|
||||
max_time = round(max_time)
|
||||
print("Rounded duration %ss" % max_time)
|
||||
screen_create = moviepy.editor.VideoFileClip(Args.args.input)
|
||||
logging.info("Rounded duration %ss" % 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):
|
||||
num_screen = i
|
||||
thread = threading.Thread(name="screenshot",
|
||||
target=screens(fold, mintimescr, maxtimescr, screen_create, num_screen))
|
||||
thread = threading.Thread(name="screens",
|
||||
target=Head.screens, args=(fold, mintimescr, maxtimescr, i,))
|
||||
procs.append(thread)
|
||||
thread.start()
|
||||
time.sleep(1)
|
||||
|
||||
if Args.args.mediainfo:
|
||||
info(mfile, media_info)
|
||||
thread = threading.Thread(name="mediainfo",
|
||||
target=Head.info, args=(mfile, media_info,))
|
||||
procs.append(thread)
|
||||
thread.start()
|
||||
time.sleep(1)
|
||||
|
||||
for proc in procs:
|
||||
proc.join()
|
||||
|
||||
def sample(fold, exname, max_time):
|
||||
"""
|
||||
Create sample with duration 2m if file longer then 6m
|
||||
"""
|
||||
|
||||
sam = fold + "/sample" + exname
|
||||
|
||||
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)
|
||||
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)
|
||||
|
||||
def info(file, media_info):
|
||||
logging.info(f"Creating MediaInfo at {file}")
|
||||
f = open(file, "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)
|
||||
screen_create.save_frame(imgname, t=sec)
|
||||
screen_create.audio.reader.close_proc()
|
||||
screen_create.reader.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
Head.body()
|
||||
Head()
|
||||
|
||||
Reference in New Issue
Block a user