java 读取视频数据并写入另一个文件java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16095838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:53:49  来源:igfitidea点击:

Reading video data and writing to another file java

javafilefile-iocopyvideo-streaming

提问by Krish

I am reading a video file data in bytes and sending to another file but the received video file is not playing properly and is chattered.

我正在以字节为单位读取视频文件数据并将其发送到另一个文件,但接收到的视频文件无法正常播放并且出现抖动。

Can anyone explain me why this is happening and a solution is appreciated.

任何人都可以向我解释为什么会发生这种情况并感谢解决方案。

My code is as follows

我的代码如下

import java.io.*;

public class convert {

  public static void main(String[] args) {

    //create file object
    File file = new File("B:/music/Billa.mp4");

    try
    {
      //create FileInputStream object
      FileInputStream fin = new FileInputStream(file);


       byte fileContent[] = new byte[(int)file.length()];
       fin.read(fileContent);

       //create string from byte array
       String strFileContent = new String(fileContent);

       System.out.println("File content : ");
       System.out.println(strFileContent);

       File dest=new File("B://music//a.mp4");
       BufferedWriter bw=new BufferedWriter(new FileWriter(dest));
       bw.write(strFileContent+"\n");
       bw.flush();

    }
    catch(FileNotFoundException e)
    {
      System.out.println("File not found" + e);
    }
    catch(IOException ioe)
    {
      System.out.println("Exception while reading the file " + ioe);
    }
  }
}

回答by Jakub Orsula

This question might be dead but someone might find this useful.

这个问题可能已经死了,但有人可能会发现这很有用。

You can't handle video as string. This is the correct way to read and write (copy) anyfile using Java 7 or higher.

您不能将视频作为字符串处理。这是使用 Java 7 或更高版本读取和写入(复制)任何文件的正确方法。

Please note that size of buffer is processor-dependent and usually should be a power of 2. See this answerfor more details.

请注意,缓冲区的大小取决于处理器,通常应该是 2 的幂。有关更多详细信息,请参阅此答案

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileCopy {
public static void main(String args[]) {

    final int BUFFERSIZE = 4 * 1024;
    String sourceFilePath = "D:\MyFolder\MyVideo.avi";
    String outputFilePath = "D:\OtherFolder\MyVideo.avi";

    try(
            FileInputStream fin = new FileInputStream(new File(sourceFilePath));
            FileOutputStream fout = new FileOutputStream(new File(outputFilePath));
            ){

        byte[] buffer = new byte[BUFFERSIZE];

        while(fin.available() != 0) {
        fin.read(buffer);
        fout.write(buffer);
        }

    }
    catch(Exception e) {
        System.out.println("Something went wrong! Reason: " + e.getMessage());
    }

    }
}

回答by kousik Mridha

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;

import javax.imageio.ImageIO;

public class Reader {

    public Reader() throws Exception{


        File file = new File("C:/Users/Digilog/Downloads/Test.mp4");

        FileInputStream fin = new FileInputStream(file);
        byte b[] = new byte[(int)file.length()];
        fin.read(b);

        File nf = new File("D:/K.mp4");
        FileOutputStream fw = new FileOutputStream(nf);
        fw.write(b);
        fw.flush();
        fw.close();

    }

}

回答by Tarik Demirci

In addition to Jakub Orsula's answer, one needs to check the result of read operation to prevent garbage being written to end of file in last iteration.

除了Jakub Orsula 的回答之外,还需要检查读取操作的结果,以防止在上次迭代中将垃圾写入文件末尾。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileCopy {
public static void main(String args[]) {

    final int BUFFERSIZE = 4 * 1024;
    String sourceFilePath = "D:\MyFolder\MyVideo.avi";
    String outputFilePath = "D:\OtherFolder\MyVideo.avi";

    try(
            FileInputStream fin = new FileInputStream(new File(sourceFilePath));
            FileOutputStream fout = new FileOutputStream(new File(outputFilePath));
            ){

        byte[] buffer = new byte[BUFFERSIZE];
        int bytesRead;

        while(fin.available() != 0) {
        bytesRead = fin.read(buffer);
        fout.write(buffer, 0, bytesRead);
        }

    }
    catch(Exception e) {
        System.out.println("Something went wrong! Reason: " + e.getMessage());
    }

    }
}