java 实现视频转换通用工具类:视频加水印-Ffmpeg(五-1)

  • Post author:
  • Post category:java








java 实现视频转换通用工具类:获取视频元数据信息(一)









java 实现视频转换通用工具类:视频相互转换-Ffmpeg(三)



java 实现视频转换通用工具类:视频截图-Ffmpeg(四)




说明:


视频加水印支持右上角、左上角、左下角、右下角、底部居中几个方位

1.根据水印文字—添加视频水印

 /**
     * 根据水印文字---添加视频水印
     * @param srcPath  原视频路径
     * @param tarVideoPath  生成后的视频路径
     * @param wmPosition  水印位子
     * @param alpha  透明度
     * @param txt String  文字
     * @param fontSize   每个字的宽度和高度是一样的
     * @param fontColor Color  字体颜色
     * @param isBold 字体是否加粗
     * @param fontType  字体样式
     * @param fontPath  字体文件
     * @return
     */
   public static boolean processFfmpegWatermkByFont(String srcPath,String tarVideoPath,int wmPosition, float alpha,String txt, int fontSize,Color fontColor,boolean isBold,String fontType,String fontPath) {
    	   //通过文字生成的图片临时路径
           String waterMarkPath =  WebAppUtils.getWebAppRoot() +BaseCommonUtil.TXTIMGPATH  + BaseCommonUtil.getDateNum() + ".png";
    	   //waterMarkPath = "D:/project/BSRCM_TRUNK/WebRoot/swf/txtImg/1.png";
           //通过文字生成透明图片
           ImageHelps.createJpgByFont(txt,fontSize,fontColor,isBold,fontType,fontPath,waterMarkPath);
           
           //avs ---通过水印图片添加水印视频
           //return processFfmpegWatermarkByImage(srcPath,tarVideoPath,waterMarkPath,wmPosition,alpha);
           
           //vfilters ---通过水印图片添加水印视频
           boolean boo = processFfmpegWatermarkByImg(srcPath,tarVideoPath,waterMarkPath,wmPosition,alpha);
           if(boo){
        	   logger.info("【" + srcPath +"】 视频添加文字水印图片成功! ");
        	   return  true;
           }else{
        	   logger.error("【" + srcPath +"】 processFfmpegWatermkByFont  视频添加文字水印图片失败! ");
        	   return false;
           }
          
   }

2.根据水印图片—添加视频水印

 /**
      *根据水印图片---添加视频水印
      * @param srcPath  原视频路径
      * @param tarVideoPath  生成后的视频路径
      * @param waterMarkPath  水印图片路径
      * @param wmPosition  水印位子
      * @param alpha  透明度
      * @return
      */
      public static boolean processFfmpegWatermarkByImg(String srcPath,String tarVideoPath,String waterMarkPath,int wmPosition, float alpha) {  
          if (!checkfile(srcPath)) {  
        	  logger.error("【" + srcPath + "】  不存在 !"); 
              return false;  
          }  
          
          if (!checkfile(waterMarkPath)) {  
        	  logger.error("【" + waterMarkPath + "】  不存在 !"); 
              return false;  
          }  
	          //如果父目录不存在就创建一个
              tarVideoPath = BaseCommonUtil.replaceFliePathStr(tarVideoPath);
	          BaseCommonUtil.mkdir(tarVideoPath);
	          //缩放图片
	          zooImage(srcPath, waterMarkPath); 
	          //要执行的shell脚本路径
	          
	          String shellPath =  WebappConfigUtil.getParameter("shellPath");
        	  if (!checkfile(shellPath)) { 
        		  logger.error("【" + shellPath + "】shell脚本路径 不存在 !");
        	  }
        	  String extendTarName = tarVideoPath.substring(tarVideoPath.lastIndexOf(".")+1,tarVideoPath.length());
        	  Process process = null;
	              try {  
	            	    String os = System.getProperty("os.name");
		      			if (os != null && os.toLowerCase().startsWith("windows")) {
		      			  String picPath = waterMarkPath.substring(waterMarkPath.indexOf("WebRoot"),waterMarkPath.lastIndexOf("/"));
		      			  waterMarkPath = waterMarkPath.substring(waterMarkPath.lastIndexOf("/")+1,waterMarkPath.length());
		      			  process = Runtime.getRuntime().exec(shellPath +" "+picPath+" " + ffmpegPath + " " + srcPath + " " + waterMarkPath + " " + getVideoPosition(wmPosition) + " " + tarVideoPath); 
		      			} else{
		      			  process = Runtime.getRuntime().exec(shellPath + " " + ffmpegPath + " " + srcPath + " " + waterMarkPath + " " + getVideoPosition(wmPosition) + " " + tarVideoPath); 
		      			}
	                  doWaitFor(process); 
	                  //转换mate信息
	                  if("MP4".equals(extendTarName.toUpperCase())){
	      				return execMp4Box(tarVideoPath);
	      			  }
	                  
	                  if (!checkfile(tarVideoPath)) {  
	                	  logger.error("【" + srcPath + "】processFfmpegWatermarkByImage  视频添加水印不成功 !");
	                      return false;  
	                  }
	                  return true;  
	              } catch (Exception e) {  
	            	  logger.error("【" + srcPath + "】processFfmpegWatermarkByImage  视频添加水印不成功 !");
	                  return false;  
	              }finally{
	            	  if(process != null){
	            		  process.destroy();
	            	  }
	              }
      }  

3.根据方位数字获取值

