導航:首頁 > 編程語言 > java合並視頻

java合並視頻

發布時間:2022-06-21 23:15:55

java如何高效合並多個文件

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,視頻格式轉換。

RMVB轉FLV格式,推薦用 MP4/RM轉換專家

軟體轉換速度是目前最快,國內首個引入多核CPU並發轉換概念,最高支持16核心並發轉換。

對各種視頻格式的支持非常好,支持轉換RMVB視頻格式,轉換的視頻質量比其他軟體更清晰。

並將視頻保存為FLV、AVI、MP4、3GP等格式,參數設置很齊全,可以在各種機子上播放。

對各種移動設備的支持非常好,包括手機、MP4機、iPod、iPhone等機子。試試便知。

網路搜索MP4/RM轉換專家

⑶ java中如何將兩個文件合並到另一個文件

java可以使用FileChannel快速高效地將多個文件合並到一起,以下是詳細代碼:

importstaticjava.lang.System.out;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.nio.ByteBuffer;
importjava.nio.channels.FileChannel;
importjava.util.Arrays;

publicclasstest{
publicstaticfinalintBUFSIZE=1024*8;
publicstaticvoidmergeFiles(StringoutFile,String[]files){
FileChanneloutChannel=null;
out.println("Merge"+Arrays.toString(files)+"into"+outFile);
try{
outChannel=newFileOutputStream(outFile).getChannel();
for(Stringf:files){
FileChannelfc=newFileInputStream(f).getChannel();
ByteBufferbb=ByteBuffer.allocate(BUFSIZE);
while(fc.read(bb)!=-1){
bb.flip();
outChannel.write(bb);
bb.clear();
}
fc.close();
}
out.println("Merged!!");
}catch(IOExceptionioe){
ioe.printStackTrace();
}finally{
try{if(outChannel!=null){outChannel.close();}}catch(IOExceptionignore){}
}
}
publicstaticvoidmain(String[]args){
mergeFiles("D:/output.txt",newString[]{"D:/in_1.txt","D:/in_2.txt","D:/in_3.txt"});
}
}

⑷ java如何實現兩個wav文件混音合並非順序合並。

原理,兩個同時播放,並抓取播放的輸出。

程序有點費勁,暫沒時間去研究

⑸ java合並MP3文件

代碼沒問題
是這樣的每個MP3由兩到三個部分構成:ID3v2標簽+MP3聲音+(ID3v1標簽),後面一個不一定有。
其中的標簽就是MP3的各種信息,比如說歌曲名、演唱者、唱片封面什麼的

所以按你這種直接合並的方式,合並出來的就是:
標簽+MP3聲音+標簽+標簽+MP3聲音+標簽
自然中間就有一段沒有聲音了

建議你參考標簽格式,對於MP3文件進行處理,然後再合並就好了
id3官網:www.id3.org (英文的)
或者你可以直接搜索id3v2很多資料都可以用

⑹ java 如何將多張JPG圖片合成視頻文件,比如:avi格式 或 mpg格式.

之前有做過圖片合成視頻的功能,大概代碼就是這樣,你可以看一下
/**
* 圖片合成視頻
* @param mp4SavePath 視頻保存路徑
* @param imageDir 圖片地址
* @param rate 這個可以理解成視頻每秒播放圖片的數量
*/
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();
}

/**
* 這個方法主要是防止圖片比例達不到視頻合成比例的要求,如果達不到下面條件視頻則會無法播放
* 圖片寬:必須要被32整除
* 圖片高:必須要被2整除
*/
int [] maxWH = getImgMaxWH(widthArray,heightArray);
recorder = new FFmpegFrameRecorder(mp4SavePath,maxWH[0],maxWH[1]);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
/**
* 視頻質量:目前測試出來的是25-30最清晰,視頻質量范圍好像是0-40,具體可以自己慢慢測
*/
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中怎樣實現文件內容合並

FileWriter類的構造方法,就有一個參數是直接追加到文件末尾寫入的
------------------------
FileWriter
public FileWriter(File file,
boolean append)
throws IOException根據給定的 File 對象構造一個 FileWriter 對象。如果第二個參數為 true,則將位元組寫入文件末尾處,而不是寫入文件開始處。

