黑马程序员技术交流社区

标题: 毕老师第二十天切割文件,如何切割视频? [打印本页]

作者: Eric1225    时间: 2014-10-17 21:18
标题: 毕老师第二十天切割文件,如何切割视频?
刚看到毕老师第20天视频的最后一集,提到切割视频。想问下具体怎么实现!

作者: 哈达洋    时间: 2014-10-17 23:39
本帖最后由 哈达洋 于 2014-10-18 00:01 编辑
  1. <div class="blockcode"><blockquote>import java.io.*;
  2. import java.util.*;

  3. class VideoSplit
  4. {
  5.         public static void main(String[] args) throws Exception
  6.         {
  7.                 File file = new File("g:\\超凡蜘蛛侠2.BD.720p.中英双字幕.rmvb");
  8.                 //split(file);
  9.                 merge();

  10.         }

  11.         public static void merge() throws Exception
  12.         {
  13.                 ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();

  14.                 for(int i=0;i<4;i++)
  15.                 {
  16.                         al.add(new FileInputStream("g:\\part\\"+i+".part"));
  17.                 }

  18.                 Iterator<FileInputStream> it = al.iterator();

  19.                 Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
  20.                
  21.                         public boolean hasMoreElements()
  22.                         {
  23.                                 return it.hasNext();
  24.                         }

  25.                         public FileInputStream nextElement()
  26.                         {
  27.                                 return it.next();
  28.                         }
  29.                 };

  30.                 SequenceInputStream sis = new SequenceInputStream(en);
  31.                 BufferedOutputStream bos =
  32.                         new BufferedOutputStream(new FileOutputStream("g:\\part\\spilderman.rmvb"));
  33.                 byte[] buf = new byte[1024*1024*10];

  34.                 int len=0;
  35.                 while((len=sis.read(buf))!=-1)
  36.                 {
  37.                         bos.write(buf,0,len);
  38.                         bos.flush();
  39.                 }
  40.                 bos.close();
  41.                 sis.close();
  42.        
  43.         }

  44.         public static void split(File file) throws Exception
  45.         {
  46.                 if(!file.exists())
  47.                 {
  48.                         System.out.println("文件不存在");
  49.                         return;
  50.                 }
  51.                 else
  52.                 {
  53.                         BufferedInputStream bis =
  54.                                 new BufferedInputStream(new FileInputStream(file));

  55.                         //定义一个10M的缓冲,虽然BufferedInputStream自带了缓冲,
  56.                         //但是这里再定义一个缓冲,在写入的时候,会更快。
  57.                         byte[] buf1 = new byte[1024*1024*10];

  58.                         BufferedOutputStream bos =null;
  59.                         int len=0;
  60.                         int count=0;
  61.                         while((len=bis.read(buf1))!=-1)
  62.                         {       
  63.                                
  64.                                 if(count%50==0)//按500M分割
  65.                                 {
  66.                                         bos = new BufferedOutputStream(new FileOutputStream("g:\\part\\"+(count/50)+".part"));
  67.                                 }
  68.                                 count++;
  69.                                 bos.write(buf1,0,len);
  70.                                 bos.flush();
  71.                                 if(count%50==0)
  72.                                 {
  73.                                         bos.close();
  74.                                 }
  75.                         }
  76.                         bis.close();
  77.                         bos.close();
  78.                 }       
  79.         }
  80. }
复制代码



作者: suoxidong    时间: 2014-10-18 08:14
QQ影音里面有功能可以切割视频,合并视频功能
作者: 郑飞    时间: 2014-10-18 20:49
切什么不都是大同小异 老毕有讲过吧




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2