使用python 2.7将视频转换为图片的问题 [英] Problems converting video to pictures using python 2.7

查看:42
本文介绍了使用python 2.7将视频转换为图片的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试了很多选择,但想法不多了.我希望这里有人可以提供帮助.我正在尝试在 python 中编写一些代码,从视频(.avi 或 .wmv)中提取帧(比如每十帧)并创建图片(最好是 .jpg - 但其他格式也可以).我没有成功,想知道是否有人可以通过提供我尝试过和失败的替代方案来帮助我解决我的问题.

Tried a lot of options and am running out of ideas. I was hoping someone here could help. I am trying to write some code in python that will extract frames (say every tenth frame) from a video (.avi or .wmv) and create a picture (.jpg preferably - but other formats will do). I have had no success and was wondering if someone could assist me in solving my problem by providing an alternative to what I have tried and failed.

我尝试过 PyMedia(他们教程中的例子 http://pymedia.org/tut/src/dump_video.py.html 不起作用 - 程序在寻找视频编解码器时会爆炸):

I have tried PyMedia (the example in their tutorial http://pymedia.org/tut/src/dump_video.py.html does not work - the program bombs out when it looks for the video codecs):

#dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] )  This line does not work

  dm= muxer.Demuxer( 'avi' )  #This modified line does seem to work however

  i= 1

  inFile = "VideoTest.avi"

  f= open( inFile, 'rb' )

  s= f.read( 400000 )

  r= dm.parse( s )

  v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams )

  v_id= v[ 0 ][ 'index' ]

  print 'Assume video stream at %d index: ' % v_id

  c= vcodec.Decoder( dm.streams[ v_id ] )     #this is the point where it crashes.

我已经为 Python 尝试了 OpenCV v2.2,但这也不起作用(我可以让大部分 OpenCV 工作 - 除了我需要的一个函数 CaptureFromFile 不起作用).我相信这个功能不起作用的原因是因为在 Windows 上它需要 highGui 才能运行,并且由于某种原因,python 和 opencv 找不到 highgui 即使它在正确的目录中.我也知道 OpenCV 在查找和应用正确的视频编解码器方面存在问题,所以我不确定这是我的问题的原因.

I have tried OpenCV v2.2 for Python but that doesn't work either (I can get most of OpenCV to work - except the one function, CaptureFromFile, that I need does not work). I believe the reason this function doesn't work is because on Windows it requires highGui to operate and for some reason, python and opencv cannot find highgui eventhough it is in the correct directory. I also understand OpenCV has issues with finding and applying correct video codecs so I am not sure which is the cause of my problem.

我查看了 pyFFMPEG,但它的最后一个版本是 2.6 版,我正在运行 python 2.7.

I have looked at pyFFMPEG but the last build for that was version 2.6 and I am running python 2.7.

我在 Windows Vista 和 Windows 7 机器上运行它,将 Python 2.7 和 OpenCV 2.2 加载到 C:\ 和所有其他 python 包(pygames、pymedia、numpy、scipy)安装在 C:\python27\Libs\site-packages..."我从可执行文件下载并安装,所有包都是为 python 2.7 构建的.我的 Path 变量包括 Python27 和 OpenCV,我有一个 PYTHONPATH 变量.

I am running this on Windows Vista and Windows 7 machines, have Python 2.7 and OpenCV 2.2 loaded on C:\ and all other python packages (pygames, pymedia, numpy, scipy) installed in C:\python27\Libs\site-packages..." I downloaded and installed from executables, all packages were built for python 2.7. My Path variable includes Python27 and OpenCV and I have a PYTHONPATH variable.

感谢您的任何想法或建议.

Thanks for any ideas or recommendations.

推荐答案

我尝试了以下方法将每个帧提取为单独的图像:

I tried the following to extract each frames as a separate image:

import cv2

file_path = "some\path\to\the\file.avi"
video_object = cv2.VideoCapture(path)

success = True
while success:
    success,frame = video_object.read()
    if success:
        cv2.imwrite(path[:-4]+"_"+str(i)+".jpg",frame)
print("Done!")

此脚本将每一帧作为图像保存在与源文件相同的文件夹中.这可能不是最有效的方法,但它对我有用!我知道我的字符串解析不是最推荐的,但它有效.video_object.read() 返回两个对象.第一个是bool,表示读取操作是否成功,第二个是图像.

This script saves each frame as an image in the same folder as the source file. It may not be the most efficient way to do this, but it works for me! I know my string parsing is not the most recommended, but it works. video_object.read() returns two object. The first is a bool indicating whether the reading operation was a success or not, and the second is the image.

视频编解码器可能存在限制,我不知道.截至 2013 年 11 月 7 日,我使用 Python 2.7 和最新版本的 openCV 和 Numpy.

There may be limitations with respect to the video codec, of which I am not aware. I'm using Python 2.7 with the most recent version of openCV and Numpy as of 11/7/2013.

这篇关于使用python 2.7将视频转换为图片的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