【发布时间】:2020-03-09 23:47:21
【问题描述】:
我写了一个程序,反转一个视频。但是当我试图打开视频文件时,它显示了一个错误。文件已损坏。当我尝试反转文本文件时,它可以工作,但视频已损坏。我通过从文件中读取字节并将其以相反的顺序存储到另一个文件中来实现这一点。 请参阅下面的代码..
// The beginning of the program
package IO;
import java.io.*;
public class Reverse {
public static void main(String[] args) {
// videoName is the video file to be reversed**
String videoName = "/Users/Noah/Videos/362f42d24752447aacb3f263c58472ba.mp4";
// pathName is the output folder path**
String pathName = "/Users/Noah/Desktop/video file.mp4";
File vFile = new File(videoName);
if (vFile.exists()) {
FileInputStream fIn = null;
FileOutputStream fOut = null;
try {
fIn = new FileInputStream(vFile);
fOut = new FileOutputStream(pathName);
int availableData = fIn.available();
byte[] buffer = new byte[availableData];
// read all data into buffer**
int readData = fIn.read(buffer);
// write data to output folder path in reverse
// using a countdown**
if (readData != -1) {
for (int i = buffer.length - 1; i >= 0; i--) {
fOut.write(buffer[i]);
System.out.println('*'); // not necessary
} // end for
} else System.out.println("The file was not read properly, try again");
// end if
} catch (IOException ignored) {
} finally {
try {
if (fIn != null && fOut != null) {
fIn.close(); // close input stream
fOut.close(); // close output stream
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else System.out.println("File does not exists");
System.out.println("File read successfully");
}
}
【问题讨论】:
-
我认为这是因为您需要保留表示视频标头的初始 X 个字节,并且只反转作为视频的有效负载。
-
你的意思是,把它倒过来让它倒着播放?您需要了解视频格式。
-
@Jason,我不明白你说的保留最初的 X 字节是什么意思。
-
@user241802 大多数(如果不是全部)文件不仅包含构成文件内容的原始字节。它们还包含一个字节的信息,告诉解码器(即媒体播放器)如何读取文件。即文件的版本、数据、上次修改时间等。因此,您不能简单地反转所有字节并希望它有效,因为现在标头信息现在位于文件末尾。您需要保留构成标头的字节,并且只反转有效负载(视频本身)。调查一下; en.wikipedia.org/wiki/MPEG-4_Part_14
-
@user241802 反转视频意味着以相反的顺序放置帧;这更像是反转字符串中的单词,而不是反转整个字符串:“foo bar baz”反转为“baz bar foo”,而不是“zab rab oof”。您需要了解数据的结构才能正确反转它。
标签: java file-handling