Skip to content

Commit

Permalink
--config added
Browse files Browse the repository at this point in the history
IMAGES =
IMAGE_QUALITY =
  • Loading branch information
truethari committed Mar 18, 2021
1 parent 478a536 commit 61186e2
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 49 deletions.
49 changes: 34 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,47 @@ 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
```

## 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
```

Expand All @@ -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
```
63 changes: 46 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,41 @@
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?
-------------
This is a Python application that can be used to generate video thumbnail for mp4 and mkv file types.

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
Expand All @@ -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
~$ thumb-gen -d videos
~$ thumb-gen --dir videos
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
author="tharindu.dev",
author_email="[email protected]",
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,
Expand Down
4 changes: 2 additions & 2 deletions thumb_gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
8 changes: 4 additions & 4 deletions thumb_gen/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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}, \
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
84 changes: 82 additions & 2 deletions thumb_gen/config.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,82 @@
IMAGES = 12
IMAGE_QUALITY = 100
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/<USER>/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'])
27 changes: 23 additions & 4 deletions thumb_gen/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
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 = []

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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 61186e2

Please sign in to comment.