diff --git a/README.md b/README.md index f0dde8b..e8ea259 100644 --- a/README.md +++ b/README.md @@ -17,20 +17,30 @@ This is a Python application that can be used to generate video thumbnail for mp You can use pip: ```console -$ pip3 install thumb-gen +~$ pip3 install thumb-gen ``` ## Configurations -This application contains several default values that cannot be changed externally. You can modify **config.py** as follows. - (These may change during the update) - The number of screen images that should be included in the final thumbnail image. - Thumbnail image quality -``` python +``` console +~$ thumb-gen -c +``` + +or + +``` console +~$ thumb-gen --config +``` + +By program default: + +``` ini IMAGES = 12 IMAGE_QUALITY = 100 ``` @@ -38,10 +48,16 @@ IMAGE_QUALITY = 100 ## Usage options ``` text --v, --version show program's version number and exit -h, --help show this help message and exit + +-c, --config change screenshots for thumbnail image and image quality + +-v, --version show program's version number and exit + -f, --file input a single video + -w, --where input a single video from another directory + -d, --dir input videos from a directory ``` @@ -50,18 +66,21 @@ IMAGE_QUALITY = 100 Example ``` console -~$ python main.py -h -~$ python main.py --help +~$ thumb-gen -h +~$ thumb-gen --help + +~$ thumb-gen -c +~$ thumb-gen --config -~$ python main.py -v -~$ python main.py --version +~$ thumb-gen -v +~$ thumb-gen --version -~$ python main.py -f input.mp4 -~$ python main.py --file input.mp4 +~$ thumb-gen -f input.mp4 +~$ thumb-gen --file input.mp4 -~$ python main.py -w "D:/Videos/input.mp4" -~$ python main.py --where "D:/Videos/input.mp4" +~$ thumb-gen -w "D:/Videos/input.mp4" +~$ thumb-gen --where "D:/Videos/input.mp4" -~$ python main.py -d videos -~$ python main.py --dir videos +~$ thumb-gen -d videos +~$ thumb-gen --dir videos ``` diff --git a/README.rst b/README.rst index 1c3f733..e3b3940 100644 --- a/README.rst +++ b/README.rst @@ -2,6 +2,14 @@ Thumbnail Generator =================== +.. image:: https://travis-ci.com/truethari/thumb-gen.svg?branch=master + :target: https://travis-ci.com/truethari/thumb-gen +.. image:: https://app.codacy.com/project/badge/Grade/01b66feeb94743ac80e413e4e9075595 + :target: https://www.codacy.com/gh/truethari/thumb-gen/dashboard?utm_source=github.com&utm_medium=referral&utm_content=truethari/thumb-gen&utm_campaign=Badge_Grade + +.. image:: https://socialify.git.ci/truethari/thumb-gen/image?description=1&descriptionEditable=Python%20application%20that%20can%20be%20used%20to%20generate%20video%20thumbnail%20for%20mp4%20and%20mkv%20file%20types.&font=Inter&language=1&logo=https%3A%2F%2Fen.gravatar.com%2Fuserimage%2F101097900%2F0187b63cf526a88a4c67cab4ab5bfe7f.png&owner=1&pattern=Circuit%20Board&theme=Dark + :target: https://github.com/truethari/thumb-gen + ------------- What is This? ------------- @@ -9,14 +17,26 @@ This is a Python application that can be used to generate video thumbnail for mp Configurations ============== -This application contains several default values that cannot be changed externally. You can modify **config.py** as follows. (These may change during the update) - The number of screen images that should be included in the final thumbnail image. - Thumbnail image quality -.. code-block:: Python +.. code-block:: bash + + ~$ thumb-gen -c + +or + +.. code-block:: bash + + ~$ thumb-gen --config + + +By program default: + +.. code-block:: ini IMAGES = 12 IMAGE_QUALITY = 100 @@ -29,27 +49,36 @@ Usage options: .. code-block:: - -v, --version show program's version number and exit - -h, --help show this help message and exit - -w, --where input a single video from another directory - -f, --file input a single video - -d, --dir input videos from a directory + -h, --help show this help message and exit + + -c, --config change screenshots for thumbnail image and image quality + + -v, --version show program's version number and exit + + -f, --file input a single video + + -w, --where input a single video from another directory + + -d, --dir input videos from a directory Examples ======== .. code-block:: bash - ~$ python main.py -h - ~$ python main.py --help + ~$ thumb-gen -h + ~$ thumb-gen --help + + ~$ thumb-gen -c + ~$ thumb-gen --config - ~$ python main.py -v - ~$ python main.py --version + ~$ thumb-gen -v + ~$ thumb-gen --version - ~$ python main.py -f input.mp4 - ~$ python main.py --file input.mp4 + ~$ thumb-gen -f input.mp4 + ~$ thumb-gen --file input.mp4 - ~$ python main.py -w "D:/Videos/input.mp4" - ~$ python main.py --where "D:/Videos/input.mp4" + ~$ thumb-gen -w "D:/Videos/input.mp4" + ~$ thumb-gen --where "D:/Videos/input.mp4" - ~$ python main.py -d videos - ~$ python main.py --dir videos \ No newline at end of file + ~$ thumb-gen -d videos + ~$ thumb-gen --dir videos diff --git a/setup.py b/setup.py index 3af71f5..a19986b 100644 --- a/setup.py +++ b/setup.py @@ -16,12 +16,13 @@ author="tharindu.dev", author_email="tharindu.nm@yahoo.com", url="https://github.com/truethari/thumb-gen", + keywords="thumbnails video screenshot", + license='MIT', project_urls={ "Bug Tracker": "https://github.com/truethari/thumb-gen/issues", }, classifiers=[ "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.6", ], packages=['thumb_gen'], include_package_data=True, diff --git a/thumb_gen/__init__.py b/thumb_gen/__init__.py index 931fe7e..b146736 100644 --- a/thumb_gen/__init__.py +++ b/thumb_gen/__init__.py @@ -2,6 +2,6 @@ from .application import screenshots, resize, thumb from .options import parseOpts, begin from .worker import Generator -from .viewer import deco, args_error, helps -from .config import * +from .viewer import deco, args_error, helps, configurations +from .config import get_datadir, create_config, modify_config, read_config from .version import * \ No newline at end of file diff --git a/thumb_gen/application.py b/thumb_gen/application.py index 356dfad..1548e44 100644 --- a/thumb_gen/application.py +++ b/thumb_gen/application.py @@ -4,7 +4,7 @@ from videoprops import get_video_properties from PIL import Image -from .config import * +from .config import read_config def screenshots(video_path, screenshot_folder): for img in os.listdir(screenshot_folder): @@ -14,7 +14,7 @@ def screenshots(video_path, screenshot_folder): video_properties = get_video_properties(video_path) video_duration = int(round(float(video_properties['duration']), 2)) - frame_time = round((video_duration / IMAGES), 2) + frame_time = round((video_duration / read_config('images')), 2) screenshot_path = screenshot_folder + tm_video_path[-1] event = FFmpeg(inputs={video_path: None}, \ @@ -45,7 +45,7 @@ def thumb(video_path, resize_folder, secure_temp): r_new_width, new_height = image.size break - img_rows = IMAGES / 3 + img_rows = read_config('images') / 3 tmp_var = str(img_rows).split('.') if tmp_var[1] != '0': img_rows = int(img_rows) + 1 @@ -75,7 +75,7 @@ def thumb(video_path, resize_folder, secure_temp): back_im.paste(img_list[count - 1], (x, y)) x = x + r_new_width + 5 - back_im.save(video_path[:-4] + '.png', quality=IMAGE_QUALITY) + back_im.save(video_path[:-4] + '.png', quality=read_config('image_quality')) print('The thumbnail was saved in - {}\n'.format(video_path[:-4] + '.png')) return True diff --git a/thumb_gen/config.py b/thumb_gen/config.py index b918696..1bbfae9 100644 --- a/thumb_gen/config.py +++ b/thumb_gen/config.py @@ -1,2 +1,82 @@ -IMAGES = 12 -IMAGE_QUALITY = 100 \ No newline at end of file +import os +import sys +import pathlib +import configparser + +def get_datadir() -> pathlib.Path: + + """ + Returns a parent directory path + where persistent application data can be stored. + + # linux: ~/.local/share + # macOS: ~/Library/Application Support + # windows: C:/Users//AppData/Roaming + """ + + home = pathlib.Path.home() + + if sys.platform == "win32": + return home / "AppData/Roaming" + elif sys.platform == "linux": + return home / ".local/share" + elif sys.platform == "darwin": + return home / "Library/Application Support" + +def create_config(IMAGES=12, IMAGE_QUALITY=80): + my_datadir = get_datadir() / "thumb-gen" + + try: + my_datadir.mkdir(parents=True) + + except FileExistsError: + pass + + finally: + configfile_path = str(my_datadir) + "/config.ini" + config_object = configparser.ConfigParser() + config_object["DEFAULT"] = {"images": IMAGES, "image_quality": IMAGE_QUALITY} + with open(configfile_path, 'w') as conf: + config_object.write(conf) + + return True + +def modify_config(options, value_1, value_2=0): + my_datadir = get_datadir() / "thumb-gen" + configfile_path = str(my_datadir) + "/config.ini" + config_object = configparser.ConfigParser() + + config_object.read(configfile_path) + userinfo = config_object["DEFAULT"] + + if options == "images_image_quality": + userinfo["images"] = str(value_1) + userinfo["image_quality"] = str(value_2) + elif options == "images": + userinfo["images"] = str(value_1) + elif options == "image_quality": + userinfo["image_quality"] = str(value_1) + + with open(configfile_path, 'w') as conf: + config_object.write(conf) + + return True + +def read_config(option): + my_datadir = get_datadir() / "thumb-gen" + configfile_path = str(my_datadir) + "/config.ini" + config_object = configparser.ConfigParser() + + if os.path.isfile(configfile_path): + config_object.read(configfile_path) + default = config_object["DEFAULT"] + else: + create_config_return = create_config() + if create_config_return: + config_object.read(configfile_path) + default = config_object["DEFAULT"] + + if option == 'images': + return int(default['images']) + elif option == 'image_quality': + return int(default['image_quality']) diff --git a/thumb_gen/options.py b/thumb_gen/options.py index b7fa6d7..821a7ed 100644 --- a/thumb_gen/options.py +++ b/thumb_gen/options.py @@ -2,8 +2,10 @@ import sys import getopt -from .viewer import args_error, helps, deco +from .viewer import args_error, helps, deco from .version import __version__ +from .viewer import configurations +from .config import modify_config videos = [] @@ -11,8 +13,10 @@ def parseOpts(argument_list): deco() input_dir = '' input_file = '' + try: - opts, args = getopt.getopt(argument_list,"vhf:d:w:",["version","help","where=","file=","dir="]) + opts, args = getopt.getopt(argument_list,"cvhf:d:w:",["config","version","help","file=","dir=","where="]) + except getopt.GetoptError: args_error(argument_list) sys.exit() @@ -22,7 +26,7 @@ def parseOpts(argument_list): helps() sys.exit() - elif opt == "-f" or opt == "--file" or opt == "-w" or opt == "--where": + elif opt in("-f", "--file", "-w", "--where"): if arg == '': args_error() sys.exit() @@ -31,7 +35,7 @@ def parseOpts(argument_list): return input_dir, input_file, opt break - elif opt == "-d" or opt == "--dir": + elif opt in ("-d", "--dir"): if arg == '': args_error() sys.exit() @@ -43,11 +47,26 @@ def parseOpts(argument_list): elif opt in ("-v", "--version"): print(__version__) sys.exit() + + elif opt in ("-c", "--config"): + conf_images, conf_image_quality = configurations() + + if conf_images != 0 and conf_image_quality != 0: + modify_config('images_image_quality', conf_images, conf_image_quality) + + elif conf_images != 0: + modify_config('images', conf_images) + + elif conf_image_quality != 0: + modify_config('image_quality', conf_image_quality) + + sys.exit() def begin(input_dir = '', input_file = '', opt = ''): if opt == '-d' and input_dir == '': current_folder = os.getcwd() + '/' current_folder = current_folder.replace("\\", "/") + for video in os.listdir(current_folder): if video.endswith('.mp4') or video.endswith('.mkv'): videos.append(current_folder + video) diff --git a/thumb_gen/run.py b/thumb_gen/run.py index c8e14d6..cde0935 100644 --- a/thumb_gen/run.py +++ b/thumb_gen/run.py @@ -27,7 +27,6 @@ def run(argument_list = False): for video_path in videos: app = Generator(video_path, secure_temp, screenshot_folder, resize_folder) app.run() - except: - print("Error: {}".format(sys.exc_info()[0])) + finally: shutil.rmtree(temp_dir.name) diff --git a/thumb_gen/version.py b/thumb_gen/version.py index 2ece630..5d4fa49 100644 --- a/thumb_gen/version.py +++ b/thumb_gen/version.py @@ -1 +1 @@ -__version__ = "0.0.dev1" \ No newline at end of file +__version__ = "0.0.dev2" \ No newline at end of file diff --git a/thumb_gen/viewer.py b/thumb_gen/viewer.py index de87e02..c25c1ff 100644 --- a/thumb_gen/viewer.py +++ b/thumb_gen/viewer.py @@ -1,3 +1,7 @@ +import sys + +from .config import read_config + def deco(): print(""" _______ _ _ _____ @@ -17,11 +21,50 @@ def args_error(argument_list = False): def helps(): print (""" - -v, --version show program's version number and exit -h, --help show this help message and exit + -c, --config change screenshots for thumbnail image and image quality + -v, --version show program's version number and exit Options: -f, --file input a single video from current directory -w, --where input a single video from another directory -d, --dir input videos from a directory """) + +def configurations(): + print("Configurations\n") + print("Images = {}\nThumbnail Quality = {}\n".format(read_config('images'), read_config('image_quality'))) + + loop = True + while loop: + try: + loop2 = True + while loop2: + print("If you do not want to change the values, leave the input blank and press Enter.\n") + print("CTRL + C to exit.") + try: + images = int(input("Images = ") or 0) + loop2 = False + except ValueError: + print("Invalid input! Please enter a valid number.") + loop3 = True + while loop3: + try: + loop4 = True + while loop4: + image_quality = int(input("Thumbnail Quality (10 - 100) = ") or 0) + if 101 > image_quality > 9 or image_quality == 0: + loop4 = False + loop3 = False + else: + print("Enter number between 10 - 100!") + except ValueError: + print("Invalid input! Please enter a valid number.") + + except KeyboardInterrupt: + loop = False + sys.exit() + + loop = False + + return images, image_quality \ No newline at end of file