欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java将m3u8格式转成视频文件的方法

时间:2021-12-08 13:18:27|栏目:JAVA代码|点击:

这是一次尝试,android手机将在线的m3u8小电影保存到手机端,手机端把文件复制到电脑端。
然后使用小工具合并成可播放的视频。

/**
 * 合并视频文件
 *
 */
public class MergeVideos {
 /**
 * source为源地址,destination为合并之后的文件地址,videoName为合并后视频的名字,num为视频数量
 * @param source
 * @param destination
 * @throws IOException
 */
 public static void MergeVideos(File source, String destination) throws IOException{
 FileOutputStream out = new FileOutputStream(destination);
 FileInputStream in = null;
 File[] files = source.listFiles();
 for(File file: files){
  in = new FileInputStream(file);
  byte[] bytes = new byte[1024];
  int len = 0;
  while((len = in.read(bytes)) > 0){
  out.write(bytes,0,len);
  }
 }
 in.close();
 out.close();
 }
}
public class M3u8Util{
  /**
   * 根目录
   * @param root
   */
  public static void findFile(File root) throws IOException {
    if(root.exists()){
      if(root.isDirectory()){
        File[] categorys=root.listFiles();
        for(File cate: categorys){
          if(rename(cate)){
            MergeVideos.MergeVideos(cate,cate.getName()+".ts");
          }
        }
      }else{
        System.out.println("file name: "+root.getName());
      }
    }
  }
  /**
   * 将0 改成00或者 000
   * @param cat
   */
  public static boolean rename(File cat){
    if(cat.exists()){
      if(cat.isDirectory()){
        File[] files=cat.listFiles();
        int len=String.valueOf(files.length).length();
        String file0=files[0].getName();
        String pre=file0.substring(0,file0.length()-1);
        Integer max=pre.length()+len;
        Arrays.stream(files)
            .filter(temp->max-temp.getName().length()>0)
            .forEach(item->{
              int fill=max-item.getName().length();
              String name="";
              for(int i=0;i<fill;i++){
                name+=0;
              }
              String n=item.getAbsolutePath().replace(pre,pre+name);
              item.renameTo(new File(n));
            });
        return true;
      }else{
        System.out.println("file name: "+cat.getName());
      }
    }
    return false;
  }

 核心代码如上,再加上一个swing界面,堪称完美。

目录选择方式,可以选择粘贴,或者文件选择的方式。

运行完成。合并的文件都好了。 

总结

上一篇:分享JPA的几个小技巧

栏    目:JAVA代码

下一篇:springboot 单文件上传的实现步骤

本文标题:java将m3u8格式转成视频文件的方法

本文地址:http://www.codeinn.net/misctech/186205.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有