參數:
file - 要寫入數據的 File 對象
append - 如果為 true,則將位元組寫入文件末尾處,而不是寫入文件開始處
拋出:
IOException - 如果該文件存在,但它是一個目錄,而不是一個常規文件;或者該文件不存在,但無法創建它;抑或因為其他某些原因而無法打開它
從以下版本開始:
1.4

⑻ (高分)怎樣講手機的幾個java文件合並成一個java文件

件分割合並工具的Java源碼2007/02/21 05:38 A.M.public class FileCut
/*cl_tl*/
{
private Shell shell;
private Display display;
private Text txtSourceFile;
private Text txtNewFilePath;
private Text txtFileSize;
private Button btnChooseFile;
private Button btnChoosePath;
private Button btnCut;
private Button btnUnionFiles;
private Button btnUp;
private Button btnDown;
private Button btnClear;
private Button btnClearAll;
private Button btnUnion;
private Table tblFileList;

private File sourceFile = null; /*源文件*/
private File[] newFile = null; /*分割後產生的文件*/
private int fileCount = 0; /*分割成的文件個數*/
private int fileSize = 0; /*分割的文件塊大小*/
private String strSourceFile = null; /*源文件路徑及名稱*/
private String strNewFilePath = null; /*分割文件存放路徑*/

public static void main(String[] args)
{
Display display=new Display();
FileCut Item=new FileCut();
Item.createShell();

while( !Item.shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

/*fn_hd
*rem:創建窗體
*aut:
*log:2005-11-24
*/
private void createShell()
/*fn_tl*/
{
shell = new Shell(display, SWT.MIN);
shell.setBounds(300,250,500,330);
shell.setText("文件分割合並");
GridLayout shellLayout = new GridLayout();
shellLayout.numColumns = 3;
shell.setLayout(shellLayout);
createWidgets();
shell.open();
}

/**fn_hd
*rem:在窗體內添加控制項
*per:
*aut:
*log:2005-11-24
*/
private void createWidgets()
/*fn_tl*/
{
final Label lblNull0 = new Label(shell,SWT.None);
GridData gd0 = new GridData();
gd0.horizontalSpan = 3;
lblNull0.setLayoutData(gd0);

final Label lblSourceFile = new Label(shell, SWT.None);
lblSourceFile.setText("源 文 件");

GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
txtSourceFile = new Text(shell, SWT.BORDER);
txtSourceFile.setLayoutData(gd2);

btnChooseFile = new Button(shell, SWT.PUSH);
btnChooseFile.setText("..");

final Label lblPath = new Label(shell, SWT.None);
lblPath.setText("存放路徑");

txtNewFilePath = new Text(shell, SWT.BORDER);
GridData gd3 = new GridData(GridData.FILL_HORIZONTAL);
txtNewFilePath.setLayoutData(gd3);

btnChoosePath = new Button(shell, SWT.PUSH);
btnChoosePath.setText("..");

final Label lblSize = new Label(shell, SWT.None);
lblSize.setText("分塊大小(KB)");

txtFileSize = new Text(shell,SWT.BORDER);
GridData gd7 = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
txtFileSize.setLayoutData(gd7);

btnCut = new Button(shell, SWT.PUSH);
GridData gd4 = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
btnCut.setLayoutData(gd4);
btnCut.setText("開始分割");

final Label lbl1 = new Label(shell, SWT.None);
GridData gd8 = new GridData();
gd8.horizontalSpan = 3;
lbl1.setLayoutData(gd8);
lbl1.setText("待合並的文件列表");

tblFileList = new Table(shell, SWT.BORDER);
GridData gd1 = new GridData(GridData.FILL_BOTH);
gd1.horizontalSpan = 2;
tblFileList.setLayoutData(gd1);

Composite com = new Composite(shell, SWT.None);
GridLayout comLayout = new GridLayout();
com.setLayout(comLayout);

final Label lblNote = new Label(shell, SWT.None);
GridData data = new GridData();
data.horizontalSpan=3;
lblNote.setLayoutData(data);
lblNote.setText("提示:注意合並文件的順序");

btnUnionFiles = new Button(com, SWT.PUSH);
btnUnionFiles.setText("選擇文件");

btnUp = new Button(com, SWT.PUSH);
btnUp.setText(" 上移 ");
btnUp.setEnabled(false);

btnDown = new Button(com, SWT.PUSH);
btnDown.setText(" 下移 ");
btnDown.setEnabled(false);

btnClear = new Button(com, SWT.PUSH);
btnClear.setText(" 刪除 ");
btnClear.setEnabled(false);

btnClearAll = new Button(com, SWT.PUSH);
btnClearAll.setText("清空列表");

btnUnion = new Button(com, SWT.PUSH);
btnUnion.setText("開始合並");

btnCut.addSelectionListener(new SelectionAdapter()
//添加"開始分割"監視器
{
public void widgetSelected(SelectionEvent event)
{
cutButtonEvent();
}
});

btnChooseFile.addSelectionListener(new SelectionAdapter()
//添加選擇(源文件)監視器
{
public void widgetSelected(SelectionEvent event)
{
FileDialog fdOpen = new FileDialog(shell,SWT.OPEN);
String strFileName = fdOpen.open();
if (strFileName != null)
{
txtSourceFile.setText(strFileName);
txtNewFilePath.setText(fdOpen.getFilterPath());
txtFileSize.setFocus();
}
}
});

btnChoosePath.addSelectionListener(new SelectionAdapter()
//添加選擇(分割文件存放路徑)監視器
{
public void widgetSelected(SelectionEvent event)
{
DirectoryDialog dirDia = new DirectoryDialog(shell);
String strFileDir = dirDia.open();
if (strFileDir != null)
{
txtNewFilePath.setText(strFileDir);
txtFileSize.setFocus();
}
}
});

btnUp.addSelectionListener(new SelectionAdapter()
//添加"上移"監視器
{
public void widgetSelected(SelectionEvent event)
{
//int[] itemIndices = tblFileList.getSelectionIndices();
int itemIndex = tblFileList.getSelectionIndex();
if (itemIndex == 0)
{
tblFileList.setFocus();
return;
}
//交換列表中兩行的內容
String strTemp = tblFileList.getItem(itemIndex).getText();
tblFileList.getItem(itemIndex).setText(tblFileList.getItem(
itemIndex - 1).getText());
tblFileList.getItem(itemIndex - 1).setText(strTemp);
//設置焦點
tblFileList.setSelection(itemIndex - 1);
tblFileList.setFocus();
}
});

btnDown.addSelectionListener(new SelectionAdapter()
//添加"下移"監視器
{
public void widgetSelected(SelectionEvent event)
{
//int[] itemIndices = tblFileList.getSelectionIndices();
int itemIndex = tblFileList.getSelectionIndex();
if (itemIndex == tblFileList.getItemCount() - 1)
{
tblFileList.setFocus();
return;
}
//交換列表中兩行的內容
String strTemp = tblFileList.getItem(itemIndex).getText();
tblFileList.getItem(itemIndex).setText(tblFileList.getItem(
itemIndex + 1).getText());
tblFileList.getItem(itemIndex + 1).setText(strTemp);
//設置焦點
tblFileList.setSelection(itemIndex + 1);
tblFileList.setFocus();
}
});

btnClear.addSelectionListener(new SelectionAdapter()
//添加"刪除"監視器
{
public void widgetSelected(SelectionEvent event)
{
int itemIndex = tblFileList.getSelectionIndex();
tblFileList.remove(itemIndex);
btnUp.setEnabled(false);
btnDown.setEnabled(false);
btnClear.setEnabled(false);
}
});

btnClearAll.addSelectionListener(new SelectionAdapter()
//添加"清空列表"監視器
{
public void widgetSelected(SelectionEvent event)
{
tblFileList.removeAll();
btnUp.setEnabled(false);
btnDown.setEnabled(false);
btnClear.setEnabled(false);
}
});

txtFileSize.addSelectionListener(new SelectionAdapter()
//添加"分塊大小"文本框中輸入回車監視器
{
public void widgetDefaultSelected(SelectionEvent event)
{
cutButtonEvent();
}
});

btnUnionFiles.addSelectionListener(new SelectionAdapter()
//添加"選擇文件"監視器
{
public void widgetSelected(SelectionEvent event)
{
FileDialog fd = new FileDialog(shell, SWT.MULTI);
fd.setFilterExtensions(new String[]{"*.dat", "*.*"});
if (fd.open() != null)
{
String[] strFiles = fd.getFileNames();
String strFilePath = fd.getFilterPath();
//strUnionFilePath = new String[strFiles.length];
for(int i = 0; i < strFiles.length; i++)
{
//strUnionFilePath[i] = fd.getFilterPath();
TableItem item = new TableItem(tblFileList, SWT.None);
item.setText(strFilePath + "\\" + strFiles[i]);
}
}
}
});

btnUnion.addSelectionListener(new SelectionAdapter()
//添加"開始合並"監視器
{
public void widgetSelected(SelectionEvent event)
{
if (tblFileList.getItemCount() == 0)
{
return;
}
switch (unionFiles())
{
case 1:
showMessage("成功", "合並完成!",
SWT.OK | SWT.ICON_INFORMATION);
tblFileList.removeAll();
btnUp.setEnabled(false);
btnDown.setEnabled(false);
btnClear.setEnabled(false);
break;
case -1:
showMessage("錯誤", "文件不存在!",
SWT.OK | SWT.ICON_ERROR);
break;
case -2:
break;
default:
showMessage("錯誤", "有錯誤發生,文件合並失敗!",
SWT.OK | SWT.ICON_ERROR);
}
}
});

tblFileList.addSelectionListener(new SelectionAdapter()
//添加選中列表中元素監視器
{
public void widgetSelected(SelectionEvent event)
{
btnUp.setEnabled(true);
btnDown.setEnabled(true);
btnClear.setEnabled(true);
}
});
}

/**fn_hd
*rem:顯示消息框
*log:2005-11-24
*/
private int showMessage(String strText, String strMessage, int i)
{/*fn_tl*/
MessageBox msgBox = new MessageBox(shell,i);
msgBox.setText(strText);
msgBox.setMessage(strMessage);
return msgBox.open();
}

/**fn_hd
*rem:點擊"分割"按鈕觸發的事件響應
*log:2005-11-24
*/
private void cutButtonEvent()
/*fn_tl*/
{
strSourceFile = txtSourceFile.getText().trim();
strNewFilePath = txtNewFilePath.getText().trim();

if (strSourceFile.equals("") || strNewFilePath.equals(""))
{
showMessage("提示", "請輸入源文件和 \n\n分割文件的路徑! ",
SWT.OK | SWT.ICON_INFORMATION);
return;
}
try
{
fileSize = Integer.parseInt(txtFileSize.getText());
fileSize *= 1024;
if (fileSize <= 0)
{
showMessage("錯誤", "分塊大小為正整數! ",
SWT.OK | SWT.ICON_ERROR);
return;
}
}
catch(Exception e)
{
showMessage("錯誤", "請在分塊大小框填入數字! ",
SWT.OK | SWT.ICON_ERROR);
return;
}
switch (cutFile())
{
case 1:
showMessage("提示", "分割完成! ", SWT.OK |
SWT.ICON_INFORMATION);
txtSourceFile.setText("");
txtNewFilePath.setText("");
txtFileSize.setText("");
break;
case -1:
showMessage("錯誤", "源文件不存在或存放路徑不存在!",
SWT.OK | SWT.ICON_ERROR);
break;
default:
showMessage("未知錯誤", "文件分割失敗! ",
SWT.OK | SWT.ICON_ERROR);
}
}

/*fn_hd
*rem:文件分割實現
*per:成功返回1,文件未找到返回-1,其他情況返回0
*exp:IOException
*aut:
*log:2005-11-22,創建
*log:2005-11-24,修改
*/
private int cutFile()
/*fn_tl*/
{
sourceFile = new File(strSourceFile);
fileCount = (int) (sourceFile.length() / fileSize);
if (sourceFile.length() % fileSize != 0)
{
fileCount++;
}
newFile = new File[fileCount];

try
{
int count = 0;
int i = 0;
byte[] bueff = new byte[fileSize];
FileOutputStream out = null;
FileInputStream in = new FileInputStream(sourceFile);
for (i = 0; i < newFile.length; i++)
{
newFile[i] = new File(strNewFilePath,
i + sourceFile.getName() + ".dat");
}
i = 0;
while ((count = in.read(bueff,0,fileSize)) != -1)
{
out = new FileOutputStream(newFile[i]);
out.write(bueff,0,count);
out.close();
i++;
}
in.close();
return 1;
}
catch(FileNotFoundException e)
{
System.out.println(e);
return -1;
}
catch(IOException e)
{
System.out.println(e);
return 0;
}
}

/*fn_hd
*rem:文件合並的實現
*per:成功返回1,文件未找到返回-1,取消操作返回-2,其他情況返回0;
*aut:
*exp:FileNotFoundException,IOException
*log:2005-11-28,創建
*/
private int unionFiles()
/*fn_tl*/
{
String[] strFiles = new String[tblFileList.getItemCount()];
File[] unionFiles = new File[strFiles.length];
FileDialog fdSave = new FileDialog(shell, SWT.SAVE);
String s = fdSave.open();
if (s == null)
{
return -2;
}
File outFile = new File(fdSave.getFilterPath(),
fdSave.getFileName());
if (outFile.exists())
{
int msg = showMessage("提示", "該文件以存在,是否替換?",
SWT.YES | SWT.NO | SWT.ICON_QUESTION);
if (msg == SWT.NO)
{
return -2;
}
}
for(int i = 0; i < strFiles.length; i++)
{
strFiles[i] = tblFileList.getItem(i).getText();
}
try
{
FileInputStream in = null;
FileOutputStream out = new FileOutputStream(outFile);
byte[] buff = new byte[1024];
int count;
for(int i = 0; i < strFiles.length; i++)
{
in = new FileInputStream(strFiles[i]);
while((count = in.read(buff,0,1024)) != -1)
{
out.write(buff,0,count);
}
in.close();
}
out.close();
return 1;
}
catch(FileNotFoundException e)
{
System.out.println(e);
return -1;
}
catch(IOException e)
{
System.out.println(e);
return 0;
}
}

⑼ java 怎麼合並兩個wav文件

//幫你寫了一個,是兩個mp3文件的合並
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
/**
*把兩個.mp3文件合並成一個.mp3文件
*
*@authorwangran
*
*/
publicclassMerger{
publicMerger(){
}
publicstaticvoidmain(String[]args){
FileInputStreamfis=null;
FileOutputStreamfos=null;
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
//源文件
Filein1=newFile("D:/雜/娛樂/音樂/hero.mp3");
Filein2=newFile("D:/雜/娛樂/音樂/carelesswhisper.mp3");

//目標文件
Fileout=newFile("D:/music2.mp3");

//進行流操作
try{
fis=newFileInputStream(in1);
fos=newFileOutputStream(out,true);
bis=newBufferedInputStream(fis);
bos=newBufferedOutputStream(fos);
intlen;
byte[]buf=newbyte[1024];
while((len=bis.read(buf))!=-1){
bos.write(buf,0,len);
}
bos.flush();
fis=newFileInputStream(in2);
bis=newBufferedInputStream(fis);
while((len=bis.read(buf))!=-1){
bos.write(buf,0,len);
}
bos.flush();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
//關閉流
if(bis!=null)
try{
bis.close();
}catch(IOExceptione){
e.printStackTrace();
}
if(bos!=null)
try{
bos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}

閱讀全文

與java合並視頻相關的資料

熱點內容
二進制流轉pdf 瀏覽:913
php判斷爬蟲 瀏覽:567
960除24除4簡便演算法 瀏覽:786
關於解壓英語翻譯 瀏覽:564
python控制鍵盤右鍵 瀏覽:920
php沒有libmysqldll 瀏覽:828
時政新聞app哪個好 瀏覽:906
手機已加密怎麼辦 瀏覽:201
安卓手機截屏怎麼傳到蘋果 瀏覽:527
京管家app哪裡下載 瀏覽:33
文件夾橫向排列的豎向排列 瀏覽:451
51單片機驅動攝像頭模塊 瀏覽:689
政府文件加密沒法轉換 瀏覽:373
android判斷棧頂 瀏覽:331
憑證軟體源碼 瀏覽:860
androidwebview滾動事件 瀏覽:11
如何將電腦上的圖片壓縮成文件包 瀏覽:899
程序員轉金融IT 瀏覽:837
黑馬程序員培訓效果如何 瀏覽:913
本地集成編譯 瀏覽:528