使用java編程語(yǔ)言,對(duì)文件進(jìn)行操作,合并多個(gè)文件,代碼如下:
專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)靖遠(yuǎn)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
import?static?java.lang.System.out;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.nio.ByteBuffer;
import?java.nio.channels.FileChannel;
import?java.util.Arrays;
public?class?test?{
public?static?final?int?BUFSIZE?=?1024?*?8;
public?static?void?mergeFiles(String?outFile,?String[]?files)?{
FileChannel?outChannel?=?null;
out.println("Merge?"?+?Arrays.toString(files)?+?"?into?"?+?outFile);
try?{
outChannel?=?new?FileOutputStream(outFile).getChannel();
for(String?f?:?files){
FileChannel?fc?=?new?FileInputStream(f).getChannel();?
ByteBuffer?bb?=?ByteBuffer.allocate(BUFSIZE);
while(fc.read(bb)?!=?-1){
bb.flip();
outChannel.write(bb);
bb.clear();
}
fc.close();
}
out.println("Merged!!?");
}?catch?(IOException?ioe)?{
ioe.printStackTrace();
}?finally?{
try?{if?(outChannel?!=?null)?{outChannel.close();}}?catch?(IOException?ignore)?{}
}
}
//下面代碼是將D盤(pán)的1.txt?2.txt?3.txt文件合并成out.txt文件。
public?static?void?main(String[]?args)?{
mergeFiles("D:/output.txt",?new?String[]{"D:/1.txt",?"D:/2.txt",?"D:/3.txt"});
}
}
之前有做過(guò)圖片合成視頻的功能,大概代碼就是這樣,你可以看一下
/**
* 圖片合成視頻
* @param mp4SavePath 視頻保存路徑
* @param imageDir 圖片地址
* @param rate 這個(gè)可以理解成視頻每秒播放圖片的數(shù)量
*/
public static boolean jpgToMp4(String mp4SavePath, String imageDir, double rate) {
FFmpegFrameRecorder recorder = null;
boolean flag = true;
try {
File[] files = FileUtils.fileSort(imageDir);
int [] widthArray = new int[files.length];
int [] heightArray = new int[files.length];
/**
* 獲取合成視頻圖片的最大寬高,避免圖片比例不一致最終合成效果差
*/
for (int i = 0; i files.length; i++) {
BufferedImage bufferedImage = ImageIO.read(files[i]);
widthArray[i] = bufferedImage.getWidth();
heightArray[i] = bufferedImage.getHeight();
}
/**
* 這個(gè)方法主要是防止圖片比例達(dá)不到視頻合成比例的要求,如果達(dá)不到下面條件視頻則會(huì)無(wú)法播放
* 圖片寬:必須要被32整除
* 圖片高:必須要被2整除
*/
int [] maxWH = getImgMaxWH(widthArray,heightArray);
recorder = new FFmpegFrameRecorder(mp4SavePath,maxWH[0],maxWH[1]);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
/**
* 視頻質(zhì)量:目前測(cè)試出來(lái)的是25-30最清晰,視頻質(zhì)量范圍好像是0-40,具體可以自己慢慢測(cè)
*/
recorder.setVideoQuality(25);
recorder.setFormat("mp4");
recorder.setFrameRate(rate 0 ? rate : 1);
recorder.setPixelFormat(0);
recorder.start();
OpenCVFrameConverter.ToIplImage conveter = new OpenCVFrameConverter.ToIplImage();
/**
* 合成視頻
*/
for(int i = 0; i files.length; i++ ){
opencv_core.IplImage image = cvLoadImage(files[i].getPath());
recorder.record(conveter.convert(image));
opencv_core.cvReleaseImage(image);
}
logger.info("合成成功");
} catch(Exception e) {
e.printStackTrace();
flag = false;
logger.error("合成失敗");
} finally {
try {
if (recorder != null){
recorder.stop();
recorder.release();
}
} catch (FrameRecorder.Exception e) {
e.printStackTrace();
}
}
return flag;
}
java可以使用FileChannel快速高效地將多個(gè)文件合并到一起,以下是詳細(xì)代碼:
import?static?java.lang.System.out;??
import?java.io.FileInputStream;??
import?java.io.FileOutputStream;??
import?java.io.IOException;??
import?java.nio.ByteBuffer;??
import?java.nio.channels.FileChannel;??
import?java.util.Arrays;??
public?class?test?{??
public?static?final?int?BUFSIZE?=?1024?*?8;??
public?static?void?mergeFiles(String?outFile,?String[]?files)?{??
FileChannel?outChannel?=?null;??
out.println("Merge?"?+?Arrays.toString(files)?+?"?into?"?+?outFile);??
try?{??
outChannel?=?new?FileOutputStream(outFile).getChannel();??
for(String?f?:?files){??
FileChannel?fc?=?new?FileInputStream(f).getChannel();???
ByteBuffer?bb?=?ByteBuffer.allocate(BUFSIZE);??
while(fc.read(bb)?!=?-1){??
bb.flip();??
outChannel.write(bb);??
bb.clear();??
}??
fc.close();??
}??
out.println("Merged!!?");??
}?catch?(IOException?ioe)?{??
ioe.printStackTrace();??
}?finally?{??
try?{if?(outChannel?!=?null)?{outChannel.close();}}?catch?(IOException?ignore)?{}??
}??
}??
public?static?void?main(String[]?args)?{??
mergeFiles("D:/output.txt",?new?String[]{"D:/in_1.txt",?"D:/in_2.txt",?"D:/in_3.txt"});??
}??
}
java合成視頻沒(méi)有報(bào)錯(cuò),但是視頻沒(méi)有成功原因如下:
1、heightdiffers視頻的高度不同導(dǎo)致的,認(rèn)真查看自己的需要合并的視頻文件。
2、發(fā)現(xiàn)有些視頻是橫屏拍攝,有些視頻是豎屏拍攝,導(dǎo)致文件高度不同。因此在視頻文件在合并的時(shí)候報(bào)錯(cuò),解決辦法保證需要合并的視頻文件是相同高度的視頻文件。
當(dāng)前名稱:java合并視頻代碼 java 文件合并
當(dāng)前網(wǎng)址:http://jinyejixie.com/article46/doscjhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、小程序開(kāi)發(fā)、虛擬主機(jī)、域名注冊(cè)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)