【问题标题】:Embedding local video using Django使用 Django 嵌入本地视频
【发布时间】:2015-03-18 06:26:43
【问题描述】:

我正在使用 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})

而'video2.html'是这样的:

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

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

【问题讨论】:

  • 经验法则 - 仅相对 URL - 没有绝对 URL。确保 mp4 在项目内,并使用相对路径加载文件。另外,请使用video 标签而不是embed
  • 无论我使用绝对路径还是相对路径都不起作用...
  • ok 试试像{{MEDIA_URL}}/plot/video.mp4这样的媒体文件
  • 您的视频嵌入没有任何动态,因此无需为此标记或显示您的 Django 代码。您可能要考虑为此使用 html5。我建议您先对如何嵌入视频进行一些适当的研究。

标签: django video web


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 2017-10-06
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多