This commit is contained in:
2022-02-14 21:49:37 +03:00
commit e7ba83f0e1
5 changed files with 147 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" value="Default" />
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/prepare.iml" filepath="$PROJECT_DIR$/.idea/prepare.iml" />
</modules>
</component>
</project>

105
prepare.py Normal file
View File

@@ -0,0 +1,105 @@
#!/usr/bin/python3
import argparse
import random
from argparse import ArgumentParser
from os import makedirs, path
from pathlib import Path
from moviepy.editor import *
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from pymediainfo import MediaInfo
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 screenshos")
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 screenschots, default 10", type=int)
parser.add_argument('-i', action='store', dest='input', help="input file", required=True)
args = parser.parse_args()
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")
# print(media_info)
f = open(mfile, "w")
f.write(str(media_info))
f.close()
def screens(fold, max_time):
print("Creating %s screenschots" % Args.args.count)
i = 0
max_time = round(max_time)
print("Rounded duration %ss" % max_time)
screen_create = VideoFileClip(Args.args.input)
mintimescr = round(max_time * 0.05)
maxtimescr = round(max_time * 0.95)
while i < Args.args.count:
i += 1
time = random.randint(mintimescr, maxtimescr)
imgname = fold + "/" + str(time) + ".png"
print("%s at time %ds" % (i, time))
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 = path.basename(Args.args.input)
fname = path.splitext(bname)[0]
exname = path.splitext(bname)[1]
print("Filename is %s" % bname)
fold = str(Path.home()) + "/samples/" + fname
if path.isdir(fold):
print("Directory %s is exist" % fold)
else:
try:
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 = 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
if Args.args.sample:
sample(fold, exname, max_time)
if Args.args.screens:
screens(fold, max_time)
if Args.args.mediainfo:
info(mfile, media_info)
if __name__ == '__main__':
Head()

3
requirements.txt Normal file
View File

@@ -0,0 +1,3 @@
setuptools
moviepy
pymediainfo

24
setup.py Normal file
View File

@@ -0,0 +1,24 @@
from setuptools import setup
try:
import multiprocessing
except:
pass
install_requires = [
'setuptools',
'moviepy',
'pymediainfo',
]
setup(
name='prepare',
version='0.1.1',
url='',
zip_safe=True,
install_requires=install_requires,
include_package_data=True,
author='beaconborn',
author_email='rik.slava@mail.com',
description='Creating screens, Media Info and sample from script'
)