Skip to content
Snippets Groups Projects
Commit fc6f8094 authored by Simon Künzel's avatar Simon Künzel
Browse files

Add crf for ffmpeg

parent 2979e295
No related branches found
No related tags found
No related merge requests found
...@@ -70,6 +70,7 @@ class SourceFileTargetProducer(SingleOutputTargetProducer["SourceMedium"]): ...@@ -70,6 +70,7 @@ class SourceFileTargetProducer(SingleOutputTargetProducer["SourceMedium"]):
class RescaleVideoTargetProducer(SingleInputTargetProducer, SingleOutputTargetProducer): class RescaleVideoTargetProducer(SingleInputTargetProducer, SingleOutputTargetProducer):
target_vertical_resolution: int = json_field(min_value=1, max_value=10000) target_vertical_resolution: int = json_field(min_value=1, max_value=10000)
crf: int | None = None
@classmethod @classmethod
def get_type(cls) -> str: def get_type(cls) -> str:
...@@ -96,7 +97,8 @@ class RescaleVideoTargetProducer(SingleInputTargetProducer, SingleOutputTargetPr ...@@ -96,7 +97,8 @@ class RescaleVideoTargetProducer(SingleInputTargetProducer, SingleOutputTargetPr
return "rescale_video", { return "rescale_video", {
"input_file": input_medium.file.file_path, "input_file": input_medium.file.file_path,
"output_file": output_file.file_path, "output_file": output_file.file_path,
"target_vertical_resolution": self.target_vertical_resolution "target_vertical_resolution": self.target_vertical_resolution,
"crf": self.crf
} }
......
...@@ -252,6 +252,7 @@ _AUDIO_CODECS = [OutputCodec.OPUS, OutputCodec.FLAC, OutputCodec.AAC] ...@@ -252,6 +252,7 @@ _AUDIO_CODECS = [OutputCodec.OPUS, OutputCodec.FLAC, OutputCodec.AAC]
class OutputStream(JsonDataClass): class OutputStream(JsonDataClass):
graph_id: str graph_id: str
framerate: int | None = json_field(default=None, note="If not specified, ffmpeg will guess") framerate: int | None = json_field(default=None, note="If not specified, ffmpeg will guess")
crf: int | None = None
codec: OutputCodec codec: OutputCodec
...@@ -324,7 +325,12 @@ class OutputFileJNode(JGraphNode): ...@@ -324,7 +325,12 @@ class OutputFileJNode(JGraphNode):
if output_stream.framerate is not None: if output_stream.framerate is not None:
if not isinstance(metadata, FVideoStreamMetadata): if not isinstance(metadata, FVideoStreamMetadata):
raise ValueError(f"Cannot set output framerate for non-video stream {output_stream.graph_id}") raise ValueError(f"Cannot set output framerate for non-video stream {output_stream.graph_id}")
out_args["r"] = output_stream.framerate out_args["r"] = output_stream.framerate#
if output_stream.crf is not None:
if not isinstance(metadata, FVideoStreamMetadata):
raise ValueError(f"Cannot set output crf for non-video stream {output_stream.graph_id}")
out_args["crf"] = output_stream.crf
streams.append((metadata.fid, out_args)) streams.append((metadata.fid, out_args))
......
...@@ -205,7 +205,8 @@ ...@@ -205,7 +205,8 @@
{ {
"graph_id": "video_with_inoutro_logo", "graph_id": "video_with_inoutro_logo",
"codec": "av1", "codec": "av1",
"framerate": 50 "framerate": 50,
"crf": 23
}, },
{ {
"graph_id": "audio_with_inoutro", "graph_id": "audio_with_inoutro",
...@@ -218,5 +219,5 @@ ...@@ -218,5 +219,5 @@
] ]
}, },
{"type": "sample_thumbnail", "timestamp_percent": 25, "input_id": "processed_video", "output_id": "thumbnail"}, {"type": "sample_thumbnail", "timestamp_percent": 25, "input_id": "processed_video", "output_id": "thumbnail"},
{"type": "rescale_video", "target_vertical_resolution": 720, "input_id": "processed_video", "output_id": "video_720"} {"type": "rescale_video", "target_vertical_resolution": 720, "crf": 23, "input_id": "processed_video", "output_id": "video_720"}
], "publish_target_ids": ["processed_video", "thumbnail", "video_720"], "publish_wait_for_full_process": true} ], "publish_target_ids": ["processed_video", "thumbnail", "video_720"], "publish_wait_for_full_process": true}
\ No newline at end of file
...@@ -4,7 +4,7 @@ import subprocess ...@@ -4,7 +4,7 @@ import subprocess
from pathlib import Path from pathlib import Path
from videoag_common.ffmpeg import get_file_extension, FFProbe, FFProbeVideoStream from videoag_common.ffmpeg import get_file_extension, FFProbe, FFProbeVideoStream
from videoag_common.miscellaneous import CJsonObject, MAX_VALUE_SINT32 from videoag_common.miscellaneous import CJsonObject, MAX_VALUE_SINT32, MIN_VALUE_SINT32
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -17,6 +17,7 @@ def execute(database, own_job_id, input_data: CJsonObject): ...@@ -17,6 +17,7 @@ def execute(database, own_job_id, input_data: CJsonObject):
output_file = _DATA_DIR.joinpath(input_data.get_string("output_file", max_length=None)) output_file = _DATA_DIR.joinpath(input_data.get_string("output_file", max_length=None))
tmp_output_file = Path("/tmp/job_output." + get_file_extension(output_file)) tmp_output_file = Path("/tmp/job_output." + get_file_extension(output_file))
target_vertical_resolution = input_data.get_int("target_vertical_resolution", min_value=0, max_value=MAX_VALUE_SINT32) target_vertical_resolution = input_data.get_int("target_vertical_resolution", min_value=0, max_value=MAX_VALUE_SINT32)
crf = input_data.get_int("crf", min_value=MIN_VALUE_SINT32, max_value=MAX_VALUE_SINT32, optional=True)
if not input_file.exists() or not input_file.is_file(): if not input_file.exists() or not input_file.is_file():
raise ValueError(f"Input file {input_file} doesn't exist") raise ValueError(f"Input file {input_file} doesn't exist")
...@@ -51,6 +52,8 @@ def execute(database, own_job_id, input_data: CJsonObject): ...@@ -51,6 +52,8 @@ def execute(database, own_job_id, input_data: CJsonObject):
raise ValueError(f"Unable to chose encoder for unknown codec '{codec}'") raise ValueError(f"Unable to chose encoder for unknown codec '{codec}'")
cmd.extend([f"-filter:{i}", f"scale=height={target_vertical_resolution}:width=-1"]) cmd.extend([f"-filter:{i}", f"scale=height={target_vertical_resolution}:width=-1"])
cmd.extend([f"-c:{i}", encoder]) cmd.extend([f"-c:{i}", encoder])
if crf is not None:
cmd.extend([f"-crf:{i}", str(crf)])
cmd.extend(["-c:a", "copy"]) cmd.extend(["-c:a", "copy"])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment