kaltura / nginx-vod-module

NGINX-based MP4 Repackager
GNU Affero General Public License v3.0
1.96k stars 431 forks source link

Does this package use ffmpeg for Repackaging? #1508

Open Saeeed-B opened 4 months ago

Saeeed-B commented 4 months ago

Does this package use ffmpeg for Repackaging?

And whether it is possible to make a Repackager like this with Python?

erankor commented 4 months ago
  1. No, this module has its own implementation of the packaging, for performance reasons. Ffmpeg is used only for encoding/decoding, in features like audio rate change and thumbnail capture.
  2. You can implement anything you want in python, but it's not related to this module...
Saeeed-B commented 4 months ago

I tried to cut and encode 5 seconds of video using ffmpeg in Python and return the output as a response. But it is very slow, while using all the capacity of the processor.

What do you think can improve the speed of this code?


def run(source, start, chuck):
    input = os.path.join(BASE_DIR, source)
    main_list = [
        'ffmpeg', '-y', '-i', f'{input}',
        '-ss', f'0{start}', '-t', f'00:00:05', '-async', '1',
    ]

    main_list += [
        '-c:a', 'aac',
        '-c:v', 'libx264',
        '-preset', 'ultrafast',
       '-f', 'segment',
        '-segment_time', '5',
        '-segment_format', 'mpegts',
        '-segment_start_number', f'{chuck}',
        f'R:/1080_%03d.ts',
    ]
    result = subprocess.run(main_list, capture_output=True, text=True)
erankor commented 4 months ago

You are transcoding the video with this command, it is expected to be slow. If -

  1. You only need to cut the video
  2. The source video is already using web-playable codecs
  3. The source video has keyframes in the relevant offsets

Then you can avoid the transcode, and just use -c copy, this will run MUCH faster.

Saeeed-B commented 4 months ago

@erankor Thank you, that was a very helpful tip.

What advice can you give about cutting a part of the video without using ffmpeg (assuming that the keyframes are regular every 2 seconds)

I have read all the issues of the repository, as well as part of the code, I know that you do this by reading the atoms of a mp4 file.

But again, I didn't understand exactly how this is done, I just know that ffmpeg is very slow in doing this and it has to be done manually.

Thank you for explaining with a piece of code. Thank You.

Saeeed-B commented 4 months ago

Can we calculate how many bytes each second or each frame is and separate it into 2 seconds ?