我的视频 bailey.mpg 我使用 xuggler 方法从中创建了 .png 图像,然后我读取字节数组中的每个图像,并将哈希作为分隔符和文本数据附加到这个字节数组中,并使用这个字节数组重新创建图像。
现在我正在从这组(文本附加图像)图像中重建 .avi 格式的视频。通过使用 xuggler 从新的 avi 视频中获取 .png 图像集。我正在读取字节数组中的每个图像,并且我正在图像集的字节数组中搜索分隔符,但我无法找到哈希分隔符。我认为这意味着文本视频创建过程中的数据丢失。我应该怎么办?
从视频包DifferentialPackage创建图像的代码;
import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.MediaListenerAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IVideoPictureEvent;
import com.xuggle.xuggler.Global;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author pratibha
*/
public class VideoIntoFrames {
public static final double SECONDS_BETWEEN_FRAMES =1;
String inputFilename;
private static final String outputFilePrefix = "C:\\photo\\image_";
// The video stream index, used to ensure we display frames from one and
//only one video stream from the media container.
private static int mVideoStreamIndex = -1;
// Time of last frame write
private static long mLastPtsWrite = Global.NO_PTS;
public static final long MICRO_SECONDS_BETWEEN_FRAMES =(long)(100 * SECONDS_BETWEEN_FRAMES);
int FrameNo=0;
public VideoIntoFrames(String filepath){
inputFilename=filepath;
IMediaReader mediaReader = ToolFactory.makeReader(inputFilename);
// stipulate that we want BufferedImages created in BGR 24bit color space
mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);
mediaReader.addListener(new ImageSnapListener());
// read out the contents of the media file and
// dispatch events to the attached listener
while (mediaReader.readPacket() == null) ;
}
private class ImageSnapListener extends MediaListenerAdapter {
public void onVideoPicture(IVideoPictureEvent event) {
if (event.getStreamIndex() != mVideoStreamIndex) {
// if the selected video stream id is not yet set, go ahead an
// select this lucky video stream
if (mVideoStreamIndex == -1)
mVideoStreamIndex = event.getStreamIndex();
// no need to show frames from this video stream
else
return;
}
// if uninitialized, back date mLastPtsWrite to get the very first frame
if (mLastPtsWrite == Global.NO_PTS)
mLastPtsWrite = event.getTimeStamp() - MICRO_SECONDS_BETWEEN_FRAMES;
// if it's time to write the next frame
if (event.getTimeStamp() - mLastPtsWrite >=
MICRO_SECONDS_BETWEEN_FRAMES) {
++FrameNo;
String outputFilename = dumpImageToFile(event.getImage());
// indicate file written
double seconds = ((double) event.getTimeStamp()) /
Global.DEFAULT_PTS_PER_SECOND;
System.out.printf(
"at elapsed time of %6.3f seconds wrote: %s\n",
seconds, outputFilename);
// update last write time
mLastPtsWrite += MICRO_SECONDS_BETWEEN_FRAMES;
}
}
private String dumpImageToFile(BufferedImage image) {
try {
String outputFilename = outputFilePrefix +FrameNo+ ".gif";
ImageIO.write(image, "jpg", new File(outputFilename));
return outputFilename;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
public static void main(String args[]){
String path="D:/bailey.mpg";
VideoIntoFrames v=new VideoIntoFrames(path);
}
}
所有图像都保存在 c:/photo 中。在 image_1.gif 中插入文本数据的代码是
try{
String data="My Name is ";
int[] charValue=new int[data.length()];
for(int rowIndex=0;rowIndex<charValue.length;rowIndex++){
charValue[rowIndex]=data.charAt(rowIndex);
}
File videoFile = new File("C:/photo/image_1.gif");
FileInputStream videoInput = new FileInputStream(videoFile);
int VideoByte = -1;
List<Byte> bytes = new ArrayList<Byte>();
while ((VideoByte = videoInput.read()) != -1) {
bytes.add((byte) VideoByte);
}
byte[] ByteOfVideo = new byte[bytes.size()];
for (int count = 0; count < ByteOfVideo.length; count++) {
ByteOfVideo[count] = bytes.get(count);
// System.out.println(count+" of ByteOfImage "+ByteOfVideo[count]);
}
////////////////////////////////////#////////////////////////
/////now insert actual image fRames,Row and Columns
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
for (int i = 0; i < ByteOfVideo.length; i++) {
byteOut.write(ByteOfVideo[i]);
}
byte[] HashArray = "#".getBytes();
byteOut.write(HashArray);
byteOut.write(HashArray);
byteOut.write(HashArray);
byteOut.write(HashArray);
byteOut.write(HashArray);
/// String retrievedString = new String(FrameByteArray, "UTF-8");
for (int i = 0; i < charValue.length; i++) {
System.out.println(" NameArray in Bytes" + charValue[i]);
byteOut.write(charValue[i]);
}
////insert #
//////write this Video File
String FinalModifiedVideo="C:\\photo\\image_1.gif";
File ModifiedFile=new File(FinalModifiedVideo);
DataOutputStream out=new DataOutputStream(new FileOutputStream(ModifiedFile));
byteOut.writeTo(out);
out.close();
System.out.println("Process End");
}
catch(Exception e){
e.printStackTrace();
}
从图像集(c:/photo 中的图像)文件夹创建视频的代码。此创建 image.avi 视频
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Frame;
import test.*;
import ch.randelshofer.media.avi.AVIOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Inbo
*/
public class MyVideoWriter {
static ArrayList<String> img = new ArrayList();
public static void readFiles() {
String path = "C:\\photo\\";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
int c = 0;
for (int i = 0; i <799; i++) {
img.add(path + "\\image_" + (i + 1) + ".gif");
}
// System.out.println(img);
}
public MyVideoWriter(String path) {
readFiles();
try {
AVIOutputStream AVIout = new AVIOutputStream(new File(path + ".avi"), AVIOutputStream.VideoFormat.JPG);
AVIout.setVideoCompressionQuality(1);
//AVIout.setFrameRate(10);
AVIout.setVideoDimension(352, 240);
for (int i = 0; i < img.size(); i++) {
AVIout.writeFrame(new File(img.get(i)));
}
AVIout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
String path="C:\\image";
MyVideoWriter mv = new MyVideoWriter(path);
}
}