Skip to content

Commit

Permalink
Add option to pick h264 or hevc (default)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonjchen committed Jul 11, 2024
1 parent 664a31c commit 9e19909
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
25 changes: 19 additions & 6 deletions ffmpeg_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def make_ffmpeg_clip(
start_seconds: int,
length_seconds: int,
target_mb: int,
format: str,
nvidia_hardware_rendering: bool,
forward_upon_wide_h: float,
output: str,
Expand All @@ -61,6 +62,9 @@ def make_ffmpeg_clip(
route = re.sub(r"--\d+$", "", route_or_segment)
route_date = re.sub(r"^[^|]+\|", "", route)

if format not in ["h264", "hevc"]:
raise ValueError(f"Invalid format: {format}")

# Target bitrate in bits per second (bps). Try to get close to the target file size.
target_bps = (target_mb) * 8 * 1024 * 1024 // length_seconds
# Start seconds relative to the start of the concatenated video
Expand Down Expand Up @@ -126,8 +130,11 @@ def make_ffmpeg_clip(
"+faststart",
]
if nvidia_hardware_rendering:
# Use H264 for maximum Discord compatibility
command += ["-c:v", "h264_nvenc"]
if format == "h264":
command += ["-c:v", "h264_nvenc"]
elif format == "hevc":
command += ["-c:v", "hevc_nvenc"]
command += ["-vtag", "hvc1"]

# Target bitrate with maxrate and bufsize
command += [
Expand Down Expand Up @@ -184,7 +191,11 @@ def make_ffmpeg_clip(
"+faststart",
]
if nvidia_hardware_rendering:
command += ["-c:v", "hevc_nvenc"]
if format == "h264":
command += ["-c:v", "h264_nvenc"]
elif format == "hevc":
command += ["-c:v", "hevc_nvenc"]
command += ["-vtag", "hvc1"]

# Target bitrate with maxrate and bufsize
command += [
Expand Down Expand Up @@ -275,9 +286,11 @@ def make_ffmpeg_clip(
]

if nvidia_hardware_rendering:
# Use HEVC encoding for 360 since people aren't looking at these
# directly in Discord anyway.
command += ["-c:v", "hevc_nvenc"]
if format == "h264":
command += ["-c:v", "h264_nvenc"]
elif format == "hevc":
command += ["-c:v", "hevc_nvenc"]
command += ["-vtag", "hvc1"]

# Target bitrate with maxrate and bufsize
command += [
Expand Down
10 changes: 10 additions & 0 deletions predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ def predict(
fileSize: int = Input(
description="Rough size of clip output in MB.", ge=10, le=100, default=25
),
fileFormat: str = Input(
description="H.264 or HEVC (HEVC is 50-60 percent higher quality for its filesize but may not be compatible with all web browsers or devices).",
choices=[
"h264",
"hevc",
],
default="hevc",
),
jwtToken: str = Input(
description='Optional JWT Token from https://jwt.comma.ai for non-"Public access" routes. ⚠️ DO NOT SHARE THIS TOKEN WITH ANYONE as https://jwt.comma.ai generates JWT tokens valid for 1 year and they are irrevocable. Please use the safer, optionally temporary, more granular, and revocable "Public Access" toggle option on comma connect if possible. For more info, please see https://github.com/nelsonjchen/op-replay-clipper#jwt-token-input .',
default="",
Expand Down Expand Up @@ -150,6 +158,7 @@ def predict(
f"--smear-amount={smearAmount}",
f"--speedhack-ratio={speedhackRatio}",
f"--target-mb={fileSize}",
f"--format={fileFormat}",
f"--nv-hybrid-encoding",
f"--data-dir={os.path.abspath(data_dir)}",
f"--output=cog-clip.mp4",
Expand Down Expand Up @@ -232,6 +241,7 @@ def predict(
start_seconds=startSeconds,
length_seconds=lengthSeconds,
target_mb=fileSize,
format=fileFormat,
nvidia_hardware_rendering=True,
forward_upon_wide_h=forwardUponWideH,
output="./shared/cog-clip.mp4",
Expand Down

0 comments on commit 9e19909

Please sign in to comment.