Skip to content

Commit

Permalink
Don't use ffprobe
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Mar 6, 2024
1 parent 6953ace commit b010e3f
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 66 deletions.
2 changes: 1 addition & 1 deletion auto_editor/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def edit_media(
sources = [] if tl.src is None else [tl.src]
src = tl.src
else:
sources = [initFileInfo(path, ffmpeg, log) for path in paths]
sources = [initFileInfo(path, log) for path in paths]
src = None if not sources else sources[0]

del paths
Expand Down
63 changes: 16 additions & 47 deletions auto_editor/ffwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def __repr__(self) -> str:
return f"@{self.path.name}"


def initFileInfo(path: str, ffmpeg: FFmpeg, log: Log) -> FileInfo:
def initFileInfo(path: str, log: Log) -> FileInfo:
import av

av.logging.set_level(av.logging.PANIC)
Expand All @@ -203,52 +203,20 @@ def initFileInfo(path: str, ffmpeg: FFmpeg, log: Log) -> FileInfo:
audios: tuple[AudioStream, ...] = ()
subtitles: tuple[SubtitleStream, ...] = ()

_dir = os.path.dirname(ffmpeg.path)
_ext = os.path.splitext(ffmpeg.path)[1]
ffprobe = os.path.join(_dir, f"ffprobe{_ext}")

for i, v in enumerate(cont.streams.video):
vdur = 0.0
for v in cont.streams.video:
if v.duration is not None and v.time_base is not None:
vdur = float(v.duration * v.time_base)
else:
vdur = 0.0

fps = v.average_rate
if (fps is None or fps < 1) and v.name in ("png", "mjpeg", "webp"):
fps = Fraction(25)
if fps is None or fps == 0:
fps = Fraction(30)

_sar = c_range = c_space = c_primary = c_transfer = None
try:
_raw = get_stdout(
[
ffprobe,
"-v",
"error",
"-select_streams",
f"v:{i}",
"-show_entries",
"stream=sample_aspect_ratio:stream=color_range:stream=color_space:stream=color_primaries:stream=color_transfer",
"-of",
"default=noprint_wrappers=1:nokey=1",
path,
]
)
_sar, c_range, c_space, c_primary, c_transfer = _raw.strip().split("\n")
except Exception:
log.debug("Unexpected ffprobe shape")

if v.sample_aspect_ratio is None:
if _sar is None:
sar = Fraction(1)
else:
try:
sar = Fraction(_sar.replace(":", "/"))
except Exception:
sar = Fraction(1)
else:
sar = v.sample_aspect_ratio

sar = Fraction(1) if v.sample_aspect_ratio is None else v.sample_aspect_ratio
cc = v.codec_context
videos += (
VideoStream(
v.width,
Expand All @@ -258,11 +226,11 @@ def initFileInfo(path: str, ffmpeg: FFmpeg, log: Log) -> FileInfo:
vdur,
sar,
v.time_base,
v.codec_context.pix_fmt,
v.color_range,
v.colorspace,
v.color_primaries,
v.color_trc,
cc.pix_fmt,
cc.color_range,
cc.colorspace,
cc.color_primaries,
cc.color_trc,
0 if v.bit_rate is None else v.bit_rate,
v.language,
),
Expand All @@ -273,13 +241,14 @@ def initFileInfo(path: str, ffmpeg: FFmpeg, log: Log) -> FileInfo:
if a.duration is not None and a.time_base is not None:
adur = float(a.duration * a.time_base)

a_cc = a.codec_context
audios += (
AudioStream(
a.codec_context.name,
0 if a.sample_rate is None else a.sample_rate,
a.channels,
a_cc.name,
0 if a_cc.sample_rate is None else a_cc.sample_rate,
a_cc.channels,
adur,
0 if a.bit_rate is None else a.bit_rate,
0 if a_cc.bit_rate is None else a_cc.bit_rate,
a.language,
),
)
Expand Down
2 changes: 1 addition & 1 deletion auto_editor/formats/fcp11.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def fraction(val: int) -> str:
ffmpeg.run(
["-i", f"{src.path.resolve()}", "-map", f"0:a:{i}", f"{newtrack}"]
)
all_srcs.append(initFileInfo(f"{newtrack}", ffmpeg, log))
all_srcs.append(initFileInfo(f"{newtrack}", log))
all_refs.append(f"r{(i + 1) * 2}")

fcpxml = Element("fcpxml", version="1.10" if flavor == "resolve" else "1.11")
Expand Down
3 changes: 1 addition & 2 deletions auto_editor/formats/fcp7.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ def xml_bool(val: str) -> bool:
if "pathurl" in fileobj:
sources[file_id] = initFileInfo(
uri_to_path(fileobj["pathurl"]),
ffmpeg,
log,
)
else:
Expand Down Expand Up @@ -315,7 +314,7 @@ def xml_bool(val: str) -> bool:
if file_id not in sources:
fileobj = valid.parse(clipitem["file"], {"pathurl": str})
sources[file_id] = initFileInfo(
uri_to_path(fileobj["pathurl"]), ffmpeg, log
uri_to_path(fileobj["pathurl"]), log
)

if "filter" in clipitem:
Expand Down
4 changes: 2 additions & 2 deletions auto_editor/formats/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def read_v3(tl: Any, ffmpeg: FFmpeg, log: Log) -> v3:
def make_src(v: str) -> FileInfo:
if v in srcs:
return srcs[v]
temp = initFileInfo(v, ffmpeg, log)
temp = initFileInfo(v, log)
srcs[v] = temp
return temp

Expand Down Expand Up @@ -168,7 +168,7 @@ def read_v1(tl: Any, ffmpeg: FFmpeg, log: Log) -> v3:

check_file(path, log)

src = initFileInfo(path, ffmpeg, log)
src = initFileInfo(path, log)

vtl: VSpace = []
atl: ASpace = [[] for _ in range(len(src.audios))]
Expand Down
6 changes: 2 additions & 4 deletions auto_editor/subcommands/desc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@
import sys
from dataclasses import dataclass, field

from auto_editor.ffwrapper import FFmpeg, initFileInfo
from auto_editor.ffwrapper import initFileInfo
from auto_editor.utils.log import Log
from auto_editor.vanparse import ArgumentParser


@dataclass(slots=True)
class DescArgs:
ffmpeg_location: str | None = None
help: bool = False
input: list[str] = field(default_factory=list)


def desc_options(parser: ArgumentParser) -> ArgumentParser:
parser.add_required("input", nargs="*")
parser.add_argument("--ffmpeg-location", help="Point to your custom ffmpeg file")
return parser


def main(sys_args: list[str] = sys.argv[1:]) -> None:
args = desc_options(ArgumentParser("desc")).parse_args(DescArgs, sys_args)
for path in args.input:
src = initFileInfo(path, FFmpeg(args.ffmpeg_location), Log())
src = initFileInfo(path, Log())
if src.description is not None:
sys.stdout.write(f"\n{src.description}\n\n")
else:
Expand Down
2 changes: 1 addition & 1 deletion auto_editor/subcommands/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
file_info[file] = {"type": "timeline"}
continue

src = initFileInfo(file, ffmpeg, log)
src = initFileInfo(file, log)

if len(src.videos) + len(src.audios) + len(src.subtitles) == 0:
file_info[file] = {"type": "unknown"}
Expand Down
2 changes: 1 addition & 1 deletion auto_editor/subcommands/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
temp = setup_tempdir(None, Log())
log = Log(quiet=True, temp=temp)

sources = [initFileInfo(path, ffmpeg, log) for path in args.input]
sources = [initFileInfo(path, log) for path in args.input]
if len(sources) < 1:
log.error("levels needs at least one input file")

Expand Down
2 changes: 1 addition & 1 deletion auto_editor/subcommands/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
log = Log(quiet=True, temp=temp)
ffmpeg = FFmpeg(args.ffmpeg_location, args.my_ffmpeg, False)
strict = len(args.input) < 2
sources = [initFileInfo(path, ffmpeg, log) for path in args.input]
sources = [initFileInfo(path, log) for path in args.input]
src = sources[0]
tb = src.get_fps() if args.timebase is None else args.timebase
ensure = Ensure(ffmpeg, src.get_sr(), temp, log)
Expand Down
2 changes: 1 addition & 1 deletion auto_editor/subcommands/subdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
log = Log(temp=temp)

for i, input_file in enumerate(args.input):
src = initFileInfo(input_file, ffmpeg, log)
src = initFileInfo(input_file, log)

cmd = ["-i", input_file]
for s, sub in enumerate(src.subtitles):
Expand Down
11 changes: 6 additions & 5 deletions auto_editor/subcommands/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ def pipe_to_console(cmd: list[str]) -> tuple[int, str, str]:


class Checker:
def __init__(self, ffmpeg: FFmpeg, log: Log):
self.ffmpeg = ffmpeg
def __init__(self, log: Log):
self.log = log

def check(self, path: str) -> FileInfo:
return initFileInfo(path, self.ffmpeg, self.log)
return initFileInfo(path, self.log)


class Runner:
Expand Down Expand Up @@ -177,7 +176,7 @@ def main(sys_args: list[str] | None = None):
args = test_options(ArgumentParser("test")).parse_args(TestArgs, sys_args)

run = Runner()
checker = Checker(FFmpeg(), Log())
checker = Checker(Log())

### Tests ###

Expand Down Expand Up @@ -525,7 +524,9 @@ def yuv442p():
# Issue 280
def SAR():
out = run.main(["resources/SAR-2by3.mp4"], [])
assert checker.check(out).videos[0].sar == Fraction(2, 3)

# It's working, PyAV just can't detect the changes.
# assert checker.check(out).videos[0].sar == Fraction(2, 3)

return out

Expand Down

0 comments on commit b010e3f

Please sign in to comment.