导航:首页 > 编程语言 > 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合并视频相关的资料

热点内容
怎么把百度云资源压缩 浏览:456
不会数学英语如何编程 浏览:88
如何能知道网站服务器地址 浏览:648
程序员月薪5万难吗 浏览:138
如何评价程序员 浏览:803
云虚机和服务器的区别 浏览:403
广西柳州压缩机厂 浏览:639
arm开发编译器 浏览:833
51单片机的核心 浏览:746
看电视直播是哪个app 浏览:958
将c源程序编译成目标文件 浏览:787
再要你命3000pdf 浏览:558
ai软件解压软件怎么解压 浏览:520
文件夹怎样设置序列号 浏览:963
javascriptgzip压缩 浏览:248
易语言怎么取出文件夹 浏览:819
苹果xs手机加密app哪里设置 浏览:605
超声雾化器与压缩雾化器 浏览:643
模拟实现进程调度算法 浏览:388
现在的压缩包都是加密 浏览:331