Replace movies with commandline

This commit is contained in:
Jindra Petřík
2022-12-26 00:30:14 +01:00
parent 9f971a0c88
commit f2646f164b
3 changed files with 325 additions and 353 deletions

View File

@@ -4099,267 +4099,11 @@ public final class MainPanel extends JPanel implements TreeSelectionListener, Se
if (item instanceof DefineVideoStreamTag) {
previewPanel.clear();
DefineVideoStreamTag movie = (DefineVideoStreamTag) item;
File selfile = Helper.fixDialogFile(selectedFile);
try (FileInputStream fis = new FileInputStream(selfile.getAbsolutePath())) {
FLVInputStream flvIs = new FLVInputStream(fis);
Reference<Boolean> audioPresent = new Reference<>(false);
Reference<Boolean> videoPresent = new Reference<>(false);
flvIs.readHeader(audioPresent, videoPresent);
List<FLVTAG> flvTags = flvIs.readTags();
DefineVideoStreamTag movie = (DefineVideoStreamTag) item;
List<FLVTAG> videoTags = new ArrayList<>();
for (FLVTAG tag : flvTags) {
if (tag.tagType == FLVTAG.DATATYPE_VIDEO) {
videoTags.add(tag);
}
}
if (!videoTags.isEmpty()) {
FLVTAG firstVideoTag = videoTags.get(0);
VIDEODATA videoData = ((VIDEODATA) firstVideoTag.data);
FLVInputStream dis = new FLVInputStream(new ByteArrayInputStream(videoData.videoData));
int width = 0;
int height = 0;
switch (videoData.codecId) {
case VIDEODATA.CODEC_SORENSON_H263:
dis.readUB(17); //pictureStartCode
dis.readUB(5); //version
dis.readUB(8); //temporalReference
int pictureSize = (int) dis.readUB(3);
switch (pictureSize) {
case 0:
width = (int) dis.readUB(8);
height = (int) dis.readUB(8);
break;
case 1:
width = (int) dis.readUB(16);
height = (int) dis.readUB(16);
break;
case 2:
width = 352;
height = 288;
break;
case 3:
width = 176;
height = 144;
break;
case 4:
width = 128;
height = 96;
break;
case 5:
width = 320;
height = 240;
break;
case 6:
width = 160;
height = 120;
break;
}
break;
case VIDEODATA.CODEC_SCREEN_VIDEO:
case VIDEODATA.CODEC_SCREEN_VIDEO_V2:
dis.readUB(4); //blockWidth
width = (int) dis.readUB(12);
dis.readUB(4); //blockHeight
height = (int) dis.readUB(12);
break;
case VIDEODATA.CODEC_VP6:
case VIDEODATA.CODEC_VP6_ALPHA:
int horizontalAdjustment = (int) dis.readUB(4);
int verticalAdjustment = (int) dis.readUB(4);
if (videoData.codecId == VIDEODATA.CODEC_VP6_ALPHA) {
dis.readUI24(); //offsetToAlpha
}
int frameMode = (int) dis.readUB(1); //frameMode
dis.readUB(6); //qp
int multiStream = (int) dis.readUB(1); //marker
if (frameMode == 0) {
int vp3VersionNo = (int) dis.readUB(5);
int vpProfile = (int) dis.readUB(2);
dis.readUB(1); //interlace
if (multiStream == 1 || vpProfile == 0) {
dis.readUI16(); //offset
}
int dim_y = dis.readUI8();
int dim_x = dis.readUI8();
//dis.readUI8(); //render_y
//dis.readUI8(); //render_x
width = 16 * dim_x - horizontalAdjustment;
height = 16 * dim_y - verticalAdjustment;
}
break;
case VIDEODATA.CODEC_AVC:
throw new IOException("AVC codec is not supported for import");
case VIDEODATA.CODEC_JPEG:
throw new IOException("JPEG codec is not supported for import");
}
if (width <= 0 || height <= 0) {
throw new IOException("Invalid dimension");
}
movie.codecID = videoData.codecId;
movie.width = width;
movie.height = height;
movie.videoFlagsDeblocking = DefineVideoStreamTag.DEBLOCKING_OFF;
movie.videoFlagsSmoothing = true;
movie.numFrames = videoTags.size();
movie.setPauseRendering(true);
videoTags.sort(new Comparator<FLVTAG>() {
@Override
public int compare(FLVTAG o1, FLVTAG o2) {
return Long.compare(o1.timeStamp, o2.tagType);
}
});
//remove old VideoFrame tags
Map<Integer, VideoFrameTag> frames = new HashMap<>();
SWF swf = movie.getSwf();
SWF.populateVideoFrames(movie.characterID, swf.getTags(), frames);
Timelined timelined = null;
for (VideoFrameTag t : frames.values()) {
t.getTimelined().removeTag(t);
timelined = t.getTimelined();
}
timelined.resetTimeline();
int startFrame = 0;
boolean startFound = false;
int placeDepth = -1;
for (Tag t : timelined.getTags()) {
if (t instanceof ShowFrameTag) {
startFrame++;
}
if (t instanceof PlaceObjectTypeTag) {
PlaceObjectTypeTag pt = (PlaceObjectTypeTag) t;
if (pt.getCharacterId() == movie.characterID) {
placeDepth = pt.getDepth();
startFound = true;
break;
}
}
}
int lastFrame = -1;
if (timelined != null) {
VideoFrameTag lastVideoFrame = null;
for (FLVTAG ftag : videoTags) {
videoData = ((VIDEODATA) ftag.data);
if (videoData.codecId == VIDEODATA.CODEC_VP6 || videoData.codecId == VIDEODATA.CODEC_VP6_ALPHA) {
int horizontalAdjustment = (int) dis.readUB(4);
int verticalAdjustment = (int) dis.readUB(4);
if (videoData.codecId == VIDEODATA.CODEC_VP6_ALPHA) {
dis.readUI24(); //offsetToAlpha
}
int frameMode = (int) dis.readUB(1); //frameMode
dis.readUB(6); //qp
int multiStream = (int) dis.readUB(1); //marker
if (frameMode == 1) {
if (multiStream == 1) { // || version2 == 0) { ???
dis.readUI16(); //buff2Offset
}
dis.readUB(1); //refreshGoldenFrame
int useLoopFilter = (int) dis.readUB(1); //useLoopFilter
if (useLoopFilter == 1) {
int loopFilterSelector = (int)dis.readUB(1);
if (loopFilterSelector == 0) {
movie.videoFlagsDeblocking = DefineVideoStreamTag.DEBLOCKING_LEVEL1;
} else if(loopFilterSelector == 1) {
movie.videoFlagsDeblocking = DefineVideoStreamTag.DEBLOCKING_LEVEL4;
}
}
}
}
int idealFrame = startFrame + (int) Math.floor(swf.frameRate * ftag.timeStamp / 1000.0);
if (idealFrame == lastFrame) {
idealFrame++;
}
int swfFrameNum = -1;
ReadOnlyTagList tagList = timelined.getTags();
int p = 0;
boolean found = false;
for (; p < tagList.size(); p++) {
Tag t = tagList.get(p);
if (t instanceof ShowFrameTag) {
swfFrameNum++;
if (swfFrameNum == idealFrame) {
found = true;
break;
}
}
}
//add frames when necessary
if (!found) {
p--;
swfFrameNum++;
for (; swfFrameNum <= idealFrame; swfFrameNum++) {
PlaceObject2Tag placeObject = new PlaceObject2Tag(swf);
placeObject.setTimelined(timelined);
placeObject.depth = placeDepth;
placeObject.placeFlagMove = true;
placeObject.placeFlagHasRatio = true;
placeObject.ratio = swfFrameNum - startFrame;
timelined.addTag(placeObject);
p++;
ShowFrameTag sft = new ShowFrameTag(swf);
sft.setTimelined(timelined);
timelined.addTag(sft);
p++;
}
swfFrameNum--;
}
VideoFrameTag videoFrameTag = new VideoFrameTag(swf);
videoFrameTag.streamID = movie.characterID;
videoFrameTag.frameNum = swfFrameNum - startFrame;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
VIDEODATA vdata = (VIDEODATA) ftag.data;
switch (movie.codecID) {
case DefineVideoStreamTag.CODEC_VP6:
case DefineVideoStreamTag.CODEC_VP6_ALPHA:
baos.write(Arrays.copyOfRange(vdata.videoData, 1 /*strip adjustment*/, vdata.videoData.length));
break;
default:
baos.write(vdata.videoData);
break;
}
videoFrameTag.videoData = new ByteArrayRange(baos.toByteArray());
timelined.addTag(p, videoFrameTag);
videoFrameTag.setTimelined(timelined);
lastFrame = idealFrame;
}
}
List<Tag> tagsToRemove = new ArrayList<>();
for (Tag t : timelined.getTags()) {
if (t instanceof PlaceObjectTypeTag) {
PlaceObjectTypeTag pt = (PlaceObjectTypeTag) t;
if (pt.getDepth() == placeDepth) {
int ratio = pt.getRatio();
if (ratio > lastFrame - startFrame) {
tagsToRemove.add(t);
}
}
}
}
for (Tag t : tagsToRemove) {
timelined.removeTag(t);
}
movie.setModified(true);
timelined.resetTimeline();
refreshTree();
movie.resetTimeline();
movie.resetPlayer();
movie.setPauseRendering(false);
}
try (FileInputStream fis = new FileInputStream(selfile)) {
movie.replace(fis);
refreshTree();
} catch (IOException ex) {
ViewMessages.showMessageDialog(MainPanel.this, translate("error.movie.invalid") + ": "+ex.getMessage(), translate("error"), JOptionPane.ERROR_MESSAGE);
}
@@ -5870,57 +5614,5 @@ public final class MainPanel extends JPanel implements TreeSelectionListener, Se
previewPanel.startEditGenericTag();
}
}
public static void main(String[] args) throws Exception {
/*FLVInputStream fis = new FLVInputStream(new FileInputStream("c:\\Virtual\\shared\\movie123_source\\white\\frames\\frames_550.flv"));
fis.readHeader(new Reference<>(false), new Reference<>(false));
int pos = 0;
for(FLVTAG t:fis.readTags()){
if (t.tagType == 9) {
VIDEODATA vdata = (VIDEODATA)t.data;
FileOutputStream fos = new FileOutputStream("c:\\Virtual\\shared\\movie123_source\\white\\frames\\frames_550."+pos+".bin");
fos.write(vdata.videoData);
fos.close();
pos++;
}
//System.out.println("reading tag type "+t.tagType);
}*/
FLVInputStream dis = new FLVInputStream(new FileInputStream("c:\\Virtual\\shared\\movie123_source\\white\\frames\\frames_551.0.bin"));
int horizontalAdjustment = (int) dis.readUB(4);
int verticalAdjustment = (int) dis.readUB(4);
int frameMode = (int) dis.readUB(1); //frameMode
System.out.println("frameMode=" + frameMode);
int qp = (int) dis.readUB(6); //qp
System.out.println("qp=" + qp);
int marker = (int) dis.readUB(1); //marker
System.out.println("horizontalAdjustment=" + horizontalAdjustment);
System.out.println("verticalAdjustment=" + verticalAdjustment);
System.out.println("marker=" + marker);
if (frameMode == 0) {
int version = (int) dis.readUB(5);
System.out.println("version=" + version);
int version2 = (int) dis.readUB(2);
System.out.println("version2=" + version2);
dis.readUB(1); //interlace
if (marker == 1 || version2 == 0) {
int offset = dis.readUI16(); //offset
System.out.println("offset = " + offset);
}
int dim_y = dis.readUI8();
int dim_x = dis.readUI8();
int render_y = dis.readUI8(); //render_y
int render_x = dis.readUI8(); //render_x
System.out.println("dim_y=" + dim_y);
System.out.println("dim_x=" + dim_x);
System.out.println("render_y=" + render_y);
System.out.println("render_x=" + render_x);
System.out.println("width=" + (dim_x * 16 - horizontalAdjustment));
System.out.println("height=" + (dim_y * 16 - verticalAdjustment));
}
}
}
}