java 显示视频时间--玩的

1.显示视频时间

package view.time;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ViewTimeGUI {
	  private Frame f;  
	  private Button b;  
	  private Button btn_all; 
	  private Button btn_export;
	  private TextField tf;
	  private Label l;
	  private TextArea ta;
	  
	  
	public void ViewGui(){
		init(); 	
	}
	
	private void init() {
		// TODO Auto-generated method stub
		f=new Frame("显示文件夹下所有视频时间及路径  jdk-1.7");
		f.setBounds(300, 200, 795, 275);
		f.setBackground(Color.lightGray);
		f.setLayout(new FlowLayout());
		
		l=new Label("文件夹路径:");
		tf=new TextField(50);
		tf.setText("C:/Users/Administrator/Desktop/sp/mp4");
		b=new Button("查看flv格式视频"); 
		btn_all=new Button("查看所有视频时间不限制格式");
		ta=new TextArea();
		ta.setColumns(200);
		ta.setEditable(false);
		btn_export=new Button("导出信息到记事本");
		
		f.add(l);
		f.add(tf);
	    f.add(b);
	    f.add(btn_all);
	    f.add(ta);
	    f.add(btn_export);
	    
	    
	    event();
	    
	    f.setVisible(true);  
	    System.out.println("Hello world!"); 
	}

	private void event() {
		// TODO Auto-generated method stub
		 f.addWindowListener(new WindowAdapter() {  
	            @Override  
	            public void windowClosing(WindowEvent e) {  
	                // TODO Auto-generated method stub  
	                System.exit(0);  
	            }  
	          
	        });  
		 /*
	        tf.addKeyListener(new KeyAdapter() {  
	            public void keyPressed(KeyEvent e){ 
	            	//验证键盘数字
	                int code=e.getKeyCode();  
	                if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){  
	                    System.out.println(code+"....非法的输入");  
	                    e.consume();  //不执行加入文本框.  
	                }  
	                
	                  
	            }  
	              
	        });  
	          */
	          
	          
	        b.addActionListener(new ActionListener() {  
	              
	            @Override  
	            public void actionPerformed(ActionEvent e) {  
	                // TODO Auto-generated method stub  
	                //System.out.println("actionPerformed 活动一次");  
	            }  
	        });  
	         //查看按钮操作
	        b.addMouseListener(new MouseAdapter() {  
	            private int count=0;  
	            private int clickCount=1;  
	            /*
	            //鼠标放在按钮上事件
	            public void mouseEntered(MouseEvent e){  
	                System.out.println("鼠标进入到改组件"+count++);  
	            }  
	            */
	              
	            public void mouseClicked(MouseEvent e){  
	           
	                if(e.getClickCount()==2){  
	                    System.out.println("双击动作");  
	                }else
	                {
	                	String fileName=tf.getText().trim();	                	
	                	//判断文本框不能为空
	                	if(fileName!=null&&fileName.length()>0)
	                	{
	                		File file =new File(fileName); 
	                		//文件夹不存在或不是文件夹目录
	                		if (!file .exists() && !file .isDirectory())
	                		{ 
	                			//System.out.println("文件夹路径不存在或不是文件夹目录");
	                			ta.append("文件夹路径不存在或不是文件夹目录!\r\n");
	                		}else{
	                			ta.setText("");
			                	System.out.println("file+name"+fileName);
		//	                	String fileName="C:/Users/Administrator/Desktop/sp/mp4";
			            		File f=new File(fileName);
			                    print(f,ta);  	                
			                    System.out.println("点击动作"+clickCount++);  
	                		}
	                	}else
	                	{
	                		ta.append("\r\n请填写文件路径!!\r\n");
	                	}
	                }
	                  
	            }  
	              
	        });
	        btn_all.addMouseListener(new MouseAdapter(){
	        	    private int count=0;  
		            private int clickCount=1;  
		            /*
		            //鼠标放在按钮上事件
		            public void mouseEntered(MouseEvent e){  
		                System.out.println("鼠标进入到改组件"+count++);  
		            }  
		            */
		              
		            public void mouseClicked(MouseEvent e){  
		           
		                if(e.getClickCount()==2){  
		                    System.out.println("双击动作");  
		                }else
		                {
		                	String fileName=tf.getText().trim();	                	
		                	//判断文本框不能为空
		                	if(fileName!=null&&fileName.length()>0)
		                	{
		                		File file =new File(fileName); 
		                		//文件夹不存在或不是文件夹目录
		                		if (!file .exists() && !file .isDirectory())
		                		{ 
		                			//System.out.println("文件夹路径不存在或不是文件夹目录");
		                			ta.append("文件夹路径不存在或不是文件夹目录!\r\n");
		                		}else{
		                			ta.setText("");
				                	System.out.println("file+name"+fileName);
			//	                	String fileName="C:/Users/Administrator/Desktop/sp/mp4";
				            		File f=new File(fileName);
				            		printAll(f,ta);  	                
				                    System.out.println("点击动作"+clickCount++);  
		                		}
		                	}else
		                	{
		                		ta.append("\r\n请填写文件路径!!\r\n");
		                	}
		                }
		                  
		            }                    	            
	        });
	        //导出到记事本
	        btn_export.addMouseListener(new MouseAdapter() {  
	            private int count=0;  
	            private int clickCount=1;  
	            /*
	            //鼠标放在按钮上事件
	            public void mouseEntered(MouseEvent e){  
	                System.out.println("鼠标进入到改组件"+count++);  
	            }  
	            */
	              
	            public void mouseClicked(MouseEvent e){  
	           
	                if(e.getClickCount()==2){  
	                    System.out.println("双击动作");  
	                }else
	                {
	                	//文件路径
	                	String filepath=null;
	                	//ta文本框信息
	                	String unamemessage=ta.getText().trim();
	                	System.out.println("unamemessage:"+unamemessage);
//	                	String fileName="C:/Users/Administrator/Desktop/sp/mp4";
	            		//File f=new File();
	                	//获取磁盘名称
	                	List<String> list=getDiskName();
	                	for(String str:list){
	                		if(str.equals("D:\\")){
	                			//有d盘的话  写入到d盘
	                			String filename="Result.txt";
	                			 /**写入到记事本中*/
	                		     try {
	                		    	 filepath=str+filename;
	                				BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filepath), true));
	                				writer.write(unamemessage);
	                				// writer.write("文件名称:"+source.getName()+"\r\n");
	                			    writer.close();
	                			} catch (IOException es) {
	                				// TODO Auto-generated catch block
	                				es.printStackTrace();
	                			}
	                		}else
	                		{
	                			System.out.println("没有d盘!");
	                		}
	                	}
	                	//追加信息
	                	ta.append("\r\n\r\n文件写入到了::"+filepath+"\r\n\r\n"); 	                
	                 //   System.out.println("点击动作"+clickCount++);  
	                }
	                  
	            }  
	              
	        }); 
	        
	        /*
	        //添加 键盘事件  
	        b.addKeyListener(new KeyAdapter() {  
	          
	            public void keyPressed(KeyEvent e){  
	                System.out.println(e.getKeyChar()+"..."+e.getKeyCode());  
	                System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());  
	                //enter  就退出  
	                /*if(e.getKeyCode()==KeyEvent.VK_ENTER) 
	                    System. 
	                    exit(0);*/  
	                  
	        /*
	                //ctrl + Enter 发送消息  
	                if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER){  
	                    System.out.println("我要发送消息!");  
	                }  
	                  
	            }  
	          
	        });  
	        
	       */
	        
	        
	        
	}

	/*
	//因为接口WindowLinstener中的所有方法都被子类 WindowAdapter实现了,.  
	//并且覆盖了其中的所有方法,那么我们只能继承 WindowAdapter 覆盖我们的方法即可  
	class MyWin extends WindowAdapter{  
	      
	    @Override  
	    public void windowClosing(WindowEvent e) {  
	        // TODO Auto-generated method stub  
	        //System.out.println("Window closing"+e.toString());  
	        System.out.println("我关了");  
	        System.exit(0);  
	    }  
	    @Override  
	    public void windowActivated(WindowEvent e) {  
	        //每次获得焦点 就会触发  
	        System.out.println("我活了");    
	        //super.windowActivated(e);  
	    }  
	    @Override  
	    public void windowOpened(WindowEvent e) {  
	        // TODO Auto-generated method stub  
	        System.out.println("我开了");  
	        //super.windowOpened(e);  
	    }  
	}
	*/
/**
 * 获取磁盘名称
 * */
public  List<String> getDiskName(){
		List<String> list=new ArrayList<String>();
		File [] root = File.listRoots();
		for(File file : root)
		{
			//获取到的信息添加到list
		    list.add(file.getAbsolutePath());
		   System.out.println(file.getAbsolutePath());
		   
		}
		return list;
	}
/**递归查找所有文件
 * flv
 * */
	  public static void print(File f,TextArea ta){
		  String timeflvstr=null;
	        if(f!=null){
	            if(f.isDirectory()){
	                File[] fileArray=f.listFiles();
	                if(fileArray!=null){
	                    for (int i = 0; i < fileArray.length; i++) {
	                        //递归调用
	                        print(fileArray[i],ta);
	                    }
	                }
	               
	             
	            }
	            else{
	            	//处理mp4视频文件 只读取flv视频时间
	            	if(f.getName().indexOf(".flv")!=-1){
	            		//处理mp4
	            		String timeflv=getVideoTime(f.getPath());
	            		//System.out.println("name:"+f.getName()+"文件:"+f.getPath()); 
	            		//显示和写入的信息
	            	    timeflvstr="时长为:"+timeflv;
	            		//System.out.println("时长为:"+timeflv);	 
	            	    ta.append(timeflvstr);
	                	ta.append("\r\n"); 
	            	    System.out.println(timeflvstr);
	            	    
	            	}
	                //System.out.println(f);	       
	            }
	        }	
	       
	    }
 /**递归查找所有文件
  * 所有视频
  * */
 public static void printAll(File f,TextArea ta){
		  String timeflvstr=null;
	        if(f!=null){
	            if(f.isDirectory()){
	                File[] fileArray=f.listFiles();
	                if(fileArray!=null){
	                    for (int i = 0; i < fileArray.length; i++) {
	                        //递归调用
	                        printAll(fileArray[i],ta);
	                    }
	                }
	               
	             
	            }
	            else{
	            	   //获取所有视频
	            		String timeflv=getVideoTime(f.getPath());
	            		//System.out.println("name:"+f.getName()+"文件:"+f.getPath()); 
	            		//显示和写入的信息
	            	    timeflvstr="时长为:"+timeflv;
	            		//System.out.println("时长为:"+timeflv);	 
	            	    ta.append(timeflvstr);
	                	ta.append("\r\n"); 
	            	    System.out.println(timeflvstr);	       
	            }
	        }	
	       
	    }
	/**获取单个文件视频时长*/
	 public static String getVideoTime(String  filepaths){
		    File file=new File(filepaths);
		    String timegs = null;
		    Encoder encoder = new Encoder();
		     try {
		       MultimediaInfo m = encoder.getInfo(file);
		       long ls = m.getDuration()/1000;
		       // time = Double.parseDouble(new DecimalFormat("#.00").format(ls/(1000*60.0)));
		       	long hh = ls/3600;  //转为小时
			    long mm =ls%3600/60;       // 转换为分钟   
			    long ss=ls%60;  //秒
			    timegs=hh+":"+mm+":"+ss+" filepath:"+file.getPath();  
		       return timegs;
		     } catch (Exception e) {
		      e.printStackTrace();
		      return null;
		     }
		  }
	 /**获取整个文件夹下视频时长*/
	public static  void  fileFolderSumTime(String filePath){
		/**获取文件夹下的总时长*/
		File source = new File(filePath);
	  	Encoder encoder = new Encoder();
	  	File[] file = source.listFiles();
	  	long sum =0;
	  	for (File file2: file) {
		  	try {
		 	 		MultimediaInfo m = encoder.getInfo(file2);
		  			long ls = m.getDuration()/1000;                 //ls是获取到的秒数18        
		  	 		sum += ls;    
		    } catch (Exception e) {
		     		e.printStackTrace();
		     }
	    }
	     long hh = sum/3600;  //转为小时
	     long mm =sum%3600/60;       // 转换为分钟   
	     long ss=sum%60;            //秒
	     System.out.println("总时长为--:"+hh+"::"+mm+"::"+ss);
	     System.out.println("文件路径为:"+source.getPath());
	     //System.out.println("文件名称:"+source.getName());
	     /**写入到记事本中*/
	     try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File("d:\\Result.txt"), true));
			 writer.write("总时长为--:"+hh+"::"+mm+"::"+ss+"\r\n");
			 writer.write("文件路径为:"+source.getPath()+"\r\n");
			// writer.write("文件名称:"+source.getName()+"\r\n");
			 writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     
	}
	
	//获取文件时长,备用
	// TODO Auto-generated method stub
	//File file=new File("C:/Users/Administrator/Desktop/sp/012 - Unity3D界面-Scene视图(3).mp4");
	/* File source = new File("E:\\测试视频\\R41.avi");  
      Encoder encoder = new Encoder();  
      try {  
           MultimediaInfo m = encoder.getInfo(source);  
           long ls = m.getDuration();  
           System.out.println(s/1000+"秒!");  
      } catch (Exception e) {  
          e.printStackTrace();  
      }  
      */
	
}

 

posted @ 2016-05-30 18:04  知乎神者  阅读(381)  评论(0编辑  收藏  举报