• 远程视频批量播放


    Description

    客户端显示服务端某目录下的所有视频文件,可播放。

    Solution

    后端提供两个接口,一个返回目录下的所有文件名,一个返回视频数据。

    from flask import Flask, jsonify, render_template
    from flask_cors import CORS
    import osapi
    
    app = Flask(__name__)
    
    app.config['JSON_AS_ASCII'] = False
    app.config['JSONIFY_MIMETYPE'] = "application/json;charset=utf-8"
    
    CORS(app, resources=r'/*')
    
    
    @app.route('/list/')
    def api_list():
        return jsonify(osapi.getFileList("resource"))
    
    
    @app.route('/video/<name>/')
    def api_video(name):
        return osapi.getBinaryFile("resource/%s" % name)
    
    
    if __name__ == '__main__':
        app.run(host="0.0.0.0", port=5000, debug=True)
    
    

    需要一些辅助

    import os
    def getFileList(file_dir):
        for root, dirs, files in os.walk(file_dir):
            return files
    
    
    def getBinaryFile(filename):
    	f = open(filename, "rb")
    	data = f.read()
    	f.close()
    	return data
    

    前端 JS 获取并通过修改 DOM 内容来展示

    <div id="list"> </div>
    
    <script>
        var httpRequest = new XMLHttpRequest();
        httpRequest.open('GET', 'http://10.203.172.202:5000/list/', true);
        httpRequest.send();
    
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                var json = JSON.parse(httpRequest.responseText);
                for (var item in json) {
                    var filename = json[item];
                    var tmp = document.getElementById("list").innerHTML;
                    document.getElementById("list").innerHTML += filename + "<br/><video width="320" height="240" controls><source src="http://10.203.172.202:5000/video/" + filename + "/" type="video/mp4"> </video> <br/> <br/>";
                    console.log(filename);
                }
            }
        };
    </script>
    
  • 相关阅读:
    Java里if...else语句
    Java里for循环和增强版循环;break语句:跳出循环;continue:跳转循环
    Java里while循环和do...while循环
    static 修饰符
    Java类变量(静态变量)
    Java实例变量
    Java局部变量
    Java访问实例变量和调用成员方法
    Java里import语句使用
    声明式服务调用
  • 原文地址:https://www.cnblogs.com/mollnn/p/15013845.html
Copyright © 2020-2023  润新知