private void createCodec(VideoTrack vt) { Format fmt = vt.videoFormat; String enc = fmt.get(EncodingKey); if (enc.equals(ENCODING_AVI_MJPG) // || enc.equals(ENCODING_QUICKTIME_JPEG) // ) { vt.codec = new JPEGCodec(); } else if (enc.equals(ENCODING_AVI_PNG) // || enc.equals(ENCODING_QUICKTIME_PNG) // ) { vt.codec = new PNGCodec(); } vt.codec.setInputFormat( fmt.prepend( MediaTypeKey, MediaType.VIDEO, MimeTypeKey, MIME_JAVA, EncodingKey, ENCODING_BUFFERED_IMAGE, DataClassKey, BufferedImage.class)); vt.codec.setOutputFormat( fmt.prepend(MediaTypeKey, MediaType.VIDEO, EncodingKey, enc, DataClassKey, byte[].class)); // vt.codec.setQuality(vt.videoQuality); }
/** Creates a buffered image of the specified depth with a random color palette. */ private static BufferedImage createImage(Format format) { int depth = format.get(DepthKey); int width = format.get(WidthKey); int height = format.get(HeightKey); Random rnd = new Random(0); // use seed 0 to get reproducable output BufferedImage img; switch (depth) { case 24: default: { img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); break; } case 8: { byte[] red = new byte[256]; byte[] green = new byte[256]; byte[] blue = new byte[256]; for (int i = 0; i < 255; i++) { red[i] = (byte) rnd.nextInt(256); green[i] = (byte) rnd.nextInt(256); blue[i] = (byte) rnd.nextInt(256); } rnd.setSeed(0); // set back to 0 for reproducable output IndexColorModel palette = new IndexColorModel(8, 256, red, green, blue); img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, palette); break; } case 4: { byte[] red = new byte[16]; byte[] green = new byte[16]; byte[] blue = new byte[16]; for (int i = 0; i < 15; i++) { red[i] = (byte) rnd.nextInt(16); green[i] = (byte) rnd.nextInt(16); blue[i] = (byte) rnd.nextInt(16); } rnd.setSeed(0); // set back to 0 for reproducable output IndexColorModel palette = new IndexColorModel(4, 16, red, green, blue); img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, palette); break; } } return img; }
private static void testWriting(File file, Format format) throws IOException { System.out.println("Writing " + file); // Make the format more specific format = format.prepend( MediaTypeKey, MediaType.VIDEO, // FrameRateKey, new Rational(30, 1), // WidthKey, 320, // HeightKey, 160); // Create a buffered image for this format BufferedImage img = createImage(format); Graphics2D g = img.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); QuickTimeWriter out = null; try { // Create the writer out = new QuickTimeWriter(file); // Add a track to the writer out.addTrack(format); out.setVideoColorTable(0, img.getColorModel()); // initialize the animation Random rnd = new Random(0); // use seed 0 to get reproducable output g.setBackground(Color.WHITE); g.clearRect(0, 0, img.getWidth(), img.getHeight()); for (int i = 0; i < 100; i++) { // Create an animation frame g.setColor(new Color(rnd.nextInt())); g.fillOval(rnd.nextInt(img.getWidth() - 30), rnd.nextInt(img.getHeight() - 30), 30, 30); // write it to the writer out.write(0, img, 1); } } finally { // Close the writer if (out != null) { out.close(); } // Dispose the graphics object g.dispose(); } }
public int addTrack(Format format) throws IOException { if (tracks.size() > 0) throw new UnsupportedOperationException("only 1 track supported"); Format derivedFormat = format.prepend( MediaTypeKey, MediaType.VIDEO, MimeTypeKey, MIME_ANIM, EncodingKey, ENCODING_ANIM_OP5, DataClassKey, byte[].class, // FixedFrameRateKey, false); setCAMG(toCAMG(derivedFormat)); Track tr = new Track(); tr.format = derivedFormat; tracks.add(tr); return tracks.size() - 1; }
public int process(Buffer in, Buffer out) { out.setMetaTo(in); if (in.isFlag(DISCARD)) { return CODEC_OK; } out.format = outputFormat; SeekableByteArrayOutputStream tmp; if (out.data instanceof byte[]) { tmp = new SeekableByteArrayOutputStream((byte[]) out.data); } else { tmp = new SeekableByteArrayOutputStream(); } Format vf = outputFormat; // Handle sub-image Rectangle r; int scanlineStride; if (in.data instanceof BufferedImage) { BufferedImage image = (BufferedImage) in.data; WritableRaster raster = image.getRaster(); scanlineStride = raster.getSampleModel().getWidth(); r = raster.getBounds(); r.x -= raster.getSampleModelTranslateX(); r.y -= raster.getSampleModelTranslateY(); } else { r = new Rectangle(0, 0, vf.get(WidthKey), vf.get(HeightKey)); scanlineStride = vf.get(WidthKey); } try { switch (vf.get(DepthKey)) { case 8: { writeKey8( tmp, getIndexed8(in), r.width, r.height, r.x + r.y * scanlineStride, scanlineStride); break; } case 16: { writeKey16( tmp, getRGB15(in), r.width, r.height, r.x + r.y * scanlineStride, scanlineStride); break; } case 24: { writeKey24( tmp, getRGB24(in), r.width, r.height, r.x + r.y * scanlineStride, scanlineStride); break; } case 32: { writeKey24( tmp, getARGB32(in), r.width, r.height, r.x + r.y * scanlineStride, scanlineStride); break; } default: { out.setFlag(DISCARD); return CODEC_FAILED; } } out.format = outputFormat; out.sampleCount = 1; out.setFlag(KEYFRAME); out.data = tmp.getBuffer(); out.offset = 0; out.length = (int) tmp.getStreamPosition(); return CODEC_OK; } catch (IOException ex) { ex.printStackTrace(); out.setFlag(DISCARD); return CODEC_FAILED; } }