简体   繁体   English

如何使用 ffmpeg-python 将视频剪切到一定长度并添加介绍视频?

[英]How to cut a video to a certain length and add an intro video to it using ffmpeg-python?

How to cut a video to a certain length and add an intro video to it using ffmpeg-python?如何使用 ffmpeg-python 将视频剪切到一定长度并添加介绍视频?

What i am doing is:我正在做的是:

        intro = ffmpeg.input(intro)
        mainvid = ffmpeg.input(mainvid)

        v1 = intro.video
        a1 = intro.audio
        v2 = mainvid.video
        a2 = mainvid.audio

        joined = ffmpeg.concat(v1, a1, v2, a2, v=1, a=1).node
        v3 = joined[0]
        a3 = joined[1]

        (
            ffmpeg
            .output(
                v3,
                a3,
                'out.mkv', 
                vcodec='libx265', )
            .run()
        )

But i dont know how to cut mainvid to a certain length like 10 mins before joining, i know ss will help but dont know how to use it for only mainvid.但我不知道如何在加入前将 mainvid 切割成一定长度,例如 10 分钟,我知道 ss 会有所帮助,但不知道如何仅将其用于 mainvid。

You may use trim and atrim filters for trimming the video and the audio.您可以使用trimatrim过滤器来修剪视频和音频。
The suggested solution is based on the following answer .建议的解决方案基于以下答案

  • FFmpeg supports 3 types of concatenation: "Concat demuxer", "Concat protocol" and "Concat filter". FFmpeg 支持3 种连接类型:“Concat demuxer”、“Concat protocol”和“Concat filter”。
    The method ffmpeg.concat applies Concat filter .方法ffmpeg.concat应用Concat filter
    Since "Concat filter" is used, a practical solution is trimming the video and audio by "chaining" trim and atrim filters.由于使用了“Concat filter”,一个实用的解决方案是通过“链接” trimatrim过滤器来修剪视频和音频。
  • setpts and asetpts filters are required for fixing the timestamps.修复时间戳需要setptsasetpts过滤器。

Replace v2 = mainvid.video and a2 = mainvid.audio with:v2 = mainvid.videoa2 = mainvid.audio为:

v2 = mainvid.video.filter('trim', start=0, end=600).filter('setpts', 'PTS-STARTPTS')
a2 = mainvid.audio.filter('atrim', start=0, end=600).filter('asetpts', 'PTS-STARTPTS')

I managed to do it using:我设法使用:

        intro = ffmpeg.input(intro)
        mainvid = ffmpeg.input(mainvid, ss='00:00:00', t='00:10:00') 

        v1 = intro.video
        a1 = intro.audio
        v2 = mainvid.video
        a2 = mainvid.audio

        joined = ffmpeg.concat(v1, a1, v2, a2, v=1, a=1).node
        v3 = joined[0]
        a3 = joined[1]

        (
            ffmpeg
            .output(
                v3,
                a3,
                'out.mkv', 
                vcodec='libx265', )
            .run()
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将填充颜色添加到 none 或 alpha 0,在使用 ffmpeg-python 时返回渲染视频中的绿色背景 - Add the fillcolor to none or alpha 0, return the green background in the rendered video while using ffmpeg-python 如何在ffmpeg-python中合并视频和音频文件 - How to combine The video and audio files in ffmpeg-python 如何使用 ffmpeg-python 从视频中获取第一个音频通道? - How to get the first audio channel from a video with ffmpeg-python? 合并音频和视频ffmpeg-python时出错 - Error merging audio and video ffmpeg-python 使用 ffmpeg-python 将视频分割成图像 - Split video into images with ffmpeg-python 如何使用此 ffmpeg python 脚本正确剪切视频 - How to cut video properly with this ffmpeg python script ffmpeg-python:测试视频剪辑是否有音频 - ffmpeg-python: Test if video clip has audio 如何在使用ffmpeg-python时预览ffmpeg命令行 - How to preview the ffmpeg command line while using ffmpeg-python ffmpeg-python 提取特定视频 stream,更改其 FPS 并将其嵌入回视频中 - ffmpeg-python extract a specific video stream, change its FPS and embed it back in the video 如何从 a.yuv 视频文件中提取某些帧并使用 FFmpeg、OpenCV 和 python 创建新视频? - How to extract certain frames from a .yuv video file and create a new video using FFmpeg, OpenCV and python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM