-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideoCls.py
More file actions
72 lines (64 loc) · 1.86 KB
/
videoCls.py
File metadata and controls
72 lines (64 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import os
from sys import argv
import numpy
import cv2
from imageCls import MediaFile
import loggerFct as log
videoExtensions =( 'avi', 'mp4', 'mov' )
class VideoFile (MediaFile):
def __init__ (self, video=None):
MediaFile.__init__ (self)
self.frames =[]
self.nbFrames =0
self.rangeFrames =[]
self.width =0
self.height =0
if video:
self.path = video
self.fromPath()
def test (self):
self.extension = 'avi'
def toNb (self):
for f in self.rangeFrames: self.frames[f] = cv2.cvtColor (self.frames[f], cv2.COLOR_BGR2GRAY)
self.title = self.title +' nb'
self.toPath()
def open (self):
self.toPath()
if not os.path.exists (self.path): return
videoCap = cv2.VideoCapture (self.path)
if not videoCap.isOpened():
print ("impossible d'ouvrir la vidéo", self.path)
return
self.width = int (videoCap.get (cv2.CAP_PROP_FRAME_WIDTH))
self.height = int (videoCap.get (cv2.CAP_PROP_FRAME_HEIGHT))
# Capture frame-by-frame
ret, frame = videoCap.read()
while ret:
# if frame is read correctly ret is True
self.frames.append (frame)
ret, frame = videoCap.read()
self.nbFrames = len (self.frames)
self.rangeFrames = range (self.nbFrames)
def draw (self):
isDrawable = MediaFile.draw (self)
if isDrawable:
"""
fourcc = None
if self.extension == 'avi': fourcc = cv2.VideoWriter_fourcc (*'DIVX')
else: fourcc = cv2.VideoWriter_fourcc (*'avc1')
fourcc = cv2.VideoWriter_fourcc (*'DIVX')
"""
fourcc = cv2.VideoWriter_fourcc ('M', 'J', 'P', 'G')
log.logMsg (self.path)
video = cv2.VideoWriter (self.path, fourcc, 30, (self.width, self.height))
for frame in self.frames: video.write (frame)
cv2.destroyAllWindows()
video.release()
if len (argv) <2: print ('veuillez entrer un fichier vidéo')
else:
video = VideoFile (argv[1])
video.open()
video.test()
video.draw()