Skip to content

How do I get a valid subtitle from a container? #698

@Timtam

Description

@Timtam

IMPORTANT: Be sure to replace all template sections {{ like this }} or your issue may be discarded.

Overview

I need to either entirely demux a subtitle stream from a video container so that I can put it into e.g. an .ass file and it will be valid ass, or transcode a subtitle into a fixed target format like .srt and output that into a valid file.
I thus tried opening a video file and decoding its subtitle stream (ass) and encoding it into a new file with a .srt stream:

import av

cont=av.open("D:/test.mp4")
cont2=av.open('d:/test.srt', 'w')
srtstream = cont2.add_stream('srt')
assframes = cont.decode(subtitles=1)

for f in assframes:

  srtpackets = srtstream.encode(f)

  cont2.mux(srtpackets)
cont2.close()

Expected behavior

I expected calling container.decode() returning frames.

Actual behavior

When decoding a subtitle stream though, its instead returning SubtitleSet objects, which the stream encoder doesn't know to convert them to actual frames.

Traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\scripts\loudspeaker\test.py", line 10, in <module>
    srtpackets = srtstream.encode(f)
  File "av\stream.pyx", line 152, in av.stream.Stream.encode
TypeError: Cannot convert av.subtitles.subtitle.SubtitleSet to av.frame.Frame

Investigation

I tried decoding the subtitle stream itself, like this:

substream = cont.streams.subtitles[1]

frames = substream.decode()

That however crashes the interpreter silently.

I thought about taking the, I suppose, frames, which can be found in SubtitleSet.rects, and simply writing them to a file one by one, but I don't know if that will actually give a correct .ass file in the end.

So is there a way to get either a valid .ass file (demux the ass stream from my container) or, even better, transcode the ass subtitle into a srt one?

Research

I have done the following:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions