#!/usr/bin/python3 import argparse import random import os import pathlib import moviepy.editor import moviepy import pymediainfo import threading class Args: parser = ArgumentParser = argparse.ArgumentParser( description='Extracting mediainfo, sample and screens from random timecodes from video') parser.add_argument('-s', action='store_true', dest='screens', help="create screenshots") parser.add_argument('-c', action='store_true', dest='sample', help="extract sample") parser.add_argument('-m', action='store_true', dest='mediainfo', help='create mediainfo') parser.add_argument('-n', action='store', dest="count", default=10, 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" 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: moviepy.video.io.ffmpeg_tools.ffmpeg_extract_subclip(Args.args.input, max_time / 3, max_time * 2 / 3, targetname=sam) else: moviepy.video.io.ffmpeg_tools.ffmpeg_extract_subclip(Args.args.input, max_time / 2 - 60, max_time / 2 + 60, targetname=sam) def info(mfile, media_info): print("Creating MediaInfo") # print(media_info) f = open(mfile, "w") f.write(str(media_info)) f.close() def screens(fold, mintimescr, maxtimescr, screen_create): time = random.randint(mintimescr, maxtimescr) imgname = fold + "/" + str(time) + ".png" print(" time %ds" % time) make_screen = screen_create.save_frame(imgname, t=time) screen_create.reader.close() screen_create.audio.reader.close_proc() class Head: """ Creates mediainfo, screenshots and sample from file """ def __init__(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] print("Filename is %s" % bname) fold = str(pathlib.Path.home()) + "/samples/" + fname if moviepy.editor.os.path.isdir(fold): print("Directory %s is exist" % fold) else: try: moviepy.editor.os.makedirs(fold) except OSError: print("Creation of the directory %s failed" % fold) else: print("Successfully created the directory %s " % fold) print("\nFiles will be located at %s" % fold) media_info = pymediainfo.MediaInfo.parse(Args.args.input, output="", parse_speed=2) mi = pymediainfo.MediaInfo.parse(Args.args.input) mfile = fold + "/mediainfo.txt" duration = mi.tracks[0] max_time = duration.duration / 1000 if Args.args.sample: sample(fold, exname, max_time) if Args.args.screens: print("Creating %s screenschots" % Args.args.count) i = 0 max_time = round(max_time) print("Rounded duration %ss" % max_time) screen_create = moviepy.editor.VideoFileClip(Args.args.input) mintimescr = round(max_time * 0.05) maxtimescr = round(max_time * 0.95) return mintimescr, maxtimescr, fold, screen_create # screens(fold, mintimescr, maxtimescr, screen_create) if Args.args.mediainfo: info(mfile, media_info) if __name__ == '__main__': Head() if Args.args.screens: for i in range (Args.args.count): condition = threading.Condition() thread = threading.Thread(name="screenshot", target=screens, args=(Head.mintimescr, Head.maxtimescr, Head.fold, Head.screen_create)) thread.start()