#!/usr/bin/env python
# -*- coding: gbk -*-
import urllib,urllib2,sys,os
from BeautifulSoup import BeautifulSoup
import itertools,re
import cv2
import numpy as np
from PIL import Image
url_i =1
pic_num = 1
global personnum
personnum=0
pt=[]
global gimg1
global gimg2
global start
global end
global ntrackbarValue
personcapimgpath=r'F:\Proj\20150821\youku\videos\\'
cap = cv2.VideoCapture(r'F:\Proj\20150821\youku\videos\fast.flv')
length = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
imgsavename=[]
imgsavepath=[]
personnum=0
def on_mouse(event, x, y, flags, param):
# we will use the global pt and add_remove_pt
if (event==cv2.EVENT_RBUTTONDBLCLK):
# user has click, so memorize it
#pt.append((x, y))
#pt.append([x/300,x,y])
# myperson=personnum
global personnum,personcapimgpath,cap,end,start,numse,ntrackbarValue
if(start>=end):
start=end-20
if(start<0):
start=0
cv2.setTrackbarPos('start','mywindow',start)
start=cv2.getTrackbarPos('start','mywindow')
end = cv2.getTrackbarPos('end','mywindow')
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,ntrackbarValue)
err,img = cap.read()
cv2.imshow("mywindow", img)
personcapimgpath=r'F:\Proj\20150821\youku\videos\\'
personnum=personnum+1
personcapimgpath=personcapimgpath+str(personnum)+'\\'
# personnum=personnum+1
while(os.path.exists(personcapimgpath)==1):
personnum=personnum+1
personcapimgpath=r'F:\Proj\20150821\youku\videos\\'
personcapimgpath=personcapimgpath+str(personnum)+'\\'
if(os.path.exists(personcapimgpath)!=1):
os.mkdir(personcapimgpath)
numse=start
while cap.isOpened():
err,img = cap.read()
numse=numse+1
newimagename=personcapimgpath+str(numse)+'.jpg'
cv2.imwrite(newimagename,img)
if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) >= end:
break
cv2.imshow("xxxwindows", img)
k = cv2.waitKey(10) & 0xff
if k==27:
break
#gimg2=gimg1.copy()
# print pt
#if (event==cv2.EVENT_MOUSEMOVE):
# user has click, so memorize it
#char temp[16];
#sprintf(temp,"(%d,%d)",pt.x,pt.y);
#cv2.copy(gimg1,gimg2)
#gimg2=gimg1.copy()
#gimg2=gimg1.copy()#important
#temp=str(x)+','+str(y)
#print temp
#cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2
#cv2.putText(gimg2,temp, (x,y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2);
# cv2.putText(gimg2,temp, (x,y), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0, 255, 255),2)
#cv2.circle( gimg2, (x,y), 2,(255,0,0,0) ,1)
#cv2.circle( gimg2, (x,y), 2,(255,0,0,0) )
#cv2.imshow("Image", gimg2 );
#自己定义的引号格式转换函数
def _en_to_cn(str):
obj = itertools.cycle(['“','”'])
_obj = lambda x: obj.next()
return re.sub(r"['\"]",_obj,str)
if __name__ == '__main__':
#下载连续3个网页的视频
#while url_i <= 3:
# webContent = urllib2.urlopen("http://news.youku.com/focus/index/_page26716_" + str(url_i) + ".html")
# data = webContent.read()
# #利用BeautifulSoup读取视频列表网页数据
# soup = BeautifulSoup(data)
# print "-------------------------Page " + str(url_i) + "-------------------------"
# #获得相应页面的视频thumbnail和title的list
# tag_list_thumb = soup.findAll('li','v_thumb')
# tag_list = soup.findAll('li', "v_title")
# for item in tag_list:
# #通过每个thumbnail中的herf导向视频播放页面
# web_video_play = urllib2.urlopen(item.a['href'])
# data_vp = web_video_play.read()
# #利用BeautifulSoup读取视频播放网页数据
# soup_vp = BeautifulSoup(data_vp)
# #找到“下载”对应的链接
# tag_vp_list = soup_vp.findAll('a', id = 'fn_download')
# for item_vp in tag_vp_list:
# #将下载链接保存到url_dw中
# url_dw = '"' + item_vp['_href'] + '"'
# print item.a['title'] + ": " + url_dw
# #调用命令行运行iku下载视频,需将iku加入环境变量
# os.system("iku " + url_dw)
# #保存每个视频的thumbnail
# for item_thumb in tag_list_thumb:
# urllib.urlretrieve(item_thumb.img['src'], "E:\\下载视频\\thumbnails\\" + str(pic_num) + "." +
# _en_to_cn(item_thumb.img['title']) + ".jpg")
# pic_num += 1
# print "--------------------------------------------------------------"
# print "--------Page " + str(url_i) + "'s video thumbnails have been saved!"
# url_i += 1
videoCapture = cv2.VideoCapture(r'F:\Proj\20150821\youku\videos\fast.flv')
#获得码率及尺寸
#fps = videoCapture.get(cv2.cv.CV_CAP_PROP_FPS)
#size = (int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
#success, frame = videoCapture.read()
#success, frame = videoCapture.read()
#cv2.imshow("Oto Video", frame)
cv2.namedWindow('mywindow')
cv2.cv.SetMouseCallback("mywindow",on_mouse,0)
# cv2.waitKey()
#cap.set(cv2.CAP_PROP_POS_FRAMES,400)
def onChange(trackbarValue):
global start ,end,numse,ntrackbarValue
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,trackbarValue)
err,img = cap.read()
ntrackbarValue=trackbarValue
cv2.imshow("mywindow", img)
start = cv2.getTrackbarPos('start','mywindow')
end = cv2.getTrackbarPos('end','mywindow')
print start,end
# if start >= end:
#raise Exception("start must be less than end")
#start=end-20
#if(start<0):
# start=0
numse=start;
pass
cv2.createTrackbar( 'start', 'mywindow', 0, length, onChange )
cv2.createTrackbar( 'end' , 'mywindow', 100, length, onChange )
onChange(0)
cv2.waitKey()
start = cv2.getTrackbarPos('start','mywindow')
end = cv2.getTrackbarPos('end','mywindow')
print start,end
if start >= end:
raise Exception("start must be less than end")
numse=start;
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,start)
while cap.isOpened():
err,img = cap.read()
numse=numse+1
newimagename=personcapimgpath+str(numse)+'.jpg'
cv2.imwrite(newimagename,img)
if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) >= end:
break
cv2.imshow("mywindows", img)
k = cv2.waitKey(10) & 0xff
if k==27:
break
videoCapture = cv2.VideoCapture(r'F:\Proj\20150821\youku\videos\fast.flv')
#获得码率及尺寸
fps = videoCapture.get(cv2.cv.CV_CAP_PROP_FPS)
size = (int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
success, frame = videoCapture.read()
while success :
cv2.imshow("Oto Video", frame) #显示
gimg1=frame
cv2.waitKey(1000/int(fps)) #延迟
# videoWriter.write(frame) #写视频帧
success, frame = videoCapture.read() #获取下一帧