사진을 mp3 파일에 추가하여 mp4 영화에 결합하려고합니다. 동영상의 길이는 사용자가 mp3 파일의 길이를 선택하거나 수동으로 선택할 수 있습니다. 그리고 사용자가 manual (length! = mp3 파일 길이)을 선택하면 mp3 파일을 잘라내거나 반복해야합니다.xuggler를 사용하여 사진과 mp3 java에서 mp4 만들기
없음은이 그것을 할 올바른 방법으로하지 않습니다주의하시기 바랍니다. 사진으로 만 소리 :(
private void convertImageToVideo() {
IMediaWriter writer = ToolFactory.makeWriter(outputFilename);
long delay = videotime/PicPathList.size();
long milliseconds = 0;
//adds Pictures to the mp4 stream
for (int i = 0; i < PicPathList.size(); i++) {
BufferedImage bi;
try {
bi = ImageIO.read(new File(PicPathList.get(i)));
bi = Tools.prepareForEncoding(bi);
int width=bi.getWidth();
int height=bi.getHeight();
if(width%2==1){
width++;
}
if(height%2==1){
height++;
}
if (i == 0) {
writer.addVideoStream(0, 0, ID.CODEC_ID_H264, width, height);
}
//debug
// System.out.println(PicPathList.get(i) + ", bi:" + bi.getWidth() + "x"
// + bi.getHeight() + ", ms:" + milliseconds);
writer.encodeVideo(0, bi, milliseconds, TimeUnit.MILLISECONDS);
milliseconds += delay;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error");
}
}
writer.close();
//at this part Im trying to combine the further generated mp4 file with the mp3 file
String inputVideoFilePath = outputFilename;
String inputAudioFilePath = this.musicFile.getAbsolutePath();
String outputVideoFilePath = "outputFilename";
IMediaWriter mWriter = ToolFactory.makeWriter(outputVideoFilePath);
IContainer containerVideo = IContainer.make();
IContainer containerAudio = IContainer.make();
// check files are readable
containerVideo.open(inputVideoFilePath, IContainer.Type.READ, null);
containerAudio.open(inputAudioFilePath, IContainer.Type.READ, null);
// read video file and create stream
IStreamCoder coderVideo = containerVideo.getStream(0).getStreamCoder();
IPacket packetvideo = IPacket.make();
int width = coderVideo.getWidth();
int height = coderVideo.getHeight();
// read audio file and create stream
IStreamCoder coderAudio = containerAudio.getStream(0).getStreamCoder();
IPacket packetaudio = IPacket.make();
mWriter.addAudioStream(1, 0,coderAudio.getCodecID(), coderAudio.getChannels(), coderAudio.getSampleRate());
mWriter.addVideoStream(0, 0, width, height);
while (containerVideo.readNextPacket(packetvideo) >= 0) {
containerAudio.readNextPacket(packetaudio);
// video packet
IVideoPicture picture = IVideoPicture.make(coderVideo.getPixelType(), width, height);
coderVideo.decodeVideo(picture, packetvideo, 0);
if (picture.isComplete())
mWriter.encodeVideo(0, picture);
// audio packet
IAudioSamples samples = IAudioSamples.make(512, coderAudio.getChannels(), IAudioSamples.Format.FMT_S32);
coderAudio.decodeAudio(samples, packetaudio, 0);
if (samples.isComplete())
mWriter.encodeAudio(1, samples);
}
coderAudio.close();
coderVideo.close();
containerAudio.close();
containerVideo.close();
mWriter.close();
}