【发布时间】:2010-12-18 20:37:43
【问题描述】:
上传到运行 python 的 webapp 后,我需要为视频文件创建缩略图。
我将如何处理...
【问题讨论】:
-
请参阅 [this answer][1] 了解如何使用 Gstreamer 和 Python 进行操作。 [1]:stackoverflow.com/a/16478342/1049318
标签: python video thumbnails
上传到运行 python 的 webapp 后,我需要为视频文件创建缩略图。
我将如何处理...
【问题讨论】:
标签: python video thumbnails
查看PythonMagick,这是 ImageMagick 的 Python 接口。那应该有你需要的。 (免责声明:我以前没有使用过 Python 接口,但我知道 ImageMagick 是个好东西。)
【讨论】:
【讨论】:
您可以使用 Youtube API 进行存储和转码,并免费获取提要缩略图。老实说,这是处理在线视频的最简单方法,而且我不只是在向 3rd 方服务先令,我对该 API 和内部视频路径的使用非常满意,感谢它,我能够删除内部视频路径。
【讨论】:
您可以使用ffvideo
from ffvideo import VideoStream
pil_image = VideoStream('0.flv').get_frame_at_sec(5).image()
pil_image.save('frame5sec.jpeg')
【讨论】:
import cv2
vcap = cv2.VideoCapture(filename)
res, im_ar = vcap.read()
while im_ar.mean() < threshold and res:
res, im_ar = vcap.read()
im_ar = cv2.resize(im_ar, (thumb_width, thumb_height), 0, 0, cv2.INTER_LINEAR)
#to save we have two options
#1) save on a file
cv2.imwrite(save_on_filename, im_ar)
#2)save on a buffer for direct transmission
res, thumb_buf = cv2.imencode('.png', im_ar)
# '.jpeg' etc are permitted
#get the bytes content
bt = thumb_buf.tostring()
“阈值”是一个整数。当您获得一个视频帧时,它可以是非常黑色、白色等以获得一些好的缩略图,您可以指定帧中所有像素的平均值。
【讨论】:
我无法在 OSX Sierra 上安装 ffvideo,所以我决定使用 ffmpeg。
OSX:
brew install ffmpeg
Linux:
apt-get install ffmpeg
Python 3 代码:
import subprocess
video_input_path = '/your/video.mp4'
img_output_path = '/your/image.jpg'
subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', '-vframes', '1', img_output_path])
【讨论】:
您可以使用在 GitHub 上找到的 Python 脚本 pyvideothumbnailer,该脚本使用 MoviePy、MediaInfo 和 PIL fork Pillow。它应该为您完成全部工作。
【讨论】:
使用 pyffmpeg
安装
pip install pyffmpeg
使用
from pyffmpeg import FFmpeg
inf = 'vid.mp4'
outf = 'thumb.jpg'
ff = FFmpeg()
ff.convert(inf, outf)
【讨论】: