Posted on
from flask import Flask
from os import listdir
from os.path import isfile, join


def get_folder_files(folder):
    return [f for f in listdir(folder) if isfile(join(folder, f)) and f != 'app.py']


def gen_mp4_block(file_name):
    return '''
    <video width="720" controls>
        <source src="./static/{0}" type="video/mp4">
    </video>
    '''.format(file_name)


def merge_mp4_blocks(file_names):
    html = ''
    for file in file_names:
        html = html + gen_mp4_block(file)
    return html


app = Flask(__name__)


@app.route('/')
def index():
    html = '''
        <!doctype html>
        <html>
            <head>
                <title>Serve all Files</title>
            </head>
            <body>
                <h1>Serve all Files</h1>
                {0}
            </body>
        </html>
    '''.format(merge_mp4_blocks(get_folder_files('./static')))
    return html


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)

Folder Structure

project root folder
|- static
    |- video1.mp4
    |- video2.mp4
    |- ......
|- app.py

Dependencies

The following Python packages are used:

  • flask
  • os

References

  1. https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory
  2. https://fenghe.us/how-to-serve-mp4-video-with-flask/

Leave a Reply

Your email address will not be published. Required fields are marked *