Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Sep 2, 2024
1 parent a809f87 commit fc250e4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
25 changes: 19 additions & 6 deletions twitchdl/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
from typing import Dict, Generator, List, Optional, Tuple
from urllib.parse import urlparse

import click
from PIL import Image, ImageDraw

from twitchdl import cache
from twitchdl.entities import Badge, Comment, Emote
from twitchdl.exceptions import ConsoleError
from twitchdl.fonts import Font, group_by_font, load_font
from twitchdl.naming import video_filename
from twitchdl.output import green, print_log, print_status
from twitchdl.twitch import get_comments, get_video, get_video_comments
from twitchdl.utils import format_time, iterate_with_next, parse_video_identifier
Expand All @@ -36,6 +38,9 @@ def render_chat(
font_size: int,
dark: bool,
padding: Tuple[int, int],
output: str,
format: str,
overwrite: bool,
):
video_id = parse_video_identifier(id)
if not video_id:
Expand All @@ -47,6 +52,13 @@ def render_chat(
raise ConsoleError(f"Video {video_id} not found")
total_duration = video["lengthSeconds"]

target_path = Path(video_filename(video, format, output))
if not overwrite and target_path.exists():
response = click.prompt("File exists. Overwrite? [Y/n]", default="Y", show_default=False)
if response.lower().strip() != "y":
raise click.Abort()
overwrite = True

print_log("Loading comments meta...")
video_comments = get_video_comments(video_id)
badges_by_id = {badge["id"]: badge for badge in video_comments["badges"]}
Expand Down Expand Up @@ -85,10 +97,8 @@ def render_chat(
f.write(f"file '{path.resolve()}'\n")
f.write(f"duration {duration}\n")

# TODO
output_path = Path(f"chat_{video_id}.mp4")
print_status("Generating chat video...", dim=True)
generate_video(spec_path, output_path)
generate_video(spec_path, target_path, overwrite)

print_status("Deleting cache...", dim=True)
shutil.rmtree(cache_dir)
Expand Down Expand Up @@ -305,7 +315,7 @@ def padded_image(self):
return padded_image


def generate_video(spec_path: Path, output: Path):
def generate_video(spec_path: Path, target: Path, overwrite: bool):
print_status("Generating chat video...")

command = [
Expand All @@ -323,15 +333,18 @@ def generate_video(spec_path: Path, output: Path):
"-stats",
"-loglevel",
"warning",
output,
target,
"-y",
]

if overwrite:
command.append("-y")

result = subprocess.run(command)
if result.returncode != 0:
raise ConsoleError("Joining files failed")

print_status(f"Saved: {green(output)}")
print_status(f"Saved: {green(target)}")


def shift(image: Image.Image, dy: int, background: str):
Expand Down
42 changes: 40 additions & 2 deletions twitchdl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,35 @@ def videos(
callback=validate_positive,
default=5,
)
def chat(id: str, width: int, height: int, font_size: int, dark: bool, pad_x: int, pad_y: int):
@click.option(
"-o",
"--output",
help="Output file name template. See docs for details.",
default="chat_{id}.{format}",
)
@click.option(
"-f",
"--format",
help="Video format to convert into, passed to ffmpeg as the target file extension.",
default="mp4",
)
@click.option(
"--overwrite",
help="Overwrite the target file if it already exists without prompting.",
is_flag=True,
)
def chat(
id: str,
width: int,
height: int,
font_size: int,
dark: bool,
pad_x: int,
pad_y: int,
output: str,
format: str,
overwrite: bool,
):
"""
Render chat for a given video.
Expand All @@ -481,7 +509,17 @@ def chat(id: str, width: int, height: int, font_size: int, dark: bool, pad_x: in
try:
from twitchdl.chat import render_chat

render_chat(id, width, height, font_size, dark, (pad_x, pad_y))
render_chat(
id,
width,
height,
font_size,
dark,
(pad_x, pad_y),
output,
format,
overwrite,
)
except ModuleNotFoundError as ex:
raise ConsoleError(
dedent(f"""
Expand Down

0 comments on commit fc250e4

Please sign in to comment.