/**
       * 根据方位数字获取值
       * @param wmPosition
       * Top left corner
	   * ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:10 [out]" outputvideo.flv
	   * Top right corner
	   * ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]" outputvideo.flv
	   * Bottom left corner
	   * ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:main_h-overlay_h-10 [out]" outputvideo.flv
	   * Bottom right corner
	   * ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]" outputvideo.flv
       * @return
       */
      public static String getVideoPosition(int wmPosition){
    	  String positionStr = "main_w-overlay_w-10:main_h-overlay_h-10";   //默认右上角
          switch (wmPosition) {
          case 0:
        	  positionStr ="10:10";  //左上角
  	         break;
          case 1:
        	  positionStr ="main_w-overlay_w-10:10";  //右上角
  	         break;
          case 2:
        	  positionStr ="10:main_h-overlay_h-10"; //左下角
  	         break;
          case 3:
        	  positionStr ="main_w-overlay_w-10:main_h-overlay_h-10";   //右下角
  	         break;
          case 4:
        	  positionStr ="(main_w-overlay_w)/2:main_h-overlay_h:1";  //底部居中
  		     break;
          default:
          	break;
        }
          return positionStr;
      }

4.缩放图片

/**
       * 缩放图片
       * @param srcPath  视频路径
       * @param wmImgPath  水印图片路径
       * @return
       */
      public static void zooImage(String srcPath,String wmImgPath){
     	 //获取视频信息
     	 VideoInfo videoInfo = VideoInfoHelps.getVideoInfo(srcPath);
     	 int width = 0;   //视频宽
     	 int height = 0;   //视频高
     	 if(videoInfo != null){
     		 String widthStr = videoInfo.getWidth();  //宽
     		 	if(StringUtils.isNotEmpty(widthStr)) width = Integer.parseInt(widthStr);
     		 String heightStr = videoInfo.getHeight();  //高
     		 	if(StringUtils.isNotEmpty(heightStr)) height = Integer.parseInt(heightStr);
     	 }
     	 
     	  if(width !=0 || height !=0){ //如果获取到视频宽高
	     		 //进行图片缩放
				ImageHelps.zoomPerImg(wmImgPath, wmImgPath,width,height);  
     	    }
     }

5.获取x,y坐标值,返回String数组

/**
       * 获取x,y坐标值,返回String数组
       * @param wmPosition
       * 0:左上角
       * 1:右上角
       * 2:左下角
       * 3:右下角
       * 4:居中
       * @param srcPath  视频路径
       * @param wmImgPath  水印图片路径
       * @return
       */
      public static int[] getXy(int wmPosition,String srcPath,String wmImgPath){
     	 
     	  int x=10,y=10; //定义默认为左上角
     	  
     	  int[] xy = new int[2];
     	  
     	 //获取视频信息
     	 VideoInfo videoInfo = VideoInfoHelps.getVideoInfo(srcPath);
     	 int width = 0;   //视频宽
     	 int height = 0;   //视频高
     	 if(videoInfo != null){
     		 String widthStr = videoInfo.getWidth();  //宽
     		 	if(StringUtils.isNotEmpty(widthStr)) width = Integer.parseInt(widthStr);
     		 String heightStr = videoInfo.getHeight();  //高
     		 	if(StringUtils.isNotEmpty(heightStr)) height = Integer.parseInt(heightStr);
     	 }
     	 
     	 if(width !=0 || height !=0){ //如果获取到视频宽高
     		 
	     		 //取得坐标方位编码
	         	 int position =BaseCommonUtil.getWmDegree(wmPosition);
	         	 
	         	 int wideth_biao =0; //水印图片的宽
	         	 int height_biao = 0; //水印图片的高
	     		 try {
	     			    //首先进行图片缩放
	     			    ImageHelps.zoomPerImg(wmImgPath, wmImgPath,width,height);
	     				//水印文件  
	     			    Image src_biao = ImageIO.read(new File(wmImgPath));
	     			    wideth_biao = src_biao.getWidth(null);  
	     			    height_biao = src_biao.getHeight(null);  
	     		   } catch (IOException e) {
	     			   logger.error("【" + wmImgPath + "】getXy  读取水印图片失败 !");
	     		}  
 	    		 
     		     //左上角距离边距10像素
 	             int leftUpWideth = 10;
 	             int leftUpHeight = 10;
 	             
 	             //右上角距离边距10像素
 	             int rightUpWideth = (width - wideth_biao) - 10;
 	             int rightUpHeight = 10;
 	             
 	             //左下角距离边距10像素
 	             int leftDownWideth = 10;
 	             int leftDownHeight = (height - height_biao) - 10;
 	             
 	             //右下角距离边距10像素
 	             int rightDownWideth = (width - wideth_biao) - 10;
 	             int rightDownHeight = (height - height_biao) - 10;
 	             
 	             //居中
 	             int centerWideth =  (width - wideth_biao) / 2;
 	             int centerHeight = (height - height_biao) / 2;
 	            
 	            
 		         switch (position) {
 		            case 0:
 		    	         x = leftUpWideth;
 		    	         y = leftUpHeight;
 		    	         break;
 		            case 1:
 		    	         x = rightUpWideth;
 		    	         y = rightUpHeight;
 		    	         break;
 		            case 2:
 		    	         x = leftDownWideth;
 		    	         y = leftDownHeight;
 		    	         break;
 		            case 3:
 		    	         x = rightDownWideth;
 		    	         y = rightDownHeight;
 		    	         break;
 		            case 4:
 		            	 x = centerWideth;
 		    	         y = centerHeight;
 		    		     break;
 		            default:
 		            	break;
 		          }
     	 }
     	 
     	 //设值
     	 xy[0] =x;
     	 xy[1] =y;
     	 
     	 return xy;
     }



版权声明:本文为tanghui2qinghong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。