⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtmediaplayer.java

📁 编辑视频文件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                        if (age >= (24 * 60 * 60 * 1000)) {                            tempFile.delete();                        }                    }                    scratchFile = new QTFile(tempFile);                    scratchFile.createMovieFile(QTUtils.toOSType("TOVD"),                        StdQTConstants.newMovieActive |                        StdQTConstants.createMovieFileDeleteCurFile);                    scratchFile.deleteOnExit();                    File resourceFile = new File(scratchFile.getAbsolutePath() +                            ".#res");                    if (resourceFile.exists()) {                        resourceFile.deleteOnExit();                    }                    //Movie editMovie = Movie.createMovieFile(scratchFile, QTUtils.toOSType("TOVD"),                    //	StdQTConstants.newMovieActive | StdQTConstants.createMovieFileDeleteCurFile |                    //	StdQTConstants.createMovieFileDontCreateResFile);                    Movie editMovie = new Movie();                    int dataRefCount = media.getDataRefCount();                    DataRef mediaDataRef = media.getDataRef(dataRefCount);                    QDDimension origSize = origTrack.getSize();                    Track editTrack = editMovie.newTrack(origSize.getWidthF(),                            origSize.getHeightF(), origTrack.getVolume());                    int ts = movie.getTimeScale();                    //Media editMedia = new MPEGMedia(editTrack, ts, mediaDataRef);                    /*Media editMedia = */ Media.newFromType(StdQTConstants.MPEGMediaType,                        editTrack, ts, mediaDataRef);                    int duration = origTrack.getDuration();                    origTrack.insertSegment(editTrack, 0, duration, 0);                    origTrack.copySettings(editTrack);                    OpenMovieFile omf = OpenMovieFile.asWrite(scratchFile);                    editMovie.addResource(omf, 0, scratchFile.getName());                    omf.close();                    Movie nextMovie = Movie.fromFile(OpenMovieFile.asRead(                                scratchFile));                    if (nextMovie != null) {                        nextMovie.setTimeScale(ts);                        nextMovie.update();                        /*                        System.out.println("Edit Movie: duration: " + nextMovie.getDuration());                        System.out.println("Edit Movie: num tracks: " + nextMovie.getTrackCount());                        System.out.println("Edit Movie: num samples: " + nextMovie.getTrack(1).getMedia().getSampleCount());                        */                        QTUtils.reclaimMemory();                        return nextMovie;                    }                }            }        } catch (QTException qte) {            qte.printStackTrace();        }        return null;    }    /**     * Grabs the current video frame and converts it to an Image object.     *     * @return the current video frame     */    public Image getCurrentFrameImage() {        return getFrameImageForTime(getMediaTime());    }    /**     * Grabs the frame for the given time and converts it to a BufferedImage.<br>     * The size of the image is determined by the size read from the header of     * the mediafile; QT often uses another size for MPEG-1 files.     * QT on Windows uses another default pixel format than QT on the Mac; the current     * implementation seems to work on both platforms.     *     * @param time the media time for the frame     * @return the frame image or null     */    public Image getFrameImageForTime(long time) {        try {            // by default mpeg movies seem to be sized to 320 x 240 by qt            // this is not right...            int w = 352;            int h = 288;            if ((mpegImageWidth > 0) && (mpegImageHeight > 0)) {                w = mpegImageWidth;                h = mpegImageHeight;            } else {                //w = (int)(movie.getNaturalBoundsRect().getWidth() * movie.getMatrix().getSx());                //h = (int)(movie.getNaturalBoundsRect().getHeight() * movie.getMatrix().getSy());                w = movie.getNaturalBoundsRect().getWidth();                h = movie.getNaturalBoundsRect().getHeight();            }            Pict pict = movie.getTrack(1).getPict((int) time);            //Pict pict = movie.getPict((int)(time / milliSecondsPerSample));            //System.out.println("Def pixel format: " + QDGraphics.kDefaultPixelFormat);            // Windows: 1111970369 == k32BGRAPixelFormat            // MACOS: 32 == k32ARGBPixelFormat            QDGraphics offScr = new QDGraphics(QDGraphics.kDefaultPixelFormat,                    new QDRect(0, 0, w, h));            pict.draw(offScr, offScr.getBounds());            PixMap pixmap = offScr.getPixMap();            RawEncodedImage raw = RawEncodedImage.fromPixMap(pixmap);            // copy bytes to an array            int intsPerRow = pixmap.getRowBytes() / 4;            int[] pixels = new int[intsPerRow * h];            raw.copyToArray(0, pixels, 0, pixels.length);            DirectColorModel model = new DirectColorModel(32, // bits/sample                    0x00ff0000, // R                    0x0000ff00, // G                    0x000000ff, // B                    0x00000000); // ignore alpha            Image image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(                        w, h, model, pixels, 0, intsPerRow));            //QTUtils.reclaimMemory();            return image;        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    /**     * If a scratch file has been created, delete it.     */    protected void finalize() throws Throwable {        System.out.println("Finalize QT player...");        if (scratchFile.exists()) {            File resourceFile = new File(scratchFile.getAbsolutePath() +                    ".#res");            if (resourceFile.exists()) {                resourceFile.delete();            }            scratchFile.delete();        }        if (isWavPlayer) {            TaskAllMovies.removeMovie();        }        QTSession.close();        QTUtils.reclaimMemory();        super.finalize();    }    // Greg's code    // for WIN32 under JNLP load QT JNI librarys manualy. OS-X does not require this    private void initQTJNI() {        if (System.getProperty("os.name").regionMatches(false, 0, "Win", 0, 3)) {            System.loadLibrary("QTJava");            System.loadLibrary("QTJavaNative");        }    }    /**     * DOCUMENT ME!     *     * @param layoutManager DOCUMENT ME!     */    public void setLayoutManager(ElanLayoutManager layoutManager) {        if ((this.layoutManager == null) && !isWavPlayer) {            detachItem = new JMenuItem(ElanLocale.getString("Detachable.detach"));            detachItem.addActionListener(this);            popup.insert(detachItem, 0);        }        this.layoutManager = layoutManager;    }    /*     *     */    public void actionPerformed(ActionEvent e) {        if (e.getSource().equals(detachItem) && (layoutManager != null)) {            if (detached) {                layoutManager.attach(QTMediaPlayer.this.getVisualComponent());                detachItem.setText(ElanLocale.getString("Detachable.detach"));                detached = false;            } else {                layoutManager.detach(QTMediaPlayer.this.getVisualComponent());                detachItem.setText(ElanLocale.getString("Detachable.attach"));                detached = true;            }        } else if (e.getSource() == infoItem) {            new FormattedMessageDlg(this);        } else if (e.getSource() == saveItem) {            ImageExporter export = new ImageExporter();            export.exportImage(getCurrentFrameImage());        }    }    /**     * DOCUMENT ME!     */    public void updateLocale() {        if (infoItem != null) {            infoItem.setText(ElanLocale.getString("Player.Info"));        }        if (durationItem != null) {            durationItem.setText(ElanLocale.getString("Player.duration") +                ":  " + TimeFormatter.toString(getMediaDuration()));        }        if (saveItem != null) {            saveItem.setText(ElanLocale.getString("Player.SaveFrame"));        }        if (detachItem != null) {            if (detached) {                detachItem.setText(ElanLocale.getString("Detachable.attach"));            } else {                detachItem.setText(ElanLocale.getString("Detachable.detach"));            }        }    }    /**     * Release resources to be ready for the garbage collector...?     */    public void cleanUpOnClose() {        System.out.println("Clean up QT media player...");        visualComponent = null;        layoutManager = null;        mediaDescriptor = null;        try {            timeBase.disposeQTObject();            endOfMediaCallback.cancelAndCleanup();            controller.deactivate();            controller.disposeQTObject();            canvas = null;            movie = null;            controller = null;        } catch (QTException qte) {            qte.printStackTrace();        }    }    /**     * DOCUMENT ME!     * $Id: jalopy_gnu_src_dist.xml,v 1.3 2007/02/06 13:30:33 hasloe Exp $     * @author $Author: hasloe $     * @version $Revision: 1.3 $     */    private class VisualComponent extends Panel implements ComponentListener,        HierarchyListener {        /** Holds value of property DOCUMENT ME! */        boolean doNotify;        /**         * Creates a new VisualComponent instance         *         * @param component DOCUMENT ME!         */        public VisualComponent(Component component) {            doNotify = true;            add(component);            addComponentListener(this);            addHierarchyListener(this);        }        /**         * DOCUMENT ME!         *         * @param e DOCUMENT ME!         */        public void componentResized(ComponentEvent e) {            System.out.println("resized");        }        /**         * DOCUMENT ME!         *         * @param e DOCUMENT ME!         */        public void hierarchyChanged(HierarchyEvent e) {            System.out.println("hier");        }        /**         * DOCUMENT ME!         */        public void addNotify() {            System.out.println("addNotify");            if (doNotify) {                super.addNotify();                doNotify = false;            }            /*            try {                            super.addNotify();                            add(canvas.asComponent());                //            quicktime.qd.NativeGraphics.getContext(canvas).unlock();                        } catch (Exception e) {                            e.printStackTrace();                        }*/        }        /**         * DOCUMENT ME!         */        public void removeNotify() {            System.out.println("removeNotify");            super.removeNotify();            /*            try {            //                quicktime.qd.NativeGraphics.getContext(canvas).unlock();                            remove(canvas.asComponent());                            super.removeNotify();                        } catch (Exception e) {                            e.printStackTrace();                        }*/        }        /**         * DOCUMENT ME!         *         * @param e DOCUMENT ME!         */        public void componentShown(ComponentEvent e) {            System.out.println("show");        }        /**         * DOCUMENT ME!         *         * @param e DOCUMENT ME!         */        public void componentHidden(ComponentEvent e) {            System.out.println("hide");        }        /**         * DOCUMENT ME!         *         * @param e DOCUMENT ME!         */        public void componentMoved(ComponentEvent e) {            System.out.println("move");        }        /**         * DOCUMENT ME!         *         * @throws Throwable DOCUMENT ME!         */        protected void finalize() throws Throwable {            System.out.println("Finalize visual component");            super.finalize();        }    }    /**     * Private class that extends a QTCanvas and keeps the mouse events away     * from QT otherwise the media rendering will be stopped by a mouse click     * in the visual component panel     *     * @param layoutManager DOCUMENT ME!     */    private class QTCanvasNoMouse extends QTCanvas {        /**         * DOCUMENT ME!         *         * @param ml DOCUMENT ME!         */        public void addMouseListener(java.awt.event.MouseListener ml) {            if (ml instanceof MouseHandler) {                super.addMouseListener(ml);            }        }    }    /**     * DOCUMENT ME!     * $Id: QTMediaPlayer.java,v 1.19 2007/04/24 12:42:23 hasloe Exp $     * @author $Author: hasloe $     * @version $Revision: 1.19 $     */    protected class MouseHandler extends MouseAdapter {        /**         * DOCUMENT ME!         *         * @param e DOCUMENT ME!         */        public void mousePressed(MouseEvent e) {            if (SwingUtilities.isRightMouseButton(e) || e.isPopupTrigger()) {                JPopupMenu.setDefaultLightWeightPopupEnabled(false);                popup.show(getVisualComponent(), e.getPoint().x, e.getPoint().y);            }        }        /**         * DOCUMENT ME!         *         * @param e DOCUMENT ME!         */        public void mouseClicked(MouseEvent e) {            if (e.getClickCount() >= 2) {                if (layoutManager != null) {                    layoutManager.setFirstPlayer(QTMediaPlayer.this);                }                return;            }            if (SwingUtilities.isRightMouseButton(e) || e.isPopupTrigger()) {                JPopupMenu.setDefaultLightWeightPopupEnabled(false);                popup.show(getVisualComponent(), e.getPoint().x, e.getPoint().y);            }        }    }    /**     * This class implements a method that is called when the movie stops     */    private class TimeBaseExtremesCallBack        extends quicktime.std.clocks.ExtremesCallBack {        /**         * Super class constructor         *         * @param tb DOCUMENT ME!         * @param flag DOCUMENT ME!         *         * @throws QTException DOCUMENT ME!         */        public TimeBaseExtremesCallBack(TimeBase tb, int flag)            throws QTException {            super(tb, flag);        }        /**         * Make sure all the connected controllers are stopped at end of media         */        public void execute() {            //System.out.println("extremes callback");            try {                movie.stop();                stopControllers();                setMediaTime(exactStopTime); // de troep stopt vaak enkele milli seconden te vroeg.                if (stopMode == STOP_WITH_STOP_TIME) {                    setStopTime(movie.getDuration());                } else {                    movie.setPreviewMode(false);                }                exactStopTime = movie.getDuration();                endOfMediaCallback.callMeWhen();            } catch (Exception e) {                e.printStackTrace();            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -