问题描述
我正在尝试将Django的mp4视频流加载到Vue.js.我在此处遵循了解决方案,并通过我的axios API得到了一个字节字符串,并将其连接到属性,但视频未显示.这是将视频从Django 3流传输到Vue.js的正确方法吗?我在做什么错了?
I'm trying to load a mp4 video stream from Django to Vue.js. I followed the solution here and got a byte string via my axios API which I concatenated to property, but the video is not displaying. Is this the correct way to stream videos from Django 3 to Vue.js? What am I doing wrong?
Api.js代码段:
Api.js code snippet:
import axios from 'axios'
export default () => {
return axios.create({
baseURL: `http://127.0.0.1:8000/`,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
}
Video.vue组件代码段:
Video.vue component code snippet:
methods: {
async stream() {
return await VideoService.stream(this.video)
.then(function(data) {
return data.data;
});
}
},
props: ['data'],
watch: {
data: {
deep: true,
immediate: true,
handler(curr, prev) {
this.video = curr;
this.stream().then(data => {
data = Buffer.from(data, 'binary').toString('base64');
this.video = 'data:video/mp4;base64,'+data;
});
}
}
}
正如该答案的注释中所建议的,我还将src
设置为Django api端点,例如src="/video/test.mp4
,但是我可以在终端中看到它没有到达该Django路由.如何获得链接的解决方案以与Vue.js一起使用?
As suggested in the comments of that answer, I also set src
to the Django api endpoint e.g. src="/video/test.mp4
, but I can see in my terminal that it's not reaching that Django route. How do I get the linked solution to work with Vue.js?
我从Kevin的Django视图中获得的原始字符串的图片:
Picture of the raw string I get back from Kevin's Django view:
当我使用Buffer转换为base 64时的另一幅图像:
Another image when I convert to base 64 using Buffer:
推荐答案
如果您对视频使用相同的组件,则将视频流式传输到Vue.js所需的最终解决方案是使用v-html
设置src
标签.这样您就可以动态设置src
.创建组件后,直接执行src="http://localhost:8000/v/video.mp4"
将不起作用.因此,除了Kevin的代码外,只需在视频组件中执行以下操作即可:
The final solution needed to stream videos to Vue.js if you're using the same component for videos is to set the src
tag using v-html
so that you can set src
dynamically. Directly doing src="http://localhost:8000/v/video.mp4"
won't work once the component is created. So in addition to Kevin's code, just do the following in the video component:
<template>
<div>
<video v-html="src" autoplay="" controls="" loop="" muted="" frameborder="0">
Your browser does not support HTML videos.
</video>
</div>
</template>
<script>
export default {
data() {
return {
src: ''
}
},
props: ['data'],
watch: {
data: {
deep: true,
immediate: true,
handler(curr, prev) {
this.src = '<source src="http://localhost:8000/v/"'+curr+' type="video/mp4">'
}
}
}
}
</script>
<style scoped>
</style>
这篇关于如何将mp4视频从Django 3流传输到Vue.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!