Merge remote-tracking branch 'easyffmpeg/main'

This commit is contained in:
RealStickman 2023-05-21 14:08:37 +02:00
commit 1240a56db8
4 changed files with 436 additions and 0 deletions

163
easyffmpeg/.gitignore vendored Normal file
View File

@ -0,0 +1,163 @@
# ---> 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/
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/
cover/
# 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/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .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
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# 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/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
README.md.tmp

3
easyffmpeg/README.md Normal file
View File

@ -0,0 +1,3 @@
# config-easyffmpeg
This script wraps the ffmpeg command I'm using often so I have to type less.
Probably not useful to anyone else, as it includes some hardcoded values for stuff I never change/want to keep constant in my media.

269
easyffmpeg/main.py Executable file
View File

@ -0,0 +1,269 @@
#!/usr/bin/env python3
import subprocess
import json
import os
import csv
# ffmpeg wrapper
import ffmpy
# argument parsing
import argparse
def valid_duration(filename: str, filetype: str):
"""
Check given file for presence of duration metadata
Parameters:
filename (str): Path to file
filetype (str): Should be INPUT or OUTPUT, to define if the issue appeared after encoding or before
"""
# NOTE check all files for an intact/valid duration
# Valid file example output:
# {
# "format": {
# "duration": "1425.058000"
# }
# }
# Invalid file:
# {
# "format": {
#
# }
# }
ff = ffmpy.FFprobe(
inputs={filename: None},
global_options=("-show_entries format=duration -v quiet -print_format json"),
)
proc = subprocess.Popen(
ff.cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True
)
info = json.loads(proc.stdout.read().rstrip().decode("utf8"))
# write broken files to error.csv files in current directory
if "duration" not in info["format"]:
with open("error.csv", "a", newline="") as file:
write = csv.writer(file)
write.writerow((filetype, filename))
parser = argparse.ArgumentParser(description="")
# Input file
parser.add_argument("-i", "--input-file", required=True, type=str, help="Input file")
# Media title
parser.add_argument("-t", "--title", required=True, type=str, help="Media title")
# Video stuff
parser.add_argument(
"-vc",
"--video-codec",
required=False,
type=str,
help="Output video codec. Defaults to 'copy'",
)
parser.add_argument(
"-crf",
"--crf",
required=False,
type=int,
help="Codec crf. No effect if video codec is 'copy'. Defaults to 20",
)
parser.add_argument(
"-vt", "--video-tune", required=False, type=str, help="Video codec tune"
)
parser.add_argument(
"-vp",
"--video-preset",
required=False,
type=str,
help="Video compression preset. Defaults to 'medium'",
)
# Audio stuff
parser.add_argument(
"-ac",
"--audio-codec",
required=False,
type=str,
help="Output audio codec. Defaults to 'copy'",
)
parser.add_argument(
"-ab",
"--audio-bitrate",
required=False,
type=str,
help="Output audio bitrate. No effect if audio codec is 'copy'. Defaults to '192k'",
)
parser.add_argument(
"-aj",
"--audio-japanese",
required=False,
type=str,
help="Stream identifier for japanese audio",
)
parser.add_argument(
"-ae",
"--audio-english",
required=False,
type=str,
help="Stream identifier for english audio",
)
# Subtitle stuff
parser.add_argument(
"-sn", "--subtitle-name", required=False, type=str, help="Name for subtitles"
)
parser.add_argument(
"-si",
"--subtitle-stream",
required=False,
type=str,
help="Stream identifier for subtitles",
)
parser.add_argument(
"-sd",
"--set-default-subtitle",
required=False,
action="store_true",
help="If passed, set the first subtitle as default",
)
# Output file
parser.add_argument("-o", "--output-file", required=True, type=str, help="Output file")
# Execute or print commands
parser.add_argument(
"-e",
"--execute",
action="store_true",
help="Execute script. If not set, shows the commands that would be run.",
)
args = parser.parse_args()
title = args.title
inputfile = args.input_file
# Default video codec is copy
if args.video_codec is None:
videocodec = "copy"
else:
videocodec = args.video_codec
# Default crf of 20
if args.crf is None:
crf = 20
else:
crf = args.crf
if args.video_tune is None:
tune = ""
else:
tune = "-tune " + args.video_tune
if args.video_preset is None:
preset = "medium"
else:
preset = args.video_preset
# Default audio codec is copy
if args.audio_codec is None:
audiocodec = "copy"
else:
audiocodec = args.audio_codec
# Default audio codec is copy
if args.audio_bitrate is None:
audiobitrate = "192k"
else:
audiobitrate = args.audio_bitrate
outputfile = args.output_file
# Map japanese audio, if set
if args.audio_japanese is None:
japaneseaudio = ""
else:
japaneseaudio = "-map " + args.audio_japanese
# Map english audio, if set
if args.audio_english is None:
englishaudio = ""
else:
englishaudio = "-map " + args.audio_english
# Audiometadata
if args.audio_japanese is None:
audiometa = "-metadata:s:a:0 title='English' -metadata:s:a:0 language=eng"
else:
audiometa = "-metadata:s:a:0 title='Japanese' -metadata:s:a:0 language=jpn -metadata:s:a:1 title='English' -metadata:s:a:1 language=eng"
subtitle = args.subtitle_name
subtitlestream = args.subtitle_stream
if args.set_default_subtitle:
defaultsub = "-disposition:s:0 default"
else:
defaultsub = ""
# Flag to actually execute command
execute = args.execute
# check input file for valid duration
valid_duration(inputfile, "INPUT")
# FIXME Breaks if any field (filename, title, Language) contains quotes: '
# Maps the first video stream, selected audio streams, selected subtitle streams and any attachments to the remuxed/reencoded video
ff = ffmpy.FFmpeg(
inputs={inputfile: None},
outputs={
outputfile: "-metadata title='{title}' -disposition 0 -map 0:t?"
" "
"-c:v {videocodec} -crf {crf} {tune} -preset {preset} -map 0:v:0 -metadata:s:v:0 title='Video' -disposition:v:0 default"
" "
"-c:a {audiocodec} -b:a {audiobitrate} {jpnaudiomap} {engaudiomap}"
" "
"{audiometa} -disposition:a:0 default"
" "
"-c:s copy -map {substream}? -metadata:s:s:0 title='{subtitle}' -metadata:s:s:0 language=eng {defaultsub}"
" ".format(
title=title,
videocodec=videocodec,
crf=crf,
tune=tune,
preset=preset,
audiocodec=audiocodec,
audiobitrate=audiobitrate,
jpnaudiomap=japaneseaudio,
engaudiomap=englishaudio,
audiometa=audiometa,
substream=subtitlestream,
subtitle=subtitle,
defaultsub=defaultsub,
)
},
)
if execute:
ff.run()
else:
print(ff.cmd)
# check output file for valid duration
valid_duration(outputfile, "OUTPUT")
if os.path.isfile("error.csv"):
print(
"Some media might have errors. Please check the error.csv file in this directory"
)

View File

@ -0,0 +1 @@
ffmpy