V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chenqh
V2EX  ›  Python

关于 moviepy 裁剪视频的速度问题

  •  
  •   chenqh · 10 天前 · 730 次点击

    代码

    
    from moviepy.editor import *
    #from moviepy.editor import VideoFileClip, concatenate_videoclips
    import os
    
    src_path = r"H:\video\死神-全集 1080P 日语中字\主线\主线初现篇 2\117.mp4"
    
    
    start_str = "01:30"
    end_str = "21:40"
    def format_raw(src, to, begin, end):
        clip2 = VideoFileClip(src).subclip(begin, end)
        clip2.write_videofile(to)
        print("{}->{}".format(os.path.basename(src), os.path.basename(to)))
    
    def format(src,begin_str, end_str):
        bname_raw = os.path.basename(src_path)
        src_dir = os.path.dirname(src_path)
        bname,ext = os.path.splitext(bname_raw)
        to= os.path.join(src_dir, "{}_split{}".format(bname, ext))
        begin = str_seconds(begin_str)
        end = str_seconds(end_str)
        print("{}, {}".format(begin_str, end_str))
        print("begin:{}, second:{}".format(begin, end))
        format_raw(src, to, begin, end)
    
        
    
    def str_seconds(para):
        arr = para.split(":")    
        arr = [int(v) for v in arr]
        mul = [60, 1]
        return sum([k*v for (k, v) in zip(mul, arr)])
    
    format(src_path, "01:30", "21:54")
    
    

    目的就是为了视频开始前的开头曲,现在问题裁剪好像很慢啊,5-6 分钟还没有裁剪出来,粗略估计要 15-16 分钟.

    用 ffmpeg 裁剪也很慢,

    这个速度正常吗?感觉到了自己电脑的弱小

    10 条回复    2024-06-10 17:57:42 +08:00
    GeekGao
        1
    GeekGao  
       10 天前   ❤️ 1
    要看你电脑 cpu ,线程数越高越有优势
    yangxin0
        2
    yangxin0  
       10 天前   ❤️ 1
    要看你调用硬件 codec 没,默认大概率是 CPU 调用,速度差异非常巨大。
    chenqh
        3
    chenqh  
    OP
       10 天前
    @GeekGao CPU 占用高,电脑 CPU100,我是四核
    ysc3839
        4
    ysc3839  
       10 天前 via Android   ❤️ 1
    只是截取片段,不修改画面的话,建议看看 LosslessCut 的速度如何。
    ysc3839
        5
    ysc3839  
       10 天前 via Android
    另外只是裁剪片头的话,为什么要写代码实现?要自动批量裁剪每一集的片头?
    chenqh
        6
    chenqh  
    OP
       10 天前
    @ysc3839 是的,自动批量裁剪动画片的片头
    expy
        7
    expy  
       10 天前   ❤️ 1
    估计重新编码了,可以试试 ffmpeg 不重编码直接复制流。不过因为 I/P/B 帧类型的问题,不重编码切的时间点没那么精确。

    ffmpeg -i input.mp4 -ss 00:01:30 -to 00:21:40 -c copy output.mp4
    chenqh
        8
    chenqh  
    OP
       10 天前
    @expy 快了很多,至少是 10 分钟到 2S 的提升,太厉害了,大佬
    ysc3839
        9
    ysc3839  
       10 天前 via Android
    @chenqh 那就用 LosslessCut 的命令行或者 HTTP API 裁剪吧。
    flcwk
        10
    flcwk  
       10 天前   ❤️ 1
    import os

    src_path = r"H:\video\死神-全集 1080P 日语中字\主线\主线初现篇 2\117.mp4"

    def format(src, begin_str, end_str):
    bname_raw = os.path.basename(src)
    src_dir = os.path.dirname(src)
    bname, ext = os.path.splitext(bname_raw)
    to = os.path.join(src_dir, "{}_split{}".format(bname, ext))
    begin = str_seconds(begin_str)
    end = str_seconds(end_str)
    begin_str_ffmpeg = seconds_str(begin)
    end_str_ffmpeg = seconds_str(end)

    command = f'ffmpeg -i "{src}" -ss {begin_str_ffmpeg} -to {end_str_ffmpeg} -c copy "{to}"'
    os.system(command)
    print("{}->{}".format(os.path.basename(src), os.path.basename(to)))

    def str_seconds(para):
    arr = para.split(":")
    arr = [int(v) for v in arr]
    mul = [60, 1]
    return sum([k*v for (k, v) in zip(mul, arr)])

    def seconds_str(seconds):
    minutes = seconds // 60
    seconds = seconds % 60
    return f"{minutes:02}:{seconds:02}"

    # 调用函数进行视频裁剪
    format(src_path, "01:30", "21:54")

    # 使用 ffmpeg 的-c copy 选项,这意味着视频和音频流不会被重新编码,从而大大提高处理速度。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2983 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:06 · PVG 20:06 · LAX 05:06 · JFK 08:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.