此处需要和httpfileserver相结合,httpfileserver的安装可自行百度安装。
#-*- coding:utf-8 -*-
import dominate
from dominate.tags import *
from dominate.util import raw
import os
import sys
reload(sys)
sys.setdefaultencoding('gbk')
class HTML:
def __init__(self, web_dir, title, reflesh=0):
self.title = title
self.web_dir = web_dir
self.img_dir = os.path.join(self.web_dir, 'images')
'''
if not os.path.exists(self.web_dir):
os.makedirs(self.web_dir)
if not os.path.exists(self.img_dir):
os.makedirs(self.img_dir)
'''
#print(self.img_dir)
self.doc = dominate.document(title=title)
if reflesh > 0:
with self.doc.head:
meta(http_equiv="reflesh", content=str(reflesh))
def get_image_dir(self):
return self.img_dir
def add_header(self, str):
with self.doc:
h3(str)
def add_table(self, border=1):
self.t = table(border=border, style="table-layout: fixed;")
self.doc.add(self.t)
def add_images(self, ims, width=200):
self.add_table()
with self.t:
with tr():
for im in ims:
with td(style="word-wrap: break-word;", halign="center", valign="top"):
with p():
#with a(href=os.path.join('images', link)):
# img(style="width:%dpx" % width, src=os.path.join('images', im))
#print link
with a(href= im):
print 'tttttttt:', im
#print width
img(style="width:%dpx" % width, src=im)
br()
p(im)
def add_videos(self, vds, width=200):
self.add_table()
with self.t:
with tr():
print(vds)
for vd in vds:
with td(style="word-wrap: break-word;", halign="center", valign="top"):
with p():
with a(href=vd):
video(controls="controls", width=width, src=vd)
br()
#txt, ext = os.path.splitext(vd)
p(vd)
filename = vd.replace('mp4','txt')
rsturl = 'http://10.2.126.8:8065/recall0320/'+ filename
td(raw('<a href="%s">onlinerst</a>'%(rsturl)))
def save(self):
html_file = '%s/index.html' % self.web_dir
f = open(html_file, 'wt')
f.write(self.doc.render())
f.close()
def isImage(str):
if str =='jpg' or str =='jpeg' or str =='png' or str=='JPG' or str =='JPEG':
return True
else:
return False
if __name__ == '__main__':
data_path = './0608/' #视频文件夹
i = 0
for root, dirs, files in os.walk(data_path):
#for file in files:
#i +=1
#if i ==1:
# continue
html = HTML(root, 'Experiment = test')
ims = []
txts = []
links = []
for file in files:
#imgpath = os.path.join(root,file)
#img_path = imgpath.replace(data_path, '')
if file =='index.html':
continue
ims.append(file)
step = 4
in_ims = [ims[i:i+step] for i in range(0,len(ims),step)]
for im in in_ims:
html.add_videos(im)
html.save()
#html = HTML(data_path, 'Experiment = test')
'''
folder_list = os.listdir(data_path)
for folder in folder_list:
folder_path = os.path.join(data_path,folder)
#print folder_path
if not os.path.isdir(folder_path):
continue
#html = HTML(folder_path, 'Experiment = test')
if os.path.isdir(folder_path):
video_list = os.listdir(folder_path)
for video in video_list:
video_path = os.path.join(folder_path,video)
if not os.path.isdir(video_path):
continue
img_list = os.listdir(video_path)
html = HTML(video_path, 'Experiment = test')
ims = []
txts = []
links = []
for img_name in img_list:
img_path = os.path.join(video_path,img_name)
print img_path
if not isImage(img_name[img_name.rindex('.')+1:]):
#if img_name[img_name.rindex('.')+1:] !='jpg':
continue
#print img_path
img_path = img_path.replace(data_path, '')
ims.append(img_path)
step = 6
in_ims = [ims[i:i+step] for i in range(0,len(ims),step)]
for im in in_ims:
html.add_images(im)
html.save()
#break
#break
'''
1.输入视频路径后,执行代码。会在输入的路径下,生成index.html的文件。
2.打开终端,查看当前机器的ip地址。
3.找到视频路径所在目录,之后在终端打开httpfileserver,比如输入:httpfileserver 8008,打开8008端口(前提这个端口没被使用)。
4.然后在浏览器中输入ip:8008端口之后,就能够以网页的形式查看到视频文件夹中的视频了。