Skip to content

Commit

Permalink
fix #283 remove / add split will change input order (#284)
Browse files Browse the repository at this point in the history
* fix #283 remove / add split will change input order

* add more test case
  • Loading branch information
lucemia authored Feb 17, 2024
1 parent efa6e71 commit 1c0fcff
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 14 deletions.
148 changes: 148 additions & 0 deletions src/ffmpeg/dag/tests/__snapshots__/test_validate.ambr

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 28 additions & 2 deletions src/ffmpeg/dag/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from syrupy.assertion import SnapshotAssertion

from ...base import input
from ...filters import concat
from ...filters import amix, concat
from ...utils.snapshot import DAGSnapshotExtenstion
from ..context import DAGContext
from ..schema import Stream
Expand Down Expand Up @@ -54,9 +54,35 @@ def complex_stream() -> Any:
return pytest.param(graph, id="complex-stream")


def amix_stream() -> Any:
input1 = input("input1.mp4")

graph = amix(input1.audio.areverse().areverse(), input1.audio.areverse(), duration="firat").output(
filename="tmp.mp4"
)
return pytest.param(graph, id="amix-stream")


def amix_stream_2() -> Any:
input1 = input("input1.mp4")

graph = amix(input1.audio.areverse(), input1.audio.areverse().areverse(), duration="firat").output(
filename="tmp.mp4"
)
return pytest.param(graph, id="amix-stream-2")


@pytest.mark.parametrize(
"graph",
[reduntant_split_duplicate(), redundant_split_outputs_1(), not_utilize_split(), reuse_input(), complex_stream()],
[
reduntant_split_duplicate(),
redundant_split_outputs_1(),
not_utilize_split(),
reuse_input(),
complex_stream(),
amix_stream(),
amix_stream_2(),
],
)
def test_rebuild_graph(graph: Stream, snapshot: SnapshotAssertion) -> None:
context = DAGContext.build(graph.node)
Expand Down
20 changes: 13 additions & 7 deletions src/ffmpeg/dag/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ def remove_split(current_stream: Stream, mapping: dict[Stream, Stream] = None) -
mapping[current_stream] = mapping[current_stream.node.inputs[0]]
return mapping[current_stream.node.inputs[0]], mapping

inputs = []
for input_stream in sorted(current_stream.node.inputs, key=lambda stream: -len(stream.node.upstream_nodes)):
inputs = {}
for idx, input_stream in sorted(
enumerate(current_stream.node.inputs), key=lambda idx_stream: -len(idx_stream[1].node.upstream_nodes)
):
new_stream, _mapping = remove_split(current_stream=input_stream, mapping=mapping)
inputs.append(new_stream)
inputs[idx] = new_stream
mapping |= _mapping

new_node = replace(current_stream.node, inputs=tuple(inputs))
new_node = replace(
current_stream.node, inputs=tuple(stream for idx, stream in sorted(inputs.items(), key=lambda x: x[0]))
)
new_stream = replace(current_stream, node=new_node)

mapping[current_stream] = new_stream
Expand All @@ -69,18 +73,20 @@ def add_split(
if (current_stream, down_node, down_index) in mapping:
return mapping[(current_stream, down_node, down_index)], mapping

inputs = []
inputs = {}

for idx, input_stream in sorted(
enumerate(current_stream.node.inputs), key=lambda idx_stream: -len(idx_stream[1].node.upstream_nodes)
):
new_stream, _mapping = add_split(
current_stream=input_stream, down_node=current_stream.node, down_index=idx, mapping=mapping, context=context
)
inputs.append(new_stream)
inputs[idx] = new_stream
mapping |= _mapping

new_node = replace(current_stream.node, inputs=tuple(inputs))
new_node = replace(
current_stream.node, inputs=tuple(stream for idx, stream in sorted(inputs.items(), key=lambda x: x[0]))
)
new_stream = replace(current_stream, node=new_node)

num = len(context.get_outgoing_nodes(current_stream))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[
"ffmpeg",
"-i",
"in2.mp4",
"-i",
"in1.mp4",
"-i",
"in2.mp4",
"-filter_complex",
"[0:v]reverse[s0];[0:a]areverse[s1];[1:v]hflip[s2];[s0]hue=s=0[s3];[s1]aphaser[s4];[s3][s4][s2][1:a]concat=v=1:a=1[s5#0][s5#1];[s5#1]volume=volume=0.8[s6]",
"-map",
"[s6]",
"[0:v]hflip[s0];[1:v]reverse[s1];[1:a]areverse[s2];[s1]hue=s=0[s3];[s2]aphaser[s4];[s0][0:a][s3][s4]concat=v=1:a=1[s5#0][s5#1];[s5#1]volume=volume=0.8[s6]",
"-map",
"[s5#0]",
"-map",
"[s6]",
"out.mp4"
]

0 comments on commit 1c0fcff

Please sign in to comment.