videoconvert.py
4.65 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python
# -- coding: utf-8 --
import time, datetime,base64,os
import hashlib
class VideoUtil(object):
def __init__(self,rootpath):
self.videorootpath=rootpath
def sav_video(self,datadict):
dt =datetime.datetime.now()
videopath = self.videorootpath+'/'+dt.strftime('%Y-%m-%d')+'/'+dt.strftime('%H')+'/'+datadict['event_cate']+'/'+datadict['event_type']+'/'
outVideosPath = ''
print(videopath,'.......')
try:
if datadict.has_key('video'):
videos = datadict['video']
if videos is not None and len(videos) > 0:
for video in videos:
if video.has_key('video_base64') and len(video['video_base64']) > 0:
temppath = videopath
if os.path.exists ( videopath ) == False:
os.makedirs ( videopath )
t = int ( round ( time.time () * 1000 ) )
video_base64 = video['video_base64']
if 'ofilename' in video and len ( video['ofilename'] ) > 0:
temppath = temppath +str(t)+'.' +str(video['ofilename'].split('.')[1])
elif 'format' in video and len ( video['format'] ) > 0:
temppath = temppath +str(t)+'.' + str(video['format'])
else:
temppath = temppath + str(t) + '.mp4'
print('videopath=%s' % temppath)
#temppath = temppath.decode ( 'utf8' ).encode ( 'utf8' )
with open(temppath,'wb') as f:
#f.write ( base64.b64decode( video_base64))
f.write(video_base64)
f.close()
print(temppath, '****',type(temppath))
temppath = temppath[temppath.find ( 'www' ) + 3:len ( temppath )]
print(temppath,'****')
outVideosPath= 'http://10.169.165.217:20080%s' % temppath
del video['video_base64']
video.setdefault ( 'src_url', temppath )
elif video.has_key('unid') and len(video['unid']) > 0:
temppath = video['unid'][video['unid'].find ( 'www' ) + 3:len ( video['unid'] )]
outVideosPath = 'http://10.169.165.217:20080%s' % temppath
except Exception as e:
print('视频保存异常,异常信息为:%s' % e)
pass
return outVideosPath
def sav_videobyfile(self,video_file):
dt = datetime.datetime.now()
m = hashlib.md5()
#videopath = self.videorootpath+'/'+dt.strftime('%Y-%m-%d')+'/'+dt.strftime('%H')+'/'
videopath = self.videorootpath + '/' + dt.strftime('%Y-%m-%d') + '/'
outVideosPath = ''
try:
if os.path.exists ( videopath ) == False:
os.makedirs ( videopath )
videopath = videopath+video_file.filename
print(videopath,'*****in sav_videobyfile')
with open ( videopath, 'wb' ) as f:
for line in video_file.readlines ():
f.write ( line )
#m.update(line)
#md5code = m.hexdigest()
f.close()
file_size = os.path.getsize ( videopath )
file_size = file_size/float(1024*1024)
print("video_file size is :%.2f M" %file_size)
#video_file.save(videopath)
#with open(videopath,'wb') as f:
# for chunk in video_file.chunks ():
# f.write ( chunk )
# f.close()
# outVideosPath.append(videopath)
outVideosPath = videopath
except Exception as e:
print('视频文件上传异常,异常信息为:%s' % e)
pass
#return [outVideosPath,md5code]
return outVideosPath
if __name__ == "__main__":
videojson = {
"task_type": "normal",
"event_type": "tflow",
"task_id": "48b6357fc3384b838147006557447a01",
"event_cate": "traffic",
"dev_unid": "",
"video":[
{
"video_base64":"",
"format":"avi",
"unid":'www123456'
}
]
}
videoutil = VideoUtil(r'E:\NDR相关\upload_vido')
print(videoutil.sav_videobyfile('test.mp4'))