Initial commit

This commit is contained in:
Micha Albert 2025-02-17 17:14:50 -05:00
commit 0718089e52
7 changed files with 600 additions and 0 deletions

176
.gitignore vendored Normal file
View file

@ -0,0 +1,176 @@
# 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
# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.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/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/
# 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/
.vscode/
# Ruff stuff:
.ruff_cache/
# PyPI configuration file
.pypirc

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Micha Raphael Albert
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.

70
README.md Normal file
View file

@ -0,0 +1,70 @@
# Highlight Video Maker
This script automatically creates a highlight reel from multiple video inputs by identifying and combining the loudest moments.
## How It Works
1. **Finds a Representative Video**
- Chooses the shortest video as the "representative" video.
2. **Detects Loudest Moments**
- Analyzes the representative video to find the 10 loudest segments.
3. **Applies to All Videos**
- Uses the timestamps from the representative video to divide all others into clips.
- Note: This will make all videos the same length, removing the excess content from the end.
4. **Combines Clips**
- Merges the loudest segments using:
- The representative videos timestamps
- A randomly selected perspective clip from the other videos
5. **Final Outputs**
- A combonation of all vthe loudest clips, perspective randomly chosen
- Same as above, but with a mobile-friendly vertical aspect ratio with blurred letterboxing
- Both clips will be watermarked by an image input, with the image being slightly transparent
on the standard video output and opaque on the mobile one
## CLI Options
All options except `--log-level` are required.
```
$ vr-video-maker --help
Usage: vr-video-maker [OPTIONS] COMMAND [ARGS]...
Options:
--log-level Sets the logging verbosity. Choose betweenDEBUG, INFO (default), WARNING, ERROR, or CRITICAL.Can be uppercase or lowercase.
--help Show this message and exit.
Commands:
run Main function that orchestrates the video processing pipeline.
$ vr-video-maker run --help
Usage: vr-video-maker run [OPTIONS]
Main function that orchestrates the video processing pipeline.
Options:
--input-dir The input directory to get the source videos from.
--watermark-image The path of the watermark image to overlay over the final output. If not specified, no watermark will be applied. It will not be scaled, so it should be sized appropriately relative to the input.
--horiz-output-file The path to output the final video to. It should not exist and must either be an absolute path or start with "./".
--vert-output-file The path to output the final video to. It should not exist and must either be an absolute path or start with "./".
--help Show this message and exit.
```
## Requirements
- **Operating System:** Linux (tested on Arch Linux, will not work on Windows-like systems)
- **Dependencies:** [FFmpeg](https://ffmpeg.org/) must be installed on the host system
## Temporary Storage
- The script uses `/tmp/video-maker-cache` for temporary files.
- This folder can be safely deleted after the script finishes running.

27
pyproject.toml Normal file
View file

@ -0,0 +1,27 @@
[project]
name = "highlight_video_maker"
version = "0.0.1"
authors = [{ name = "Micha Albert", email = "micha@2231.tech" }]
description = "A utility to take several video inputs, take the loudest points, and create a compilation of them"
readme = "README.md"
requires-python = ">=3.12"
classifiers = [
"Programming Language :: Python :: 3",
"Operating System :: POSIX :: Linux",
]
license = "MIT"
license-files = ["LICENSE"]
dependencies = [
"click"
]
[project.scripts]
highlight-video-maker = "highlight_video_maker.main:cli"
[project.urls]
Homepage = "https://github.com/MichaByte/highlight-video-maker"
Issues = "https://github.com/MichaByte/highlight-video-maker/issues"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

View file

View file

@ -0,0 +1,41 @@
import logging
class CustomFormatter(logging.Formatter):
blue = "\x1b[34;20m"
green = "\x1b[32;20m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format_str = (
"%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
)
FORMATS = {
logging.DEBUG: blue + format_str + reset,
logging.INFO: green + format_str + reset,
logging.WARNING: yellow + format_str + reset,
logging.ERROR: red + format_str + reset,
logging.CRITICAL: bold_red + format_str + reset,
}
def format(self, record: logging.LogRecord):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
def get_logger(level: int = logging.INFO):
logger = logging.getLogger(__name__)
logger.setLevel(level)
# create console handler with a higher log level
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
console_handler.setFormatter(CustomFormatter())
logger.addHandler(console_handler)
return logger

View file

@ -0,0 +1,265 @@
import concurrent.futures
from logging import Logger, getLevelNamesMapping
import math
import random
import subprocess
from collections import Counter
from pathlib import Path
from typing import Dict, List
import click
from .logger import get_logger
logger: Logger
@click.group()
@click.option(
"--log-level",
default="INFO",
type=str,
required=False,
metavar="Sets the logging verbosity. Choose between"
"DEBUG, INFO (default), WARNING, ERROR, or CRITICAL."
"Can be uppercase or lowercase.",
)
def cli(log_level: str):
global logger
try:
level_from_name = getLevelNamesMapping()[log_level.upper()]
logger = get_logger(level_from_name)
except Exception as e:
logger.exception(e)
IN_DIR: Path
OUT_DIR: Path
CACHE_DIR = Path("/tmp/video-maker-cache")
THREADS = 16
MIN_SEGMENT_LENGTH = 3.5
MAX_SEGMENT_LENGTH = 7.5
MAX_SEGMENT_PADDING = 6
def seconds_to_timestamp(seconds: float):
"""Converts total seconds to a timestamp (HH:MM:SS.ms)."""
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
millis = int((seconds - int(seconds)) * 100)
return f"{hours:02}:{minutes:02}:{secs:02}.{millis:02}"
def get_video_duration(file: Path):
"""Gets the duration of a video file in seconds."""
logger.debug(f"Getting file length for {file}")
try:
return float(
subprocess.run(
f'ffprobe -v error -show_entries format=duration -of csv=p=0 "{file}"',
capture_output=True,
check=True,
text=True,
shell=True,
).stdout.strip()
)
except Exception:
logger.exception(f"Error getting file length for {file}")
return 0.0
def generate_segment_lengths(file_length: float) -> List[float]:
"""Generates random segment lengths that sum up to the file length."""
segment_lengths: List[float] = []
while not math.isclose(sum(segment_lengths), file_length, rel_tol=1e-5):
remaining_length = file_length - sum(segment_lengths)
if remaining_length <= MAX_SEGMENT_PADDING:
segment_lengths.append(remaining_length)
break
segment_lengths.append(
random.uniform(
MIN_SEGMENT_LENGTH, min(MAX_SEGMENT_LENGTH, remaining_length)
)
)
logger.debug(f"Generated segment lengths: {segment_lengths}")
return segment_lengths
def split_video_segment(
segment_lengths: List[float],
file_name: Path,
idx: int,
out_dir: Path = Path(CACHE_DIR),
):
"""Splits a video into segments using ffmpeg."""
logger.debug(f"Splitting {file_name} - segment {idx}")
subprocess.run(
f"ffmpeg -nostats -loglevel 0 -y -ss {seconds_to_timestamp(sum(segment_lengths[:idx]))} "
f'-to {seconds_to_timestamp(sum(segment_lengths[:idx]) + segment_lengths[idx])} -i "{file_name}" '
f'-c copy "{Path(out_dir, file_name.stem, str(idx) + file_name.suffix)}"',
check=True,
shell=True,
)
def get_amplitude_of_segment(clip: Path):
"""Extracts the mean audio amplitude of a video segment."""
logger.debug(f"Analyzing amplitude for clip: {clip}")
res = subprocess.run(
f'ffmpeg -i "{Path(CACHE_DIR, clip)}" -filter:a volumedetect -f null -',
shell=True,
check=True,
capture_output=True,
).stderr
return float(res.decode().split("mean_volume: ")[1].split(" dB")[0])
@cli.command()
@click.option(
"--input-dir",
metavar="The input directory to get the source videos from.",
type=click.Path(exists=True, resolve_path=True, path_type=Path),
)
@click.option(
"--watermark-image",
metavar="The path of the watermark image "
"to overlay over the final output. "
"It must exist. "
"It will not be scaled, so it should be "
"sized appropriately relative to the input.",
type=click.Path(exists=True, resolve_path=True, path_type=Path),
)
@click.option(
"--horiz-output-file",
metavar="The path to output the final video to. "
"It should not exist and must either be an absolute path "
'or start with "./".',
type=click.Path(exists=False, resolve_path=True, path_type=Path),
)
@click.option(
"--vert-output-file",
metavar="The path to output the final video to. "
"It should not exist and must either be an absolute path "
'or start with "./".',
type=click.Path(exists=False, resolve_path=True, path_type=Path),
)
def run(
input_dir: Path,
watermark_image: Path,
horiz_output_file: Path,
vert_output_file: Path,
):
"""Main function that orchestrates the video processing pipeline."""
logger.info("Starting video processing pipeline.")
raw_videos = next(input_dir.walk())
representative_video = min(
(Path(raw_videos[0], p) for p in raw_videos[2]), key=get_video_duration
)
logger.info(f"The representative video is: {representative_video}")
representative_video_segments = generate_segment_lengths(
get_video_duration(representative_video)
)
for vid in raw_videos[2]:
Path(CACHE_DIR, Path(vid).stem).resolve().mkdir(parents=True, exist_ok=True)
# Splitting videos into segments using multiprocessing
with concurrent.futures.ProcessPoolExecutor(max_workers=THREADS) as split_executor:
try:
Path(CACHE_DIR, representative_video.stem).mkdir(
parents=True, exist_ok=True
)
except FileExistsError:
pass
for idx in range(len(representative_video_segments)):
for vid in raw_videos[2]:
split_executor.submit(
split_video_segment,
representative_video_segments,
Path(raw_videos[0], vid).resolve(),
idx,
)
# Computing amplitude for each segment
representative_video_audio_futures: Dict[str, concurrent.futures.Future[float]] = {}
with concurrent.futures.ProcessPoolExecutor(
max_workers=THREADS
) as amplitude_executor:
for split_vid in next(Path(CACHE_DIR, Path(representative_video).stem).walk())[
2
]:
representative_video_audio_futures[split_vid] = amplitude_executor.submit(
get_amplitude_of_segment,
Path(CACHE_DIR, Path(representative_video).stem, split_vid).resolve(),
)
representative_video_audio_levels: Dict[str, float] = {}
# Collecting results
for seg in representative_video_audio_futures.keys():
representative_video_audio_levels[seg] = representative_video_audio_futures[
seg
].result()
highest = dict(Counter(representative_video_audio_levels).most_common(10))
loudest_seg_indexes: List[int] = [int(str(Path(k).stem)) for k in highest.keys()]
for video in raw_videos[2]:
out_folder = Path(CACHE_DIR, "loudest", Path(video).stem)
out_folder.mkdir(parents=True, exist_ok=True)
for seg in loudest_seg_indexes:
split_video_segment(
representative_video_segments,
Path(raw_videos[0], video),
seg,
out_folder.parent,
)
with open(str(Path(CACHE_DIR, "list.txt")), "w") as f:
for seg in loudest_seg_indexes:
random_seg = Path(random.choice(raw_videos[2]))
f.write(
f"file '{Path(CACHE_DIR, "loudest", random_seg.stem, str(seg) + random_seg.suffix)}'\n"
)
logger.info("Creating horizontal video...")
# Horizontal Pipeline: Concatenate clips and overlay a semitransparent watermark.
subprocess.run(
f'''ffmpeg -y -f concat -safe 0 -i "{Path(CACHE_DIR, "list.txt")}" -i "{watermark_image}" \
-filter_complex "
[1]format=rgba,colorchannelmixer=aa=0.5[logo];
[0][logo]overlay=W-w-30:H-h-30:format=auto,format=yuv420p
" -c:a copy "{horiz_output_file}"''',
shell=True,
check=True,
capture_output=True,
)
logger.info("Creating vertical video...")
# Vertical Pipeline: Concatenate, crop (zoom), split & blur for a vertical aspect ratio,
# then overlay a centered, opaque watermark at the bottom.
subprocess.run(
f'''ffmpeg -y -f concat -safe 0 -i "{Path(CACHE_DIR, "list.txt")}" -i "{watermark_image}" \
-filter_complex "
[0]crop=3/4*in_w:in_h[zoomed];
[zoomed]split[original][copy];
[copy]scale=-1:ih*(4/3)*(4/3),crop=w=ih*9/16,gblur=sigma=17:steps=5[blurred];
[blurred][original]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2[vert];
[vert][1]overlay=(W-w)/2:H-h-30,format=yuv420p
" -c:a copy "{vert_output_file}"''',
shell=True,
check=True,
capture_output=True,
)
logger.info("Video processing pipeline completed.")
if __name__ == "__main__":
cli()