使用Django嵌入本地视频

时间:2015-01-20 05:28:04

标签: django video web

我使用Django创建一个网页,显示一个由访问者自己创建的视频片段。以下是我的代码:

from django.shortcuts import render
from django.http import HttpResponse
from django import forms
import numpy as np
from math import e
from matplotlib import pyplot as plt
from matplotlib import animation
from wsgiref.util import FileWrapper

class NumForm(forms.Form):
    n1 = forms.FloatField(label = 'Slope_Upper')
    n2 = forms.FloatField(label = 'Slope_Lower')

fig = plt.figure()
ax = plt.axes(xlim=(-3, 3), ylim=(0, 1))
ax.grid()
line, = ax.plot([], [], lw=2)

def psych(x,y,z):
    return 1/(1+e**(-1*y*(x-z)))

def init():
    line.set_data([], [])
    return line,

def animate(i, lo, up):

    x = np.linspace(-3,3,1000)
    dif = up - lo
    y = psych(x,.001*(up-lo)*i,0)
    line.set_data(x, y)
    return line,


def graph(request):
    if request.method == 'POST':
        form = NumForm(request.POST)
        if form.is_valid():
            sl_u = form.cleaned_data['n1']
            sl_l = form.cleaned_data['n2']
            anim = animation.FuncAnimation(fig, animate, init_func=init,
                           fargs=(sl_l, sl_u), frames=300, interval=20, blit=True)
            anim.save(filename='video.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
            return render(request, 'plotting/video2.html')
    else:
        form = NumForm()

    return render(request, 'plotting/retry.html', {'form': form})

&& 39; video2.html'是这样的:

<html>
    <body>
        <embed src="file:///c:/plot/video.mp4">
    </body>
</html>

当我运行本地服务器并访问网页时,我根本无法播放视频。 (播放按钮未激活。)问题是:如何在我使用Django创建的网页上播放我本地文件夹中的视频&#39; video.mp4&#39;? / p>

1 个答案:

答案 0 :(得分:0)

“控件”属性添加了视频控件,例如播放,暂停和音量。

<video width="320" height="240" controls>
  <source src="{{MEDIA_URL}}/movie.mp4" type="video/mp4">
</video>