为小视频编程生成缩略图

2024-09-29 23:32:39 发布

您现在位置:Python中文网/ 问答频道 /正文

python如何为视频制作一个简单的cap。在

Ideia:拍9张快照,拍9张甚至放在时间轴上的照片,然后用JGP替换

我该怎么做?在

皮尔是唯一的方法吗?(做我想做的事情不容易,对吧?)在

难道没有任何模块(python似乎有很好的模型来处理几乎所有的事情,或者我运气不好)

示例:

Movie lenght = 10 min
T1= snapshot of 1 min
T2= snapshot of 2 min
......
T9= snapshot of 9 min
   |   |
 T1| T2| T3
---+---+---
 T4| T5| T6
---+---+---
 T7| T8| T9
   |   |

Tags: of方法视频snapshotmin事情快照照片
1条回答
网友
1楼 · 发布于 2024-09-29 23:32:39

这是一个快速的改变,我已经做了一段时间,使用ffmpeg帧提取和PIL创建完整的拇指图像。在

import os, sys
from PIL import Image

# Use "ffmpeg -i <videofile>" to get total length by parsing the error message
chout, chin, cherr = os.popen3("ffmpeg -i %s" % sys.argv[1])
out = cherr.read()
dp = out.index("Duration: ")
duration = out[dp+10:dp+out[dp:].index(",")]
hh, mm, ss = map(float, duration.split(":"))
total = (hh*60 + mm)*60 + ss

# Use "ffmpeg -i <videofile> -ss <start> frame<nn>.png" to extract 9 frames
for i in xrange(9):
    t = (i + 1) * total / 10
    os.system("ffmpeg -i %s -ss %0.3fs frame%i.png" % (sys.argv[1], t, i))

# Make a full 3x3 image by pasting the snapshots
full = None
for y in xrange(3):
    for x in xrange(3):
        img = Image.open("frame%i.png" % (y*3+x))
        w, h = img.size
        if full is None:
            full = Image.new("RGB", (w*3, h*3))
        full.paste(img, (x*w, y*h))

# Save result
full.save("thumbs.png")

相关问题 更多 >

    热门问题