「java视频合并」java合并视频文件

博主:adminadmin 2022-12-03 05:45:07 61

本篇文章给大家谈谈java视频合并,以及java合并视频文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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合并多个文件

使用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) {}

  }

 }

 //下面代码是将D盘的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"});

 }

}

java 怎么合并两个wav文件?

//帮你写了一个,是两个mp3文件的合并

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

/**

 * 把两个.mp3文件合并成一个.mp3文件

 * 

 * @author wangran

 *

 */

public class Merger {

 public  Merger() {

 }

 public static void main(String[] args) {

  FileInputStream fis = null;

  FileOutputStream fos = null;

  BufferedInputStream bis = null;

  BufferedOutputStream bos = null;

  //源文件

  File in1 = new File("D:/杂/娱乐/音乐/hero.mp3");

  File in2 = new File("D:/杂/娱乐/音乐/careless whisper.mp3");

  

  //目标文件

  File out = new File("D:/music2.mp3");

  

  //进行流操作

  try {

   fis = new FileInputStream(in1);

   fos = new FileOutputStream(out, true);

   bis = new BufferedInputStream(fis);

   bos = new BufferedOutputStream(fos);

   int len;

   byte[] buf = new byte[1024];

   while ((len = bis.read(buf))!=-1) {

    bos.write(buf,0,len);

   }

   bos.flush();

   fis = new FileInputStream(in2);

   bis = new BufferedInputStream(fis);

   while ((len = bis.read(buf)) != -1) {

    bos.write(buf,0,len);

   }

   bos.flush();

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  } catch (IOException e) {

   e.printStackTrace();

  } finally {

   //关闭流

   if (bis != null)

    try {

     bis.close();

    } catch (IOException e) {

     e.printStackTrace();

    }

   if (bos != null)

    try {

     bos.close();

    } catch (IOException e) {

     e.printStackTrace();

    }

  }

 }

}

java中如何将两个文件合并到另一个文件

java可以使用FileChannel快速高效地将多个文件合并到一起,以下是详细代码:

    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视频合并和java合并视频文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-03,除非注明,否则均为首码项目网原创文章,转载请注明出处。