- 1、mp4地址加密為blob鏈接在html5的video標簽展示
PHP:
1 $file_path = "...mp4"; //視頻文件地址 2 ob_end_clean(); 3 ob_start(); 4 //打開文件 5 $handler = fopen($file_path, 'rb'); 6 $file_size = filesize($file_path); 7 //聲明頭信息 8 Header("Content-type: application/octet-stream"); 9 Header("Accept-Ranges: bytes"); 10 Header("Accept-Length: ".$file_size); 11 Header("Content-Disposition: attachment; filename=" . basename( $file_path)); 12 // 輸出文件內容 13 $oct_data = fread($handler,$file_size); 14 fclose($handler); 15 ob_end_flush(); 16 17 return $oct_data;
JS:
1 //創建XMLHttpRequest對象 2 var xhr = new XMLHttpRequest(); 3 var url ="";//服務端地址 4 //配置請求方式、請求地址以及是否同步 5 xhr.open('POST', url, true); 6 //設置請求結果類型為blob 7 xhr.responseType = 'blob'; 8 //請求成功回調函數 9 xhr.onload = function (e) { 10 if (this.status == 200) {//請求成功 11 var video = document.getElementById('video_player'); 12 //獲取blob對象地址,並把值賦給容器 13 video.onload = function(e) { 14 window.URL.revokeObjectURL(video.src); 15 }; 16 video.src = window.URL.createObjectURL(this.response); 17 video.play(); 18 } 19 }; 20 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 21 xhr.send('id='+id);//傳遞參數
- 2、用FFMpeg將視頻轉為m3u8格式
JS(調用hls.js):
1 var url ="...m3u8";//m3u8地址 2 var video = document.getElementById('video_player'); 3 if(Hls.isSupported()) { 4 var hls = new Hls(); 5 hls.loadSource(url); 6 hls.attachMedia(video); 7 hls.on(Hls.Events.MANIFEST_PARSED,function() { 8 video.play(); 9 }); 10 } 11 else if (video.canPlayType('application/vnd.apple.mpegurl')) { 12 video.src = url; 13 video.addEventListener('loadedmetadata',function() { 14 video.play(); 15 }); 16 }
- 3、m3u8地址加密為blob鏈接
。。。待更新