å¦ä½ç¨ææºççµèä¸çè§é¢ååå¹
éæ±èæ¯
尽管ç°å¨ç¨ææºççæ´æ¹ä¾¿ï¼ä½æ å¥ä¸çè¿æ¯ç¨ççµèï¼èä¸ææºä¹ä¸å¯è½å个å ç¾ä¸ªGççå§ï¼æ以æ好å 次é½è¯å¾æ¾ä¸ä¸ªç®åçç¨ææºççµèä¸çè§é¢çæ¹æ³ï¼è¯è¿å ±äº«æ件夹ï¼ESæµè§å¨ï¼windowsèªå¸¦çmediaplayerä¹ç±»çæ¹æ¡ãè½ç¶æåå强è½ççäºï¼ä½æ¯ä¸æ¹é¢ï¼æè§å¾è®¾ç½®èµ·æ¥å¾éº»ç¦ï¼å¦ä¸æ¹é¢ï¼è¿äºæ¹æ¡é½ä¸æ¯æå¤æ¥çASS/SSA/SRTåå¹ææ¾ã
å æ¤ï¼å¨æ åå¹å¼ºè¡åçèå¤æ¬¡ä¹åããæç»äºå³å®èªå·±å®ç°ä¸ä¸ªè½å¸¦åå¹ææ¾åªä½æ件çæ件æµè§å¨ã
æç»å®ç°
ä¸ä¸ªæ¬æºè¿è¡çæ件æå¡å¨(ç±pythonèæ¬æå
èæçEXEå¯æ§è¡ç¨åº) + éè¿ææºæµè§å¨æ¥çã
æ¯æèªå¨å è½½è§é¢æ件ååSRT/SSA/ASSåå¹ã
æ¯ææ«äºç»´ç å¿«é访é®ã
ä¼å¿
ç®å(åå»æå¼å°±è½è¿è¡ï¼ä¸ç¨ä»»ä½è®¾ç½®) èªå¨å è½½åå¹
å¦å¤è¿ä¸ªæ件æµè§å¨ä¹é¡ºä¾¿è®©ä¸å设å¤ä¼ æ件åçç®åäºï¼ä»¥åæè¿å¾å¼ä¸ªå¾®ä¿¡æä»¶ä¼ è¾å©æä¼ æ¥ä¼ å»ï¼é度æ
¢è¿æ100Mç大å°éå¶
å®ç°æµç¨
é¦å ç¡®å®ç½ç»æå¡å¨æ¡æ¶ä»£ç ï¼è¿éæ使ç¨pythonçflaskæ¡æ¶ãå¦ææ¯è§é¢æ件ï¼è·³è½¬ææ¾é¡µï¼å¦ææ¯æ件夹ï¼åæ¾ç¤ºæ件å表ï¼å¦ææ¯æ®éæ件ï¼åç´æ¥æä¾ä¸è½½ã
ç¶åæ¯åå¹è½¬æ¢é®é¢ï¼ç±äºåççhtmlåå¹åªæ¯ævttæ ¼å¼ï¼æ以æéè¦æASSãSRTåå¹é½è½¬ævttæ ¼å¼ï¼è¯éªäºä¸äºä¸æ¹ä¾èµä¹åæéæ©äº webvttåasstosrt è¿ä¸¤ä¸ªåºãåè æ¯æASSåSSAåå¹è½¬SRTã
æåï¼ç»åºæºä»£ç åå°è£
好çEXEæ件å°åï¼æºç åEXEä¸è½½å°åï¼githubï¼
å 为githubå¯è½æä¸å¼ï¼æ以ä¹ä¼ äºä¸ä»½ç¾åº¦äº
é¾æ¥: https://pan.baidu.com/s/11EIkdlbJfu2k-aAj3XWv6g æåç : n8tj
épythonæºç ,åçå¾ä¸éï¼ä»
ä¾åèãå¦ææpythonç¯å¢çå¯ä»¥å®è£
ç¸å
³ä¾èµåä»èæ¬è¿è¡ã
ç¥å¤§å®¶æ¸çå¼å¿ãããã
# -*- coding: utf-8 -*-
from flask import Flask, send_from_directory, send_file
from gevent import monkey
from gevent.pywsgi import WSGIServer
import os
import urllib
import sys
import html
import os
import webvtt
import asstosrt
import chardet
from tkinter import *
from tkinter import filedialog
import locale
import time
monkey.patch_all()
from flask_cors import *
app = Flask(__name__)
CORS(app, supports_credentials=True)
WORK_DIRECTORY = '/'
SEP = '/'
lang = locale.getdefaultlocale()[0]
IS_ZH = 'CN' in lang
BACK = "è¿åé¦é¡µ" if IS_ZH else 'back to index'
NAME = 'ä»ä»æä»¶ä¼ è¾å©æ' if IS_ZH else "ZZ file explorer"
@app.route('/')
def hello_world():
return get_list_page(WORK_DIRECTORY)
@app.route("/static/<path:path>", methods=["GET"])
def static_dir(path):
return send_from_directory('./static/', path)
@app.route("/file/<path:path>", methods=["GET"])
def get_file(path):
print('getfile', path)
suffix = path.split(".")
suffix = suffix[-1] if suffix else ""
path = urllib.parse.unquote(path)
path = path.replace("@@", SEP)
print(path)
if path.endswith(SEP): # å¤ææ¯å¦æ¯æ件夹
return get_list_page(path)
elif suffix not in ["exe", "bat"]:
path = path.replace("@@", SEP)
return send_file(path, conditional=True)
@app.route("/play/<path:path>", methods=["GET"])
def get_video_player(path):
# show the user profile for that user
print(path)
r = []
try:
displaypath = urllib.parse.unquote(path,
errors='surrogatepass')
except UnicodeDecodeError:
displaypath = urllib.parse.unquote(path)
displaypath = html.escape(displaypath, quote=False)
enc = sys.getfilesystemencoding()
title = 'Player for %s' % displaypath
r.append('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
'"http://www.w3.org/TR/html4/strict.dtd">')
r.append('<html>\n<head>')
r.append('<meta http-equiv="Content-Type" '
'content="text/html; charset=%s">' % enc)
r.append('<title>%s</title>\n</head>' % title)
# r.append('<script src="/static/player.js"></script>'
# # '<script src="/static/videosub-0.9.9.js"></script>'
# )
r.append('<body>\n<h1>%s</h1>' % title)
r.append('<li><a href="%s">%s</a></li>'
% ('/',
html.escape(BACK, quote=False)))
r.append(
'\n<p align="center"><video id="mainPlayer" controls="controls" width="640" height="480">')
caption_path = getCaption(path.replace('@@', SEP)).replace(SEP, '@@')
r.append('''
<source src="/file/%s" type="video/mp4">
<track id="video-caption" src="/file/%s"
kind="captions" srclang="zh" label="Chinese" default/>
''' % (path, caption_path))
r.append('</video></p>\n\n')
r.append('</body>\n</html>\n')
return '\n'.join(r)
def get_list_page(path):
"""Helper to produce a directory listing (absent index.html).
Return value is either a file object, or None (indicating an
error). In either case, the headers are sent, making the
interface the same as for send_head().
"""
try:
list = os.listdir(path)
except OSError as e:
print(e)
return None
list.sort(key=lambda a: a.lower())
r = []
displaypath = html.escape(path, quote=False)
enc = sys.getfilesystemencoding()
title = NAME + ' Directory listing for %s' % displaypath
r.append('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
'"http://www.w3.org/TR/html4/strict.dtd">')
r.append('<html>\n<head>')
r.append('<meta http-equiv="Content-Type" '
'content="text/html; charset=%s">' % enc)
r.append('<title>%s</title>\n</head>' % title)
r.append('<body>\n<h1>%s</h1>' % title)
r.append('<hr>\n<ul>')
if path != WORK_DIRECTORY: # 'a/b/c/' -> 'a/b/'
r.append('<li><a href="%s">%s</a></li>'
% ('/',
html.escape(BACK, quote=False)))
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
# print(name, fullname)
# Append / for directories
if os.path.isdir(fullname):
fullname = fullname + "/"
displayname = name + "/"
linkname = name + "/"
suffix = linkname.split('.')[-1]
if suffix in ['mp4', 'avi', 'mkv']:
r.append('<li><a href="%s">%s</a></li>'
% ('/play/' + urllib.parse.quote(fullname.replace(SEP, "@@")),
html.escape(displayname, quote=False)))
else:
r.append('<li><a href="%s">%s</a></li>'
% ('/file/' + urllib.parse.quote(fullname.replace(SEP, "@@")),
html.escape(displayname, quote=False)))
r.append('</ul>\n<hr>\n</body>\n</html>\n')
return '\n'.join(r)
def getCaption(videoPath):
# æ ¹æ®videoå称寻æ¾åå¹
print('get caption',videoPath)
vtt_exist, srt_exist, ass_exist,ssa_exist = False, False, False,False
dir_path = os.path.dirname(videoPath)
videoPath = videoPath.split('/')[-1]
videoName = videoPath.split('.')[0]
print(videoName)
vtt_name = videoName + '.vtt'
srt_name = videoName + '.srt'
ass_name = videoName + '.ass'
ssa_name = videoName + '.ssa'
file_list = os.listdir(dir_path)
for file_name in file_list:
# print(file_name)
if file_name == vtt_name:
vtt_exist = True
elif file_name == srt_name:
srt_exist = True
elif file_name == ass_name:
ass_exist = True
elif file_name == ssa_name:
ssa_exist = True
if vtt_exist:
return os.path.join(dir_path, vtt_name)
elif srt_exist:
return srt2vtt(srt_name, dir_path)
elif ass_exist:
return ass2vtt(videoName, dir_path,ass_name)
elif ssa_exist:
return ass2vtt(videoName, dir_path,ssa_name)
else:
return ''
def srt2vtt(srt_name, dir_path):
print('srt2vtt',srt_name)
srt_path = os.path.join(dir_path, srt_name)
with open(file=srt_path, mode='rb') as f3: # 以äºè¿å¶æ¨¡å¼è¯»åæ件
data = f3.read() # è·åæ件å
容
# print(data)
f3.close() # å
³éæ件
origin_charset = chardet.detect(data)['encoding'] # æ£æµæ件å
容
print(origin_charset)
convert_webvtt = webvtt.from_srt(srt_path)
convert_webvtt.save()
return convert_webvtt.file
def removeIfExists(file_path):
if (os.path.exists(file_path)):
os.remove(file_path)
def ass2vtt(video_name, dir_path,ass_name):
# ç¼ç è½¬æ¢ ç¶åç¨pysub
ass_path = os.path.join(dir_path, ass_name)
print(ass_path)
with open(file=ass_path, mode='rb') as f3: # 以äºè¿å¶æ¨¡å¼è¯»åæ件
data = f3.read() # è·åæ件å
容
# print(data)
f3.close() # å
³éæ件
origin_charset = chardet.detect(data)['encoding'] # æ£æµæ件å
容
print(origin_charset)
tmp_path = ass_path
tmp_ass_path = os.path.join(dir_path, 'tmp.ass')
if origin_charset != 'utf8':
tmp_path = tmp_ass_path
with open(tmp_path, 'wb') as tmp_file:
data = data.decode(origin_charset).encode('utf8')
tmp_file.write(data)
with open(tmp_path,encoding='utf8') as f:
srt_str = asstosrt.convert(f)
print(srt_str)
removeIfExists(tmp_ass_path)
tmp_srt_path = os.path.join(dir_path, video_name + '.srt')
with open(tmp_srt_path, 'w',newline="",encoding="utf8") as f:
f.write(srt_str)
return srt2vtt(tmp_srt_path, dir_path)
def get_ip():
import socket
ip = 'æ¬æºip'
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
print(ip)
return ip
def show_qcode(url):
import qrcode
qr = qrcode.QRCode(version=2, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=10, )
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image()
img.show()
if __name__ == '__main__':
# æè
app.debug = True #代ç ä¿®æ¹äºå°±èªå¨è¿è¡
# app.run(host='0.0.0.0',port=30006,debug=True)
try:
if IS_ZH:
print("请å¨å¼¹åºæ¡ééæ©éè¦å
±äº«çç®å½")
else:
print('please select directory to share')
window = Tk()
window.withdraw() # 主çªå£éè
WORK_DIRECTORY = filedialog.askdirectory(parent=window, initialdir="/",
title='choose the share directory')
if not WORK_DIRECTORY:
print('cancel')
sys.exit(1)
url = "http://%s:30007" % get_ip()
print("å
±äº«ç®å½ " if IS_ZH else 'share directory: ', WORK_DIRECTORY)
print("æå¡å¨å¯å¨" if IS_ZH else 'server started ')
print(
'ç¨ææºæçµèæµè§å¨è®¿é® %s (ææ«æçæçäºç»´ç )å³å¯è®¿é®å
±äº«ç®å½å
容' % url if IS_ZH else
'open link %s with browser on PC/mobile (or just scan the QR code),to xisit your content' % url)
show_qcode(url)
WSGIServer(('0.0.0.0', 30007), app).serve_forever()
except Exception as e:
print(e)
time.sleep(10)
上一篇: 使用手机或电脑远程连接windows自带的远程桌面
下一篇: centos6上安装openvz