我一直在寻找解决方案!在这个网站和其他人。 我发现了一些有趣的东西,但它们并没有解决我的问题。我会解释一下。
我有一个视频,我用xuggler对每一帧进行渲染。当我得到所有帧时,我用颜色算法编辑所有帧。另外,我将音频存储在mp3文件中。
现在我需要从所有帧创建一个视频,当然,这个视频应该具有与帧率e持续时间相同的特性。之后我必须合并音频。
我已经完成了第一部分,但我不知道如何创建具有相同特征的视频。我遵循以下代码:
http://www.javacodegeeks.com/2011/02/xuggler-tutorial-frames-capture-video.html
Can't encode video with Xuggler
但它需要快照并使用一个奇怪的循环:
for (int index = 0; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++)
我无法弄清楚如何设置正确的特性。它应该很简单,因为我了解视频的一切!大小,帧速率和帧数。
我的代码:
public static void main(String[] args) throws IOException {
final IMediaWriter writer = ToolFactory.makeWriter(outputFilename);
writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4,
720, 304);
long nextFrameTime = 0;
final long frameRate =25/1000;
long startTime = System.nanoTime();
while (indexVideo<1597) {
BufferedImage videoImage = null;
try {
videoImage = getVideoImage();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
writer.encodeVideo(0, videoImage,nextFrameTime,
TimeUnit.MILLISECONDS);
nextFrameTime += frameRate;
}
writer.close();
}
private static BufferedImage getVideoImage() throws IOException, AWTException {
File imgLoc = new File("D:/Gianfranco/Sample/"+indexVideo+".png");
BufferedImage img;
img = ImageIO.read(imgLoc);
System.out.println(imgLoc.getName());
indexVideo++;
return img;
}
Ca有人帮帮我吗?
答案 0 :(得分:1)
快速浏览一下代码,可以看出您没有设置帧速率。我不知道你正在使用什么版本的xuggler,但对我来说,我在添加视频流时设置帧速率:
writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, IRational.make(<frames>/<per second>), 720, 304);
现在另一种(可能更合适)的方法是通过打开原始视频来创建IMediaWriter。
IMediaReader reader = ToolFactory.makeReader("inputFile.mp4");
IMediaWriter writer = ToolFactory.makeWriter("outputFile.mp4", reader);
或许你可以抓住第一个容器,然后抓住格式。我只对流数据而不是归档数据这样做了。
答案 1 :(得分:0)
尝试使用帧速率:
final long frameRate =DEFAULT_TIME_UNIT.convert(25, MILLISECONDS);
答案 2 :(得分:0)
package com.Raamji.Work;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.xuggler.ICodec;
public class VideoGenerator {
private static final double FRAME_RATE = 20;
private static final int SECONDS_TO_RUN_FOR = 20;
private static final String outputFilename = "C:/myVideo.mp4";
private static Dimension screenBounds;
private static Map<String, File> imageMap = new HashMap<String, File>();
public static void main(String[] args) {
final IMediaWriter writer = ToolFactory.makeWriter(outputFilename);
screenBounds = Toolkit.getDefaultToolkit().getScreenSize();
writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4,screenBounds.width / 2, screenBounds.height / 2);
File folder = new File("C:/Users/arsingh/Desktop/tempo/video");
File[] listOfFiles = folder.listFiles();
int indexVal = 0;
for (File file : listOfFiles) {
if (file.isFile()) {
indexVal++;
System.out.println("file.getName() :"+file.getName());
imageMap.put(file.getName(), file);
}
}
//for (int index = 0; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++) {
for (int index = 1; index <= listOfFiles.length; index++) {
BufferedImage screen = getImage(index);
BufferedImage bgrScreen = convertToType(screen, BufferedImage.TYPE_3BYTE_BGR);
writer.encodeVideo(0, bgrScreen, 300*index, TimeUnit.MILLISECONDS);
}
// tell the writer to close and write the trailer if needed
writer.close();
System.out.println("Video Created");
}
public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) {
BufferedImage image;
if (sourceImage.getType() == targetType) {
image = sourceImage;
}
else {
image = new BufferedImage(sourceImage.getWidth(),
sourceImage.getHeight(), targetType);
image.getGraphics().drawImage(sourceImage, 0, 0, null);
}
return image;
}
private static BufferedImage getImage(int index) {
try {
String fileName=index+".jpg";
System.out.println("fileName :" + fileName);
File img = imageMap.get(fileName);
BufferedImage in=null;
if (img != null) {
System.out.println("img :"+img.getName());
in = ImageIO.read(img);
}else
{
System.out.println("++++++++++++++++++++++++++++++++++++++index :" + index);
img = imageMap.get(1);
in = ImageIO.read(img);
}
return in;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}