Skip to content

Commit 8a3bf87

Browse files
authored
chore(lib): filter out video files without video stream (#416)
* chore(lib): filter out video files without video stream This is a ( hopefully temporary) fix to #390. Closes #390 * fix: Windows issue
1 parent 498e9af commit 8a3bf87

File tree

1 file changed

+83
-55
lines changed

1 file changed

+83
-55
lines changed

manim_slides/utils.py

Lines changed: 83 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import hashlib
2+
import os
23
import tempfile
34
from pathlib import Path
4-
from typing import List
5+
from typing import Iterator, List
56

67
import av
78

@@ -10,28 +11,54 @@
1011

1112
def concatenate_video_files(files: List[Path], dest: Path) -> None:
1213
"""Concatenate multiple video files into one."""
13-
f = tempfile.NamedTemporaryFile(mode="w", delete=False)
14-
f.writelines(f"file '{path.absolute()}'\n" for path in files)
15-
f.close()
1614

17-
input_ = av.open(f.name, options={"safe": "0"}, format="concat")
18-
input_stream = input_.streams.video[0]
19-
output = av.open(str(dest), mode="w")
20-
output_stream = output.add_stream(
21-
template=input_stream,
22-
)
23-
24-
for packet in input_.demux(input_stream):
25-
# We need to skip the "flushing" packets that `demux` generates.
26-
if packet.dts is None:
27-
continue
28-
29-
# We need to assign the packet to the new stream.
30-
packet.stream = output_stream
31-
output.mux(packet)
32-
33-
input_.close()
34-
output.close()
15+
def _filter(files: List[Path]) -> Iterator[Path]:
16+
"""Patch possibly empty video files."""
17+
for file in files:
18+
with av.open(str(file)) as container:
19+
if len(container.streams.video) > 0:
20+
yield file
21+
else:
22+
logger.warn(
23+
f"Skipping video file {file} because it does "
24+
"not contain any video stream. "
25+
"This is probably caused by Manim, see: "
26+
"https://github.com/jeertmans/manim-slides/issues/390."
27+
)
28+
29+
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
30+
f.writelines(f"file '{file}'\n" for file in _filter(files))
31+
tmp_file = f.name
32+
33+
with av.open(
34+
tmp_file, format="concat", options={"safe": "0"}
35+
) as input_container, av.open(str(dest), mode="w") as output_container:
36+
input_video_stream = input_container.streams.video[0]
37+
output_video_stream = output_container.add_stream(
38+
template=input_video_stream,
39+
)
40+
41+
if len(input_container.streams.audio) > 0:
42+
input_audio_stream = input_container.streams.audio[0]
43+
output_audio_stream = output_container.add_stream(
44+
template=input_audio_stream,
45+
)
46+
47+
for packet in input_container.demux():
48+
if packet.dts is None:
49+
continue
50+
51+
ptype = packet.stream.type
52+
53+
if ptype == "video":
54+
packet.stream = output_video_stream
55+
elif ptype == "audio":
56+
packet.stream = output_audio_stream
57+
else:
58+
continue # We don't support subtitles
59+
output_container.mux(packet)
60+
61+
os.unlink(tmp_file) # https://stackoverflow.com/a/54768241
3562

3663

3764
def merge_basenames(files: List[Path]) -> Path:
@@ -63,36 +90,37 @@ def link_nodes(*nodes: av.filter.context.FilterContext) -> None:
6390

6491
def reverse_video_file(src: Path, dest: Path) -> None:
6592
"""Reverses a video file, writting the result to `dest`."""
66-
input_ = av.open(str(src))
67-
input_stream = input_.streams.video[0]
68-
output = av.open(str(dest), mode="w")
69-
output_stream = output.add_stream(codec_name="libx264", rate=input_stream.base_rate)
70-
output_stream.width = input_stream.width
71-
output_stream.height = input_stream.height
72-
output_stream.pix_fmt = input_stream.pix_fmt
73-
74-
graph = av.filter.Graph()
75-
link_nodes(
76-
graph.add_buffer(template=input_stream),
77-
graph.add("reverse"),
78-
graph.add("buffersink"),
79-
)
80-
graph.configure()
81-
82-
frames_count = 0
83-
for frame in input_.decode(video=0):
84-
graph.push(frame)
85-
frames_count += 1
86-
87-
graph.push(None) # EOF: https://github.com/PyAV-Org/PyAV/issues/886.
88-
89-
for _ in range(frames_count):
90-
frame = graph.pull()
91-
frame.pict_type = 5 # Otherwise we get a warning saying it is changed
92-
output.mux(output_stream.encode(frame))
93-
94-
for packet in output_stream.encode():
95-
output.mux(packet)
96-
97-
input_.close()
98-
output.close()
93+
with av.open(str(src)) as input_container, av.open(
94+
str(dest), mode="w"
95+
) as output_container:
96+
input_stream = input_container.streams.video[0]
97+
output_stream = output_container.add_stream(
98+
codec_name=input_stream.codec_context.name, rate=input_stream.base_rate
99+
)
100+
output_stream.width = input_stream.width
101+
output_stream.height = input_stream.height
102+
output_stream.pix_fmt = input_stream.pix_fmt
103+
104+
graph = av.filter.Graph()
105+
link_nodes(
106+
graph.add_buffer(template=input_stream),
107+
graph.add("reverse"),
108+
graph.add("buffersink"),
109+
)
110+
graph.configure()
111+
112+
frames_count = 0
113+
for frame in input_container.decode(video=0):
114+
graph.push(frame)
115+
frames_count += 1
116+
117+
graph.push(None) # EOF: https://github.com/PyAV-Org/PyAV/issues/886.
118+
119+
for _ in range(frames_count):
120+
frame = graph.pull()
121+
122+
for packet in output_stream.encode(frame):
123+
output_container.mux(packet)
124+
125+
for packet in output_stream.encode():
126+
output_container.mux(packet)

0 commit comments

Comments
 (0)