java将将m3u8格式转成视频文件的方法格式转成视频文件的方法
主要介绍了如何java将m3u8格式转成视频文件,本文通过实例代码给大家介绍的非常详细,对大家的学习或工
作具有一定的参考借鉴价值,需要的朋友可以参考下
这是一次尝试,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;
评论1