vue+videojs 动态切换 flv 视频流
最编程
2024-04-09 18:45:09
...
<script setup>
import { onMounted, onUnmounted, ref, watch } from 'vue'
import videojs from 'video.js'
import 'videojs-flvjs-es6'
import 'video.js/dist/video-js.css'
let myPlayer = null
const props = defineProps({
videoUrl: {
default: ''
}
})
watch(
() => props.videoUrl,
url => {
if (myPlayer) {
myPlayer.dispose()
myPlayer = null
createDom()
setTimeout(() => {
url && init()
}, 100)
} else {
url && init()
}
}
)
onMounted(() => {
props.videoUrl && init()
})
function init() {
myPlayer = videojs(
document.getElementById('videoPlayer'),
{
autoplay: true, //自动播放,由于谷歌浏览器限制需设置静音muted: true,自动播放才会生效
controls: true, //用户可以与之交互的控件
loop: true, //视频一结束就重新开始
muted: true, //默认情况下将使所有音频静音
techOrder: ['html5', 'flvjs'], // 兼容顺序
flvjs: {
mediaDataSource: {
isLive: false,
cors: true,
withCredentials: false
}
},
sources: [
{
// type: 'video/rtsp'
// type: 'rtmp/flv'
src: props.videoUrl,
type: 'video/x-flv'
}
]
},
() => {
myPlayer.log('play.....')
}
)
}
function createDom() {
const newVideoElement = document.createElement('video')
newVideoElement.id = 'videoPlayer'
newVideoElement.style.width = '100%'
newVideoElement.style.height = '100%'
newVideoElement.className = 'video-js vjs-default-skin vjs-big-play-centered'
newVideoElement.preload = 'auto'
const oldVideoElement = document.getElementById('videoDom')
oldVideoElement.appendChild(newVideoElement)
}
onUnmounted(() => {
if (myPlayer) {
myPlayer.dispose()
}
})
</script>
<template>
<div class="videoContainer" id="videoDom">
<video id="videoPlayer" class="video-js vjs-default-skin vjs-big-play-centered" preload="auto"></video>
</div>
</template>
<style lang="scss" scoped>
.videoContainer {
width: 100%;
height: 100%;
.video-js {
width: 100%;
height: 100% !important;
&.vjs-fluid {
padding-top: 0 !important;
}
}
}
</style>
推荐阅读
-
vue+videojs 动态切换 flv 视频流
-
腾讯视频直播 02-推流-美颜滤镜 同样,腾讯云提供了 setBeautyFilter 方法来设置美颜风格、磨皮程度、美白程度和泛红程度 //style 磨皮风格:0:平滑 1:自然 2:朦胧 //美容级别:0-9。值为 0 时关闭美颜效果。默认值:0,关闭美颜效果。 //美白级别:取值 0-9。值为 0 时,将关闭美白效果。默认值:0,关闭美白效果。 //ruddyLevel:取值范围为 0-9。值为 0 时关闭美白效果。默认值:0,关闭美白效果。 public boolean setBeautyFilter(int style, int beautyLevel, int whiteningLevel, int ruddyLevel);; public boolean setBeautyFilter(int style, int beautyLevel, int whiteningLevel, int ruddyLevel) 滤镜 setFilter 方法可以设置滤镜效果,滤镜本身是一个直方图文件。setSpecialRatio 方法可以设置滤镜的程度,从 0 到 1,越大滤镜效果越明显,默认值为 0.5。 Bitmap bitmap = BitmapUtils.decodeResource(getResources, R.drawable.langman); if (mLivePusher) if (mLivePusher ! = null) { mLivePusher.setFilter(bmp); } 控制摄像头 腾讯云 sdk 默认为前置摄像头(可以通过修改 TXLivePushConfig 的配置函数 setFrontCamera 来修改默认值),调用一次 switchCamera 就切换一次,注意切换摄像头前要确保 TXLivePushConfig 和 TXLivePusher 对象已经初始化。 mLivePushConfig.setFrontCamera(true); // 默认前置摄像头。 mLivePusher.switchCamera; //切换摄像头。 ⑦ 设置徽标水印 腾讯视频云目前支持两种设置水印的方式:一种是在流媒体 SDK 中设置水印,原理是在 SDK 中对视频进行编码前在画面中设置水印。另一种方式是在云端设置水印,即由云端解析视频并添加水印标识。 建议使用 SDK 添加水印,因为在云端添加水印会有问题。下面是添加水印的 SDK 介绍: //设置视频水印 mLivePushConfig.setWatermark(BitmapFactory.decodeResource(getResources,R.drawable.watermark), 10, 10); // 最后两个参数是视频的水印。 //最后两个参数是水印位置的 X 轴和 Y 轴坐标。 mLivePusher.setConfig(mLivePushConfig); 如果需要对水印图像的位置进行模型适配,则需要调用水印规范化接口。 /设置视频水印 mLivePushConfig.setWatermark(mBitmap, 0.02f, 0.05f, 0.2f); //参数为水印图像。 //参数包括水印图像的位图、水印位置的 X 轴坐标、水印位置的 Y 轴坐标和水印宽度。后三个参数的范围是 [0,1]。 // 最后两个参数是水印位置的 X 轴坐标和 Y 轴坐标。 mLivePusher.setConfig(mLivePushConfig); TXLivePushConfig 中的 setHardwareAcceleration 方法可以启用或禁用硬件编码。 if (mHWVideoEncode){ if (mLivePushConfig ! = null) { if (Build.VERSION.SDK_INT < 18){ Toast.makeText(getApplicationContext, "Hardware acceleration failed, current phone API level is too low (min 18)"、 Toast.LENGTH_SHORT).show; mHWVideoEncode = false; } } } } mLivePushConfig.setHardwareAcceleration(mHWVideoEncode ? TXLiveConstants.ENCODE_VIDEO_HARDWARE : TXLiveConstants.ENCODE_VIDEO_SOFTWARE); mLivePusher.setConfig(mLivePushConfig); // 如果您不确定何时启用硬件加速,建议将其设置为 ENCODE_VIDEO_AUTO。 // 默认情况下启用软件编码,但如果手机的 CPU 使用率超过 80% 或帧速率为 10,SDK 将自动切换到硬件编码。 ⑨ 后台推流 在常规模式下,一旦应用程序进入后台,摄像头捕捉数据的能力就会被 Android 禁用,这意味着 SDK 无法继续捕捉和编码音频和视频数据。如果我们什么都不做,故事就会按照下面的脚本发展: 阶段 1(背景剪切后 10 秒 ->)- CDN 无法将视频流传输给观众,因为没有数据,观众看到的是主帧。 阶段 2(10 秒-> 70 秒)--观众一方的播放器因无法接收到直播流而退出,房间里空无一人。 第 3 阶段(70 秒后)--服务器直接断开了推送流媒体的 RTMP 链接,主播需要重新打开直播才能继续。 主播可能只是短暂地接了一个紧急电话,但各云提供商的安全措施会迫使主播的直播提前结束。 1) 设置 setPauseFlag 在开始推流之前,使用 TXLivePushConfig 的 setPauseImg 接口设置一个等待图像,其含义建议为 "主播将暂时离开,稍后再回来"。
-
如何在Windows上轻松设置和运用ZLMediaKit开源流媒体服务器,实现实时从摄像头RTSP视频流中按需拉取,并通过HTTP-FLV网页实现流畅播放
-
实战JavaCV:切割并转换视频流为HLS播放,支持直播、回放和多种格式如MP4、RTSP、RTMP和FLV
-
【Vue前端】使用 videojs 做 hls 直播流遇到的问题及解决方案总结(销毁、反复加载视频流、http-flv 低延时优化解决方向)
-
手机直播PC直播RTMP推流服务器RTSP、RTMP、HLS、FLV网页无插件直播流媒体服务器EasyDSS之如何搭建一套*灵活的音视频流媒体直播平台?