jonghwanhyeon / python-ffmpeg

A python binding for FFmpeg which provides sync and async APIs
MIT License
302 stars 53 forks source link

Question: How to extract subtitle with stdout? #30

Closed moi15moi closed 1 year ago

moi15moi commented 1 year ago

I saw this example: https://github.com/jonghwanhyeon/python-ffmpeg/blob/main/examples/output-as-bytes-via-stdout.py

I am currently trying to extract the subtitle with stdout:

from pathlib import Path
from ffmpeg import FFmpeg, Progress

def main():
    video_path = Path("C:\\XXX\\video.mkv")

    ffmpeg = (
        FFmpeg()
        .input(video_path)
        .output(
            "pipe:1",
            map=["0:4"],
        )
    )

    subtitles = ffmpeg.execute()

if __name__ == "__main__":
    main()

But, I received this exception:

Traceback (most recent call last):
  File "c:\Users\Admin\Desktop\test.py", line 21, in <module>
    main()
  File "c:\Users\Admin\Desktop\test.py", line 18, in main
    subtitles = ffmpeg.execute()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\ffmpeg\ffmpeg.py", line 150, in execute
    raise FFmpegError(f"Non-zero exit status {self._process.returncode}")
ffmpeg.ffmpeg.FFmpegError: Non-zero exit status 1

What can I do to avoid this exception and be able to extract the subtitle into the stdout?

jonghwanhyeon commented 1 year ago

If possible, could you share a clip of the input video?

moi15moi commented 1 year ago

Sure, here is the video: https://drive.google.com/file/d/1ISJMOGSW9iHOgBaunOLBNezMqpWHWnQy/view?usp=sharing

moi15moi commented 1 year ago

Oh, I just realised I needed to write the extention.

from pathlib import Path
from ffmpeg import FFmpeg, Progress

def main():
    video_path = Path("C:\\XXX\\video.mkv")

    ffmpeg = (
        FFmpeg()
        .input(video_path)
        .output(
            "pipe:1.ass",
            map=["0:4"],
        )
    )

    subtitles = ffmpeg.execute()

if __name__ == "__main__":
    main()
moi15moi commented 1 year ago

I would have another question. How can we use multiple output with pipe?

Currently, it merge the 2 pipe.

from ffmpeg import FFmpeg

def main():
    video_path = "video.mkv"

    ffmpeg = (
        FFmpeg()
        .input(video_path)
        .output(
            "pipe:4.ass",
            map=["0:4"],
        )
        .output(
            "pipe:5.ass",
            map=["0:5"],
        )

    )

    subtitles = ffmpeg.execute()

    print(subtitles)

if __name__ == "__main__":
    main()
jonghwanhyeon commented 1 year ago

I am so sorry but currently python-ffmpeg only supports stdout which is pipe:1. I think you can detour this by executing ffmpeg multiple times.

moi15moi commented 1 year ago

I think you can detour this by executing ffmpeg multiple times.

Sure, but I guess in term of performance, it is a bit faster to extract multiple subtitle in one call than call ffmpeg to extract 1 track at the time.