Files
jpexs-decompiler/src/com/jpexs/decompiler/flash/exporters/MovieExporter.java
T
2014-06-22 00:25:28 +02:00

171 lines
7.4 KiB
Java

/*
* Copyright (C) 2014 JPEXS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.jpexs.decompiler.flash.exporters;
import com.jpexs.decompiler.flash.AbortRetryIgnoreHandler;
import com.jpexs.decompiler.flash.RetryTask;
import com.jpexs.decompiler.flash.RunnableIOEx;
import com.jpexs.decompiler.flash.SWF;
import com.jpexs.decompiler.flash.SWFInputStream;
import com.jpexs.decompiler.flash.SWFOutputStream;
import com.jpexs.decompiler.flash.exporters.modes.MovieExportMode;
import com.jpexs.decompiler.flash.exporters.settings.MovieExportSettings;
import com.jpexs.decompiler.flash.flv.FLVOutputStream;
import com.jpexs.decompiler.flash.flv.FLVTAG;
import com.jpexs.decompiler.flash.flv.VIDEODATA;
import com.jpexs.decompiler.flash.tags.DefineVideoStreamTag;
import com.jpexs.decompiler.flash.tags.Tag;
import com.jpexs.decompiler.flash.tags.VideoFrameTag;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
*
* @author JPEXS
*/
public class MovieExporter {
public List<File> exportMovies(AbortRetryIgnoreHandler handler, String outdir, List<Tag> tags, final MovieExportSettings settings) throws IOException {
List<File> ret = new ArrayList<>();
if (tags.isEmpty()) {
return ret;
}
File foutdir = new File(outdir);
if (!foutdir.exists()) {
if (!foutdir.mkdirs()) {
if (!foutdir.exists()) {
throw new IOException("Cannot create directory " + outdir);
}
}
}
for (Tag t : tags) {
if (t instanceof DefineVideoStreamTag) {
final DefineVideoStreamTag videoStream = (DefineVideoStreamTag) t;
final File file = new File(outdir + File.separator + ((DefineVideoStreamTag) t).getCharacterExportFileName() + ".flv");
new RetryTask(new RunnableIOEx() {
@Override
public void run() throws IOException {
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(exportMovie(videoStream, settings.mode));
}
}
}, handler).run();
}
}
return ret;
}
public byte[] exportMovie(DefineVideoStreamTag videoStream, MovieExportMode mode) throws IOException {
SWF swf = videoStream.getSwf();
HashMap<Integer, VideoFrameTag> frames = new HashMap<>();
SWF.populateVideoFrames(videoStream.characterID, swf.tags, frames);
if (frames.isEmpty()) {
return new byte[0];
}
//double ms = 1000.0f / ((float) frameRate);
ByteArrayOutputStream fos = new ByteArrayOutputStream();
//CopyOutputStream cos = new CopyOutputStream(fos, new FileInputStream("f:\\trunk\\testdata\\xfl\\xfl\\_obj\\streamvideo 7.flv"));
OutputStream tos = fos;
FLVOutputStream flv = new FLVOutputStream(tos);
flv.writeHeader(false, true);
//flv.writeTag(new FLVTAG(0, SCRIPTDATA.onMetaData(ms * frames.size() / 1000.0, videoStream.width, videoStream.height, 0, frameRate, videoStream.codecID, 0, 0, false, 0, fileSize)));
int horizontalAdjustment = 0;
int verticalAdjustment = 0;
for (int i = 0; i < frames.size(); i++) {
VideoFrameTag tag = frames.get(i);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int frameType = 1;
if ((videoStream.codecID == DefineVideoStreamTag.CODEC_VP6)
|| (videoStream.codecID == DefineVideoStreamTag.CODEC_VP6_ALPHA)) {
SWFInputStream sis = new SWFInputStream(swf, tag.videoData);
if (videoStream.codecID == DefineVideoStreamTag.CODEC_VP6_ALPHA) {
sis.readUI24("offsetToAlpha"); //offsetToAlpha
}
int frameMode = (int) sis.readUB(1, "frameMode");
if (frameMode == 0) {
frameType = 1; //intra
} else {
frameType = 2; //inter
}
sis.readUB(6, "qp"); //qp
int marker = (int) sis.readUB(1, "marker");
if (frameMode == 0) {
int version = (int) sis.readUB(5, "version");
int version2 = (int) sis.readUB(2, "version2");
sis.readUB(1, "interlace"); //interlace
if (marker == 1 || version2 == 0) {
sis.readUI16("offset"); //offset
}
int dim_y = sis.readUI8("dim_y");
int dim_x = sis.readUI8("dim_x");
sis.readUI8("render_y"); //render_y
sis.readUI8("render_x"); //render_x
horizontalAdjustment = (int) (dim_x * Math.ceil(((double) videoStream.width) / (double) dim_x)) - videoStream.width;
verticalAdjustment = (int) (dim_y * Math.ceil(((double) videoStream.height) / (double) dim_y)) - videoStream.height;
}
SWFOutputStream sos = new SWFOutputStream(baos, swf.version);
sos.writeUB(4, horizontalAdjustment);
sos.writeUB(4, verticalAdjustment);
}
if (videoStream.codecID == DefineVideoStreamTag.CODEC_SORENSON_H263) {
SWFInputStream sis = new SWFInputStream(swf, tag.videoData);
sis.readUB(17, "pictureStartCode");//pictureStartCode
sis.readUB(5, "version"); //version
sis.readUB(8, "temporalReference"); //temporalReference
int pictureSize = (int) sis.readUB(3, "pictureSize"); //pictureSize
if (pictureSize == 0) {
sis.readUB(8, "customWidth"); //customWidth
sis.readUB(8, "customHeight"); //customHeight
}
if (pictureSize == 1) {
sis.readUB(16, "customWidth"); //customWidth
sis.readUB(16, "customHeight"); //customHeight
}
int pictureType = (int) sis.readUB(2, "pictureType");
switch (pictureType) {
case 0: //intra
frameType = 1; //keyframe
break;
case 1://inter
frameType = 2;
break;
case 2: //disposable
frameType = 3;
break;
}
}
baos.write(tag.videoData);
flv.writeTag(new FLVTAG((int) Math.floor(i * 1000.0f / ((float) swf.frameRate)), new VIDEODATA(frameType, videoStream.codecID, baos.toByteArray())));
}
return fos.toByteArray();
}
}