Skip to content

Commit

Permalink
Add remux subtitles example
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Oct 10, 2024
1 parent 86f66ac commit 8927b01
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
8 changes: 8 additions & 0 deletions docs/cookbook/audio.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Audio
=====


Filters
-------

.. literalinclude:: ../../examples/audio/atempo.py
10 changes: 10 additions & 0 deletions docs/cookbook/subtitles.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Subtitles
=========


Remuxing
--------

Remuxing is copying stream(s) from one container to the other without transcoding it. By doing so, the data does not suffer any generational loss, and is the full quality that it was in the source container.

.. literalinclude:: ../../examples/subtitles/remux.py
4 changes: 0 additions & 4 deletions examples/audio_atempo.py → examples/audio/atempo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
output_file = av.open("output.wav", mode="w")

input_stream = input_file.streams.audio[0]

output_stream = output_file.add_stream("pcm_s16le", rate=input_stream.rate)
assert isinstance(output_stream, av.audio.AudioStream)

graph = av.filter.Graph()

graph.link_nodes(
graph.add_abuffer(template=input_stream),
graph.add("atempo", "2.0"),
Expand All @@ -31,6 +28,5 @@
for packet in output_stream.encode(None):
output_file.mux(packet)

# Close the files
input_file.close()
output_file.close()
2 changes: 2 additions & 0 deletions examples/basics/remux.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import av
import av.datasets

av.logging.set_level(av.logging.VERBOSE)

input_ = av.open(av.datasets.curated("pexels/time-lapse-video-of-night-sky-857195.mp4"))
output = av.open("remuxed.mkv", "w")

Expand Down
9 changes: 2 additions & 7 deletions examples/basics/save_keyframes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
stream = container.streams.video[0]
stream.codec_context.skip_frame = "NONKEY"

for frame in container.decode(stream):
for i, frame in enumerate(container.decode(stream)):
print(frame)

# We use `frame.pts` as `frame.index` won't make must sense with the `skip_frame`.
frame.to_image().save(
"night-sky.{:04d}.jpg".format(frame.pts),
quality=80,
)
frame.to_image().save(f"night-sky.{i:04d}.jpg", quality=80)
25 changes: 25 additions & 0 deletions examples/subtitles/remux.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import av

av.logging.set_level(av.logging.VERBOSE)

input_ = av.open("resources/webvtt.mkv")
output = av.open("remuxed.vtt", "w")

in_stream = input_.streams.subtitles[0]
out_stream = output.add_stream(template=in_stream)

for packet in input_.demux(in_stream):
if packet.dts is None:
continue
packet.stream = out_stream
output.mux(packet)

input_.close()
output.close()

print("Remuxing done")

with av.open("remuxed.vtt") as f:
for subset in f.decode(subtitles=0):
for sub in subset:
print(sub.ass)

0 comments on commit 8927b01

Please sign in to comment.