java將m3u8格式轉(zhuǎn)成視頻文件的方法
這是一次嘗試,android手機(jī)將在線的m3u8小電影保存到手機(jī)端,手機(jī)端把文件復(fù)制到電腦端。
然后使用小工具合并成可播放的視頻。
/**
* 合并視頻文件
*
*/
public class MergeVideos {
/**
* source為源地址,destination為合并之后的文件地址,videoName為合并后視頻的名字,num為視頻數(shù)量
* @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界面,堪稱完美。

目錄選擇方式,可以選擇粘貼,或者文件選擇的方式。

運(yùn)行完成。合并的文件都好了。
總結(jié)
到此這篇關(guān)于java將m3u8格式轉(zhuǎn)成視頻文件的方法的文章就介紹到這了,更多相關(guān)java m3u8 視頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Get請求與post請求的實(shí)現(xiàn)
在Spring中,GET請求和POST請求是兩種常見的HTTP請求方法,用于與服務(wù)器進(jìn)行交互,本文詳細(xì)的介紹一下Spring?Get請求與post請求的實(shí)現(xiàn),感興趣的可以了解一下2023-10-10
AsyncHttpClient的默認(rèn)配置源碼流程解讀
這篇文章主要為大家介紹了AsyncHttpClient的默認(rèn)配置源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
JavaWeb請求轉(zhuǎn)發(fā)和請求包含實(shí)現(xiàn)過程解析
這篇文章主要介紹了JavaWeb請求轉(zhuǎn)發(fā)和請求包含實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
Java實(shí)現(xiàn)非阻塞式服務(wù)器的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)一個簡單的非阻塞式服務(wù)器,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,需要的可以參考一下2023-05-05

