从JAR文件运行程序时,视频不起作用

时间:2012-09-30 05:19:02

标签: java video netbeans jar

我使用Xuggler,我已经使用绝对路径作为视频文件,当我在Netbeans中运行时,它运行得很好,但是当我从jar文件启动它时,视频没有出来..这里代码

File file = new File("C:\\qssinfo\\video\\Wildlife.avi");
decodeAndPlayVideo.PlayVideo(file);

这是DecodeAndPlayVideo类的代码,(我是从xuggler得到的,我编辑了一个小)

public class DecodeAndPlayVideo {
private ImageComponent mScreen;
private IStreamCoder videoCoder;
private IContainer container;

@SuppressWarnings("deprecation")
public DecodeAndPlayVideo(ImageComponent mScreen) {
    this.mScreen = mScreen;
}

@SuppressWarnings("deprecation")
public void PlayVideo(String filename) {
    if (!IVideoResampler.isSupported(
        IVideoResampler.Feature.FEATURE_COLORSPACECONVERSION)) {
        throw new RuntimeException("you must install the GPL version"
            + " of Xuggler (with IVideoResampler support) for "
            + "this demo to work");
    }

    container = IContainer.make();

    if (container.open(filename, IContainer.Type.READ, null) < 0) {
        throw new IllegalArgumentException("could not open file: " + filename);
    }

    int numStreams = container.getNumStreams();
    int videoStreamId = -1;

    for (int i = 0; i < numStreams; i++) {
        IStream stream = container.getStream(i);
        IStreamCoder coder = stream.getStreamCoder();

        if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
            videoStreamId = i;
            videoCoder = coder;
            break;
        }
    }

    if (videoStreamId == -1) {
        throw new RuntimeException("could not find video stream in container: "
            + filename);
    }

    if (videoCoder.open() < 0) {
        throw new RuntimeException("could not open video decoder for container: "
            + filename);
    }

    IVideoResampler resampler = null;
    if (videoCoder.getPixelType() != IPixelFormat.Type.BGR24) {
        resampler = IVideoResampler.make(videoCoder.getWidth(),
                                        videoCoder.getHeight(), IPixelFormat.Type.BGR24,
                                        videoCoder.getWidth(), videoCoder.getHeight(), videoCoder.getPixelType());
        if (resampler == null) {
            throw new RuntimeException("could not create color space "
                + "resampler for: " + filename);
        }
    }

    IPacket packet = IPacket.make();
    long firstTimestampInStream = Global.NO_PTS;
    long systemClockStartTime = 0;
    while (container.readNextPacket(packet) >= 0) {
        if (packet.getStreamIndex() == videoStreamId) {
            IVideoPicture picture = IVideoPicture.make(videoCoder.getPixelType(),
                                                    videoCoder.getWidth(), videoCoder.getHeight());

            int offset = 0;
            while (offset < packet.getSize()) {
                int bytesDecoded = videoCoder.decodeVideo(picture, packet, offset);
                if (bytesDecoded < 0) {
                    throw new RuntimeException("got error decoding video in: "
                        + filename);
                }
                offset += bytesDecoded;

                if (picture.isComplete()) {
                    IVideoPicture newPic = picture;
                    if (resampler != null) {
                        newPic = IVideoPicture.make(resampler.getOutputPixelFormat(),
                                                    picture.getWidth(), picture.getHeight());
                        if (resampler.resample(newPic, picture) < 0) {
                            throw new RuntimeException("could not resample video from: "
                                + filename);
                        }
                    }
                    if (newPic.getPixelType() != IPixelFormat.Type.BGR24) {
                        throw new RuntimeException("could not decode video"
                            + " as BGR 24 bit data in: " + filename);
                    }

                    if (firstTimestampInStream == Global.NO_PTS) {
                        firstTimestampInStream = picture.getTimeStamp();
                        systemClockStartTime = System.currentTimeMillis();
                    } else {
                        long systemClockCurrentTime = System.currentTimeMillis();
                        long millisecondsClockTimeSinceStartofVideo =
                            systemClockCurrentTime - systemClockStartTime;
                        long millisecondsStreamTimeSinceStartOfVideo =
                            (picture.getTimeStamp() - firstTimestampInStream) / 1000;
                        final long millisecondsTolerance = 50; // and we give ourselfs 50 ms of tolerance
                        final long millisecondsToSleep =
                                (millisecondsStreamTimeSinceStartOfVideo
                                    - (millisecondsClockTimeSinceStartofVideo
                                    + millisecondsTolerance));
                        if (millisecondsToSleep > 0) {
                            try {
                                Thread.sleep(millisecondsToSleep);
                            } catch (InterruptedException e) {
                                return;
                            }
                        }
                    }

                    BufferedImage javaImage = Utils.videoPictureToImage(newPic);
                    updateJavaWindow(javaImage);
                }
            }
        } else {
            do {
            } while (false);
        }

    }
    close();
}

private void updateJavaWindow(BufferedImage javaImage) {
    mScreen.setImage(javaImage);
}

public void close() {
    if (videoCoder != null) {
        videoCoder.close();
        videoCoder = null;
    }
    if (container != null) {
        container.close();
        container = null;
    }
}

请你能帮助我,我有一个月的堆叠X(

1 个答案:

答案 0 :(得分:0)

你说:

  

我已经阅读了一些有关编译JAR的内容,例如getResourceAsStream etc2,但该文件位于BIN文件夹中,我想使用绝对文件,如上例所示。

如果您的文件位于BIN文件夹中,则它可能位于jar文件中,并且无法作为文件进行访问。而是通过将其作为相对于类文件位置的资源来访问它应该做什么,并且您的代码将有更好的工作机会。

顺便说一句,我很好奇文件在BIN文件夹中的位置,当上面代码中的路径为"C:\\qssinfo\\video\\Wildlife.avi"

修改
另一个问题:你的jar文件的清单是否正确设置,以便它所依赖的视频库jar文件在其类路径上?

例如,您的jar的清单文件是否包含以下行:

Class-Path: xuggle-xuggler.jar

或类似的东西,这个jar文件是你的jar文件的同一个目录(或者实际上是在用户目录中)?