JavaWeb实现视频在线播放

本文介绍如何使用video.js实现视频在线播放,并支持多种格式如MP4、webm等。同时,提供了使用ffmpeg进行视频转码的方法,包括转换为MP4格式及添加水印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • video.js实现视频在线播放(支持MP4、webm、ogg三中格式)

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<%  
    String swfFilePath=session.getAttribute("document_url").toString();  
%>  
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Video.js 6.2.8</title>
	<link rel="stylesheet" href="../static/css/ttdms/video-js.css"> 	
	<script src="../static/js/ttdms/video.min.js"></script> 
	<script src="../static/js/ttdms/videojs-ie8.min.js"></script> 
	<style>
		body{background-color: #191919}
		.m{ width: 740px; height: 400px; margin-left: auto; margin-right: auto; margin-top: 100px; }
	</style>
		
		
</head>

<body>
	<div class="m">
		<video id="my-video" class="video-js" controls preload="auto" width="740" height="400"
		  poster="m.png" data-setup="{}">
			<!-- <source src="http://192.168.1.139:8080/boot-crm/upload/water.mp4" type="video/mp4"> -->
			<p class="vjs-no-js">
			  To view this video please enable JavaScript, and consider upgrading to a web browser that
			  <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
			</p>
		  </video>
		 
	</div>

</body>
 <script type="text/javascript">
		var myPlayer = videojs('my-video');
		videojs("my-video").ready(function(){
			var myPlayer = this;
			//myPlayer.src("http://192.168.1.139:8080/boot-crm/upload/water.mp4");
			myPlayer.src("<%=swfFilePath%>");
			myPlayer.play();
		});
</script>
</html>

  • ffmpeg把上传的视频格式转换为MP4格式(并添加水印)

import java.util.ArrayList;  
import java.util.List;  

public class ConvertVideo {	
    /**  
     * 视频转码 (PC端MP4) 
     * @param ffmpegPath    转码工具的存放路径 
     * @param upFilePath    用于指定要转换格式的文件,要截图的视频源文件 
     * @param codcFilePath    格式转换后的的文件保存路径 
     * @return  
     * @throws Exception  
     */    
    public static boolean exchangeToMp4(String ffmpegPath, String upFilePath, String codcFilePath) throws Exception {    
        // 创建List集合来保存转换视频文件为flv格式的命令   
        List<String> convert = new ArrayList<String>();    
        convert.add(ffmpegPath); // 添加转换工具路径    
        convert.add("-y"); // 该参数指定将覆盖已存在的文件    
        convert.add("-i");  
        convert.add(upFilePath);  
        convert.add("-c:v");  
        convert.add("libx264");  
        convert.add("-c:a");  
        convert.add("aac");  
        convert.add("-strict");  
        convert.add("-2");  
        convert.add("-pix_fmt");  
        convert.add("yuv420p");  
        convert.add("-movflags");  
        convert.add("faststart");  
        convert.add("-vf");   // 添加水印  
        //convert.add("movie=logosmall.png[wm];[in][wm]overlay=20:20[out]");  
        convert.add("movie=D\\\\:/conver/ffmpeg/tonghua.png[wm];[in][wm]overlay=10:main_h-overlay_h-10[out]");
        convert.add(codcFilePath);    
    
        boolean mark = true;    
                  
        try {    
            Process videoProcess = new ProcessBuilder(convert).redirectErrorStream(true).start();              
            new PrintStream(videoProcess.getInputStream()).start();                          
            //videoProcess.waitFor();  // 加上这句,系统会等待转换完成。不加,就会在服务器后台自行转换。  
              
        } catch (Exception e) {    
            mark = false;    
            System.out.println(e);    
            e.printStackTrace();    
        }    
        return mark;    
    }   


    
   
    public static void main(String s[]) {  
    	try {   		
			exchangeToMp4("D:\\conver\\ffmpeg\\ffmpeg.exe","E:\\updouad\\01.flv","E:\\updouad\\011213.mp4");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}



评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值