【问题标题】:Reading video data and writing to another file java读取视频数据并写入另一个文件java
【发布时间】:2013-04-12 07:53:02
【问题描述】:

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

谁能解释一下为什么会发生这种情况并感谢解决方案。

我的代码如下

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);
    }
  }
}

【问题讨论】:

  • 1) 这与流式视频有什么关系?视频来源是文件! 2) 您不能将视频数据视为字符串或文本!不是。
  • new byte[(int)file.length()] 可能会从文件中截断字节,因为int 小于long。您需要将文件复制到块中
  • 感谢您的回复。我想加密数据以便将文件读入字符串。
  • 还要从 write 方法中删除“\n”。

标签: java file file-io copy video-streaming


【解决方案1】:

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

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

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

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) {
            bytesRead = fin.read(buffer);
            fout.write(buffer, 0, bytesRead);
        }
        
    }
    catch(Exception e) {
        System.out.println("Something went wrong! Reason: " + e.getMessage());
    }

    }
}

【讨论】:

    【解决方案2】:
    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();
    
        }
    
    }
    

    【讨论】:

    • 出于其他原因,我使用了一些头文件。不要为此烦恼
    • 任何 cmet 解释你的解决方案?
    【解决方案3】:

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

    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());
        }
    
        }
    }
    

    【讨论】:

      【解决方案4】:

      希望这对您也有帮助 - 这可以读取文件并将其写入另一个文件(您可以使用任何文件类型来执行此操作)

      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      
      public class Copy {
          public static void main(String[] args) throws Exception {
              FileInputStream input = new FileInputStream("input.mp4");      //input file
              byte[] data = input.readAllBytes();
              FileOutputStream output = new FileOutputStream("output.mp4");  //output file
              output.write(data);
              output.close();
          }
      }
      

      【讨论】:

      • 嗨,Suchin,欢迎来到 StackOverflow。由于这个问题很老而且似乎已经得到了彻底的回答,您应该解释您的解决方案如何改进以前的解决方案。作为一般规则,请避免在没有解释的情况下粘贴代码。
      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 2021-01-23
      • 2015-04-02
      • 2018-10-30
      • 1970-01-01
      • 2021-02-06
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多