Skip to content

Commit

Permalink
Get color range info from PyAV
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Mar 6, 2024
1 parent 9c9e572 commit 6953ace
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
16 changes: 8 additions & 8 deletions auto_editor/ffwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ class VideoStream:
sar: Fraction
time_base: Fraction | None
pix_fmt: str | None
color_range: str | None
color_space: str | None
color_primaries: str | None
color_transfer: str | None
color_range: int
color_space: int
color_primaries: int
color_transfer: int
bitrate: int
lang: str | None

Expand Down Expand Up @@ -259,10 +259,10 @@ def initFileInfo(path: str, ffmpeg: FFmpeg, log: Log) -> FileInfo:
sar,
v.time_base,
v.codec_context.pix_fmt,
c_range,
c_space,
c_primary,
c_transfer,
v.color_range,
v.colorspace,
v.color_primaries,
v.color_trc,
0 if v.bit_rate is None else v.bit_rate,
v.language,
),
Expand Down
10 changes: 5 additions & 5 deletions auto_editor/formats/fcp11.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ def get_colorspace(src: FileInfo) -> str:
s = src.videos[0]
if s.pix_fmt == "rgb24":
return "sRGB IEC61966-2.1"
if s.color_space == "smpte170m":
return "6-1-6 (Rec. 601 NTSC)"
if s.color_space == "bt470bg":
if s.color_space == 5: # "bt470bg"
return "5-1-6 (Rec. 601 PAL)"
if s.color_primaries == "bt2020":
if s.color_space == 6: # "smpte170m"
return "6-1-6 (Rec. 601 NTSC)"
if s.color_primaries == 9: # "bt2020"
# See: https://video.stackexchange.com/questions/22059/how-to-identify-hdr-video
if s.color_transfer in ("arib-std-b67", "smpte2084"):
if s.color_transfer in (16, 18): # "smpte2084" "arib-std-b67"
return "9-18-9 (Rec. 2020 HLG)"
return "9-1-9 (Rec. 2020)"

Expand Down
19 changes: 13 additions & 6 deletions auto_editor/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,19 @@ def mux_quality_media(
cmd += _ffset("-c:a", args.audio_codec) + _ffset("-b:a", args.audio_bitrate)

if same_container and v_tracks > 0:
cmd += (
_ffset("-color_range", src.videos[0].color_range)
+ _ffset("-colorspace", src.videos[0].color_space)
+ _ffset("-color_primaries", src.videos[0].color_primaries)
+ _ffset("-color_trc", src.videos[0].color_transfer)
)
color_range = src.videos[0].color_range
colorspace = src.videos[0].color_space
color_prim = src.videos[0].color_primaries
color_trc = src.videos[0].color_transfer

if color_range == 1 or color_range == 2:
cmd.extend(["-color_range", f"{color_range}"])
if colorspace in (0, 1) or (colorspace >= 3 and colorspace < 16):
cmd.extend(["-colorspace", f"{colorspace}"])
if color_prim in (0, 1) or (color_prim >= 4 and color_prim < 17):
cmd.extend(["-color_primaries", f"{color_prim}"])
if color_trc == 1 or (color_trc >= 4 and color_trc < 22):
cmd.extend(["-color_trc", f"{color_trc}"])

if args.extras is not None:
cmd.extend(args.extras.split(" "))
Expand Down
28 changes: 21 additions & 7 deletions auto_editor/subcommands/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class VideoJson(TypedDict):
pixel_aspect_ratio: str
duration: float
pix_fmt: str | None
color_range: str | None
color_space: str | None
color_primaries: str | None
color_transfer: str | None
color_range: int
color_space: int
color_primaries: int
color_transfer: int
timebase: str
bitrate: int
lang: str | None
Expand Down Expand Up @@ -177,13 +177,27 @@ def stream_to_text(text: str, label: str, streams: list[dict[str, Any]]) -> str:
text += f" - track {s}:\n"
for key, value in stream.items():
if not is_null(key, value):
key = key.replace("_", " ")
if isinstance(value, list):
sep = "x" if key == "resolution" else ":"

value = sep.join(f"{x}" for x in value)

text += f" - {key}: {value}\n"
if key in (
"color_range",
"color_space",
"color_transfer",
"color_primaries",
):
if key == "color_range":
if value == 1:
text += " - color range: 1 (tv)\n"
elif value == 2:
text += " - color range: 2 (pc)\n"
elif value == 1:
text += f" - {key.replace('_', ' ')}: 1 (bt709)\n"
elif value != 2:
text += f" - {key.replace('_', ' ')}: {value}\n"
else:
text += f" - {key.replace('_', ' ')}: {value}\n"
return text

text = ""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [{ name = "WyattBlue", email = "[email protected]" }]
requires-python = ">=3.10"
dependencies = [
"numpy>=1.22.0",
"pyav==12.0.2",
"pyav==12.0.4",
"ae-ffmpeg==1.1.*",
]
keywords = [
Expand Down

0 comments on commit 6953ace

Please sign in to comment.