Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
paranarimasu committed Oct 23, 2020
0 parents commit 0bbfb1e
Show file tree
Hide file tree
Showing 16 changed files with 1,120 additions and 0 deletions.
148 changes: 148 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@

# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
pytestdebug.log

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/
doc/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pythonenv*

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# profiling data
.prof

# JetBrains IDEs
.idea

# End of https://www.toptal.com/developers/gitignore/api/python
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 paranarimasu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Description

Generate and execute collection of FFmpeg commands sequentially from external file to produce WebMs that meet [AnimeThemes.moe](https://animethemes.moe/) encoding standards.

Take advantage of sleep, work, or any other time that we cannot actively monitor the encoding process to produce a set of encodes for later quality checking and/or tweaking for additional encodes.

Ideally we are iterating over a combination of filters and settings, picking the best one at the end.

### Install

**Requirements:**

* FFmpeg
* Python >= 3.6

**Install:**

pip install animethemes-batch-encoder

### Usage

python -m batch_encoder [-h] --mode [{1,2,3}] [--file [FILE]] [--configfile [CONFIGFILE]] --loglevel [{debug,info,error}]

* `--mode 1`: Generates commands from input files in the current directory. User will be prompted for inclusion/exclusion of input file, start time, end time and output file name.
* `--mode 2`: Execute commands from file line-by-line in the current directory.
* `--mode 3`: Generate commands from input files in the current directory in the same manner as Mode 1 and then execute commands without writing to file.
* `[FILE]`: The file that commands are written to or read from. Default: commands.txt in the current directory. Unused in `--mode 3`.
* `[CONFIGFILE]`: The configuration file in which our properties are defined. Default: batch_encoder.ini written to the same directory as the batch_encoder.py script.
* `AllowedFileTypes`: Configuration property for file extensions that will be considered source file candidates
* `EncodingModes`: Configuration property for the inclusion and order of encoding modes `{CBR, VBR, CQ}`
* `CRFs`: Configuration property for the ordered list of CRF values to use with VBR and/or CQ encoding modes
* `IncludeUnfiltered`: Configuration property that sets the flag for including/excluding an encode without video filters for each EncodingMode
* `VideoFilters`: Configuration items used for named video filtergraphs
* `--loglevel error`: Only show error messages
* `--loglevel info`: Show error messages and script progression info messages
* `--loglevel debug`: Show all messages, including variable dumps
14 changes: 14 additions & 0 deletions batch-encoder.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Encoding]
allowedfiletypes = .avi,.m2ts,.mkv,.mp4,.wmv
encodingmodes = VBR,CBR
crfs = 12,15,18,21,24
includeunfiltered = True
defaultvideostream =
defaultaudiostream =

[VideoFilters]
filtered = hqdn3d=0:0:3:3,gradfun,unsharp
lightdenoise = hqdn3d=0:0:3:3
heavydenoise = hqdn3d=1.5:1.5:6:6
unsharp = unsharp

Empty file added batch_encoder/__init__.py
Empty file.
131 changes: 131 additions & 0 deletions batch_encoder/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
from ._encode_webm import EncodeWebM
from ._encoding_config import EncodingConfig
from ._seek_collector import SeekCollector
from ._source_file import SourceFile
from ._utils import commandfile_arg_type
from ._utils import configfile_arg_type

import argparse
import configparser
import logging
import os
import shutil
import subprocess
import sys


def main():
# Load/Validate Arguments
parser = argparse.ArgumentParser(prog='batch-encoder',
description='Generate/Execute FFmpeg commands for files in acting directory',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--mode', nargs='?', type=int, choices=[1, 2, 3], required=True,
help='1: Generate commands and write to file\n'
'2: Execute commands from file\n'
'3: Generate and execute commands')
parser.add_argument('--file', nargs='?', default='commands.txt', type=commandfile_arg_type,
help='1: Name of file commands are written to (default: commands.txt)\n'
'2: Name of file commands are executed from (default: commands.txt)\n'
'3: Unused')
parser.add_argument('--configfile', nargs='?', default='batch-encoder.ini', type=configfile_arg_type,
help='Name of config file (default: batch-encoder.ini)\n'
'If the file does not exist, default configuration will be written\n'
'The file is expected to exist in the same directory as this script')
parser.add_argument('--loglevel', nargs='?', default='info', choices=['debug', 'info', 'error'],
help='Set logging level')
args = parser.parse_args()

# Logging Config
logging.basicConfig(stream=sys.stdout, level=logging.getLevelName(args.loglevel.upper()),
format='%(levelname)s: %(message)s')

# Env Check: Check that dependencies are installed
if shutil.which('ffmpeg') is None:
logging.error('FFmpeg is required')
sys.exit()

if shutil.which('ffprobe') is None:
logging.error('FFprobe is required')
sys.exit()

# Write default config file if it doesn't exist
config = configparser.ConfigParser()
config_file = os.path.join(sys.path[0], args.configfile)
if not os.path.exists(config_file):
config['Encoding'] = {EncodingConfig.config_allowed_filetypes: EncodingConfig.default_allowed_filetypes,
EncodingConfig.config_encoding_modes: EncodingConfig.default_encoding_modes,
EncodingConfig.config_crfs: EncodingConfig.default_crfs,
EncodingConfig.config_include_unfiltered: EncodingConfig.default_include_unfiltered,
EncodingConfig.config_default_video_stream: '',
EncodingConfig.config_default_audio_stream: ''}
config['VideoFilters'] = EncodingConfig.default_video_filters

with open(config_file, 'w', encoding='utf8') as f:
config.write(f)

# Load config file
config.read(config_file)
encoding_config = EncodingConfig.from_config(config)

commands = []

# Generate commands from source file candidates in current directory
if args.mode == 1 or args.mode == 3:
source_file_candidates = [f for f in os.listdir('.') if f.endswith(tuple(encoding_config.allowed_filetypes))]

if not source_file_candidates:
logging.error('No source file candidates in current directory')
sys.exit()

for source_file_candidate in source_file_candidates:
if SourceFile.yes_or_no(source_file_candidate):
try:
source_file = SourceFile.from_file(source_file_candidate, encoding_config)

is_collector_valid = False
seek_collector = None
while not is_collector_valid:
seek_collector = SeekCollector(source_file)
is_collector_valid = seek_collector.is_valid()

for seek in seek_collector.get_seek_list():
logging.info(f'Generating commands with seek ss: \'{seek.ss}\', to: \'{seek.to}\'')
encode_webm = EncodeWebM(source_file, seek)
commands = commands + encode_webm.get_commands(encoding_config)
except KeyboardInterrupt:
logging.info(f'Exiting from inclusion of file \'{source_file_candidate}\' after keyboard interrupt')

# Write commands to file
if args.mode == 1:
logging.info(f'Writing {len(commands)} commands to file \'{args.file}\'...')
with open(args.file, mode='w', encoding='utf8') as f:
for command in commands:
f.write(command + '\n')

# Read and execute commands from file
if args.mode == 2:
if not os.path.isfile(args.file):
logging.error(f'File \'{args.file}\' does not exist')
sys.exit()

with open(args.file, mode='r', encoding='utf8') as f:
for command in f:
commands.append(command)

logging.info(f'Reading {len(commands)} commands from file \'{args.file}\'...')

for command in commands:
subprocess.call(command, shell=True)

# Execute commands in memory
if args.mode == 3:
logging.info(f'Executing {len(commands)} commands...')
for command in commands:
subprocess.call(command, shell=True)


if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
logging.error('Exiting after keyboard interrupt')
24 changes: 24 additions & 0 deletions batch_encoder/_bitrate_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import enum


# The Bitrate Mode Enumerated List
# Bitrate Mode determines the rate control argument values for our commands
# Further Reading: https://developers.google.com/media/vp9/bitrate-modes
class BitrateMode(enum.Enum):
def __new__(cls, value, first_pass_rate_control, second_pass_rate_control):
obj = object.__new__(cls)
obj._value_ = value
obj.first_pass_rate_control = first_pass_rate_control
obj.second_pass_rate_control = second_pass_rate_control
return obj

# Constant Bitrate Mode
CBR = (0, lambda cbr_bitrate, cbr_max_bitrate, crf: f'-b:v {cbr_bitrate} -maxrate {cbr_max_bitrate} -qcomp 0.3',
lambda cbr_bitrate, cbr_max_bitrate,
crf: f'-b:v {cbr_bitrate} -maxrate {cbr_max_bitrate} -bufsize 6000k -qcomp 0.3')
# Variable Bitrate Mode / Constant Quality Mode
VBR = (1, lambda cbr_bitrate, cbr_max_bitrate, crf: f'-crf {crf} -b:v 0 -qcomp 0.7',
lambda cbr_bitrate, cbr_max_bitrate, crf: f'-crf {crf} -b:v 0 -qcomp 0.7')
# Constrained Quality Mode
CQ = (2, lambda cbr_bitrate, cbr_max_bitrate, crf: f'-crf {crf} -b:v {cbr_bitrate} -qcomp 0.7',
lambda cbr_bitrate, cbr_max_bitrate, crf: f'-crf {crf} -b:v {cbr_bitrate} -qcomp 0.7')
Loading

0 comments on commit 0bbfb1e

Please sign in to comment.