<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean name="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="transactionAttributes"> <props> <prop key="get*"> PROPAGATION_REQUIRED,readOnly,-Exception </prop> <prop key="query*"> PROPAGATION_REQUIRED,readOnly,-Exception </prop> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="mdodify*"> PROPAGATION_REQUIRED,-Exception </prop> <prop key="del*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*"> PROPAGATION_REQUIRED,-Exception </prop> </props> </property> </bean> <bean id="VideoDAO" class="com.gcg.video.dao.VideoDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="ScheduleVideoDAO" class="com.gcg.video.dao.ScheduleVideoDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean name="videoImpl" class="com.gcg.video.service.VideoImpl"> <property name="videoDAO" ref="VideoDAO" /> </bean> <bean id="videoImplproxy" parent="txProxyTemplate"> <property name="target"> <ref bean="videoImpl" /> </property> </bean> <bean id="ScheduleVideoImpl" class="com.gcg.video.service.ScheduleVideoImpl"> <property name="scheduleVideoDAO" ref="ScheduleVideoDAO" /> </bean> <bean id="ScheduleVideoImplproxy" parent="txProxyTemplate"> <property name="target"> <ref bean="ScheduleVideoImpl" /> </property> </bean> <bean id="ScheduleImpl" class="com.gcg.video.service.quterz.ScheduleImpl"> <constructor-arg index="0"> <ref bean="videoImplproxy"/> </constructor-arg> <constructor-arg index="1"> <ref bean="ScheduleVideoImplproxy"/> </constructor-arg> <!-- <property name="videoDAO"> <ref bean="VideoDAO" /> </property> <property name="scheduleVideoDAO"> <ref bean="ScheduleVideoDAO" /> </property> --> </bean> <bean id="ScheduleImplproxy" parent="txProxyTemplate"> <property name="target"> <ref bean="ScheduleImpl" /> </property> </bean> <!-- <bean id="convertVideoTask class="org.springframework.scheduling."> <property name="targetObject"> <ref bean="ScheduleImplproxy" /> </property> <property name="targetMethod"> <value>converVedio</value> </property> </bean>--> <bean id="convertVideoTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="ScheduleImplproxy" /> </property> <property name="targetMethod"> <value>converVedio</value> </property> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="convertVideoTask" /> </property> <property name="cronExpression"> <!-- 每天 11:56 触发 --> <value>0 56 11 * * ?</value> </property> </bean> <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="cronTrigger" /> </list> </property> </bean> <bean name="/upload" class="com.gcg.video.struts.action.UploadAction"> <property name="scheduleVideo"> <ref bean="ScheduleVideoImplproxy" /> </property> </bean> </beans> package com.gcg.video.service.quterz; import com.gcg.video.dao.ScheduleVideoDAO; import com.gcg.video.dao.VideoDAO; import com.gcg.video.entity.ScheduleVideo; import com.gcg.video.entity.Video; import com.gcg.video.service.IVideo; import com.gcg.video.service.IscheduleVideo; import com.gcg.video.utils.ChangPathInfo; import com.gcg.video.utils.SysPath; import com.gcg.video.utils.Win_ffmpeg; public class ScheduleImpl { private static IVideo videoService; private static IscheduleVideo scheduleVideoService; public ScheduleImpl(){ } public ScheduleImpl(IVideo videoService, IscheduleVideo scheduleVideoService){ this.videoService = videoService; this.scheduleVideoService = scheduleVideoService; } public static void main(String[] args){ // String test = "E:/apache-tomcat-6.0.26/webapps/VideoDemo/uncovert/heh"; // //test.replace('/', '\\'); // //test.replaceAll("/^\\*&/", "\\"); // //test.replaceAll(regex, replacement) // char [] array = test.toCharArray(); // for(int i=0;i<array.length;++i){ // if(array[i] == '/'){ // array[i] ='\\'; // } // // } // //System.out.println(new String(array)); } public static synchronized void converVedio() throws RuntimeException { // TODO Auto-generated method stub try { ScheduleVideo scheduleVideo = scheduleVideoService.getEarlySchedule(); while(scheduleVideo != null){ String videoName = scheduleVideo.getScSourcePath(); String postfix = videoName.substring(videoName.lastIndexOf('.') + 1,videoName.length()); String wantPath = SysPath.getPath(); String fromPath = wantPath + "/unconvert/" + videoName; fromPath = ChangPathInfo.change(fromPath); System.out.println(fromPath); System.out.println(postfix); String converName = videoName.substring(0,videoName.lastIndexOf('.')+1)+"flv"; String toPath = wantPath + "/convert/" + converName; toPath = ChangPathInfo.change(toPath); System.out.println(toPath); // String fromPath = "unconvert/" String pattern = "300*200"; System.out.println(Win_ffmpeg.isValidPatter(postfix)); if(Win_ffmpeg.isValidPatter(postfix)){ if(Win_ffmpeg.conver(fromPath, toPath, pattern)==0){ String VImgPath = Win_ffmpeg.getConverVideoImg(fromPath); if(VImgPath != null){ videoService.addVideo(new Video(videoName ,videoName,scheduleVideo.getScUploadDate(),VImgPath) ); scheduleVideo.setScState(0L); scheduleVideoService.updateScheduleVideo(scheduleVideo); } } } scheduleVideo = scheduleVideoService.getEarlySchedule(); } } catch (RuntimeException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } } public IVideo getVideoService() { return videoService; } public void setIdeoService(IVideo ideoService) { videoService = ideoService; } public IscheduleVideo getScheduleVideoService() { return scheduleVideoService; } public void setScheduleVideoService(IscheduleVideo scheduleVideoService) { scheduleVideoService = scheduleVideoService; } } /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.gcg.video.struts.action; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.MappingDispatchAction; import com.gcg.video.entity.ScheduleVideo; import com.gcg.video.service.IscheduleVideo; import com.gcg.video.utils.UUIDString; public class UploadAction extends MappingDispatchAction { private IscheduleVideo scheduleVideo; public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //System.out.println(request.getSession().getServletContext().getRealPath("/")+"unconvert"+System.getProperty("file.separator")); DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1024 * 1024 * 2); factory.setRepository(new File(request.getSession().getServletContext().getRealPath("/")+System.getProperty("file.separator")+"unconvert")); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(1024 * 1024 * 200); String upLoadVideoName = null; List items = upload.parseRequest(request); //System.out.println(items.size()); for (Iterator it = items.iterator(); it.hasNext() ; ) { FileItem item = (FileItem)it.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString("UTF-8"); //out.println("表单域的name=value对为:" + name + "=" + value); //System.out.println(name + "=" + value); } else { String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); //System.out.println(contentType); upLoadVideoName = UUIDString.getUUID() + fileName.substring(fileName.lastIndexOf(".") , fileName.length()); FileOutputStream fos = new FileOutputStream( request.getSession().getServletContext().getRealPath("/") + "unconvert" + System.getProperty("file.separator") + upLoadVideoName); // System.out.println(request.getSession().getServletContext().getRealPath("/") + "unconvert"+System.getProperty("file.separator") + // upLoadVideoName); if (item.isInMemory() ) { fos.write(item.get()); } else { InputStream is = item.getInputStream(); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) > 0 ) { fos.write(buffer , 0 , len); } is.close(); fos.close(); } } } scheduleVideo.addScheduleVideo(new ScheduleVideo(upLoadVideoName, 1L, new java.util.Date()) ); return mapping.findForward("success"); } public IscheduleVideo getScheduleVideo() { return scheduleVideo; } public void setScheduleVideo(IscheduleVideo scheduleVideo) { this.scheduleVideo = scheduleVideo; } CREATE SEQUENCE SEQ_VIDEO INCREMENT BY 1 NOMINVALUE NOMAXVALUE CACHE 20 NOCYCLE NOORDER; CREATE SEQUENCE SEQ_SCHEDULE_VIDEO INCREMENT BY 1 NOMINVALUE NOMAXVALUE CACHE 20 NOCYCLE NOORDER; CREATE TABLE VIDEO ( V_ID NUMBER(10) , V_NAME VARCHAR2(100) NOT NULL , V_PATH VARCHAR2(200) NOT NULL, V_UPLOAD_DATE DATE DEFAULT SYSDATE NOT NULL , V_IMG_PATH VARCHAR2(200) NOT NULL ) CREATE TABLE SCHEDULE_VIDEO ( SC_ID NUMBER(10), SC_SOURCE_PATH VARCHAR2(200) NOT NULL, SC_STATE NUMBER(1) NOT NULL, SC_UPLOAD_DATE DATE DEFAULT SYSDATE NOT NULL ) ALTER TABLE VIDEO ADD CONSTRAINTS PK_VIDEO PRIMARY KEY (V_ID); ALTER TABLE SCHEDULE_VIDEO ADD CONSTRAINTS PK_SC_VIDEO PRIMARY KEY( SC_ID); ALTER TABLE SCHEDULE_VIDEO ADD CONSTRAINTS CK_SC_VIDEO CHECK (SC_STATE IN (-1,0,1)); package com.gcg.video.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; public class Win_ffmpeg { private static final Logger logger =Logger.getLogger(Win_ffmpeg.class); private static final String[] PATTER = {"mpg", "asf", "avi", "mp4", "wav", "mp4", "dv"}; //private static final String[] MenCode = {"rmvb" ,"rm"}; public static boolean isValidPatter(String patter){ boolean result = false; if(patter == null || patter.length() < 2){ logger.error("参数非法"); throw new IllegalArgumentException("视频参数非法") ; } patter = patter.trim().toLowerCase(); for(String t : PATTER){ if(t.equals(patter)){ result = true; } } return result; } synchronized public static String getConverVideoImg(String fromPath){ if(fromPath==null||fromPath.length()<2){ logger.error("参数非法"); throw new IllegalArgumentException("视频参数非法") ; } String imgName = UUIDString.getUUID() + ".jpg"; String imgPath = SysPath.getPath()+"/preview/" + imgName; imgPath = ChangPathInfo.change(imgPath); Process process = null; StringBuilder message = new StringBuilder(); List<String> commandList = new ArrayList<String>(); commandList.add("D:\\ffmpeg_full_SDK_V3.0\\Libs\\ffmpeg.exe"); commandList.add("-i"); commandList.add(fromPath); commandList.add("-y"); commandList.add("-f"); commandList.add("image2"); commandList.add("-ss"); commandList.add("60"); commandList.add("-vframes"); commandList.add("1"); commandList.add(imgPath); ProcessBuilder builder = new ProcessBuilder(commandList); int status = -1; try { process = builder.start(); builder.redirectErrorStream(true); //builder.directory(); process = builder.start(); BufferedReader in = new BufferedReader( new InputStreamReader(process.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); message.append(line + System.getProperty("line.separator")); } status = process.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block logger.error("IOException"); e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error("ThreadException"); } if (status == 0){ logger.debug("convert success"); return imgName; } else{ logger.error("convert failture"); } return null; } synchronized public static int conver(String fromPath, String toPath, String pattern){ if(fromPath==null||fromPath.length()<2||toPath==null||toPath.length()<2||pattern==null||pattern.length()<7){ logger.error("参数非法"); throw new IllegalArgumentException("视频参数非法") ; } Process process = null; StringBuilder message = new StringBuilder(); List<String> commandList = new ArrayList<String>(); commandList.add("D:\\ffmpeg_full_SDK_V3.0\\Libs\\ffmpeg.exe"); commandList.add("-i"); commandList.add(fromPath); commandList.add("-ab"); commandList.add("56"); commandList.add("-ar"); commandList.add("22050"); commandList.add("-b"); commandList.add("500"); commandList.add("-r"); commandList.add("15"); commandList.add("-s"); commandList.add(pattern); commandList.add(toPath); ProcessBuilder builder = new ProcessBuilder(commandList); int status = -1; try { process = builder.start(); builder.redirectErrorStream(true); //builder.directory(); process = builder.start(); BufferedReader in = new BufferedReader( new InputStreamReader(process.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); message.append(line + System.getProperty("line.separator")); } status = process.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block logger.error("IOException"); e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error("ThreadException"); } if (status == 0){ logger.debug("convert success"); } else{ logger.error("convert failture"); } return status; } }
VIDEO DEMO
最新推荐文章于 2022-05-30 21:32:06 发布