这是一次尝试,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界面,堪称完美。

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

运行完成。合并的文件都好了。
总结
到此这篇关于java将m3u8格式转成视频文件的方法的文章就介绍到这了,更多相关java m3u8 视频内容请搜索社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持社区! |