Java video output can only be opened using Quicktime but no other media player can open it -
i have program , captures screen , takes images , turns them movie. (using jpegimagestomovies.java customized work .png) have tried multiple extensions: .mov
, .mp4
, .avi
(i open trying others). however, regardless of extension use, when try open file windows media player following error:
windows media player encountered problem while playing file. error code c00d11b1
i've tried opening file using vlc produces following error:
no suitable decoder module: vlc not support audio or video format "twos". unfortunately there no way fix this.
opening file using quicktime work.
so question is, how can produce video file can opened if not media players.
here jpegimagestomovie.java
package maple; /* * @(#)jpegimagestomovie.java 1.3 01/03/13 * * copyright (c) 1999-2001 sun microsystems, inc. rights reserved. * * sun grants ("licensee") non-exclusive, royalty free, license use, * modify , redistribute software in source , binary code form, * provided i) copyright notice , license appear on copies of * software; , ii) licensee not utilize software in manner * disparaging sun. * * software provided "as is," without warranty of kind. * express or implied conditions, representations , warranties, including * implied warranty of merchantability, fitness particular purpose or * non-infringement, hereby excluded. sun , licensors shall not * liable damages suffered licensee result of using, modifying * or distributing software or derivatives. in no event sun or * licensors liable lost revenue, profit or data, or direct, * indirect, special, consequential, incidental or punitive damages, * caused , regardless of theory of liability, arising out of use of * or inability use software, if sun has been advised of * possibility of such damages. * * software not designed or intended use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * design, construction, operation or maintenance of nuclear * facility. licensee represents , warrants not use or * redistribute software such purposes. */ import java.io.*; import java.util.*; import java.awt.dimension; import java.awt.image.bufferedimage; import javax.imageio.imageio; import javax.media.*; import javax.media.control.*; import javax.media.protocol.*; import javax.media.datasink.*; import javax.media.format.rgbformat; import javax.media.format.videoformat; /** * program takes list of jpeg image files , convert them * quicktime movie. */ public class jpegimagestomovie implements controllerlistener, datasinklistener { static private vector<string> getimagefilespathsvector( string imagesfolderpath) { file imagesfolder = new file(imagesfolderpath); string[] imagefilesarray = imagesfolder.list(); vector<string> imagefilespathsvector = new vector<string>(); (string imagefilename : imagefilesarray) { if (!imagefilename.tolowercase().endswith("png")) continue; imagefilespathsvector.add(imagesfolder.getabsolutepath() + file.separator + imagefilename); } return imagefilespathsvector; } public boolean doit(int width, int height, int framerate, vector<string> infiles, medialocator outml) { imagedatasource ids = new imagedatasource(width, height, framerate, infiles); processor p; try { system.err .println("- create processor image datasource ..."); p = manager.createprocessor(ids); } catch (exception e) { system.err .println("yikes! cannot create processor data source."); return false; } p.addcontrollerlistener(this); // put processor configured state can set // processing options on processor. p.configure(); if (!waitforstate(p, processor.configured)) { system.err.println("failed configure processor."); return false; } // set output content descriptor quicktime. p.setcontentdescriptor(new contentdescriptor( filetypedescriptor.quicktime));// filetypedescriptor.msvideo // query processor supported formats. // set on processor. trackcontrol tcs[] = p.gettrackcontrols(); format f[] = tcs[0].getsupportedformats(); if (f == null || f.length <= 0) { system.err.println("the mux not support input format: " + tcs[0].getformat()); return false; } tcs[0].setformat(f[0]); system.err.println("setting track format to: " + f[0]); // done programming processor. let's // realize it. p.realize(); if (!waitforstate(p, controller.realized)) { system.err.println("failed realize processor."); return false; } // now, we'll need create datasink. datasink dsink; if ((dsink = createdatasink(p, outml)) == null) { system.err .println("failed create datasink given output medialocator: " + outml); return false; } dsink.adddatasinklistener(this); filedone = false; system.err.println("start processing..."); // ok, can start actual transcoding. try { p.start(); dsink.start(); } catch (ioexception e) { system.err.println("io error during processing"); return false; } // wait endofstream event. waitforfiledone(); // cleanup. try { dsink.close(); } catch (exception e) { } p.removecontrollerlistener(this); system.err.println("...done processing."); return true; } /** * create datasink. */ datasink createdatasink(processor p, medialocator outml) { datasource ds; if ((ds = p.getdataoutput()) == null) { system.err .println("something wrong: processor not have output datasource"); return null; } datasink dsink; try { system.err.println("- create datasink for: " + outml); dsink = manager.createdatasink(ds, outml); dsink.open(); } catch (exception e) { system.err.println("cannot create datasink: " + e); return null; } return dsink; } object waitsync = new object(); boolean statetransitionok = true; /** * block until processor has transitioned given state. return * false if transition failed. */ boolean waitforstate(processor p, int state) { synchronized (waitsync) { try { while (p.getstate() < state && statetransitionok) waitsync.wait(); } catch (exception e) { } } return statetransitionok; } /** * controller listener. */ public void controllerupdate(controllerevent evt) { if (evt instanceof configurecompleteevent || evt instanceof realizecompleteevent || evt instanceof prefetchcompleteevent) { synchronized (waitsync) { statetransitionok = true; waitsync.notifyall(); } } else if (evt instanceof resourceunavailableevent) { synchronized (waitsync) { statetransitionok = false; waitsync.notifyall(); } } else if (evt instanceof endofmediaevent) { evt.getsourcecontroller().stop(); evt.getsourcecontroller().close(); } } object waitfilesync = new object(); boolean filedone = false; boolean filesuccess = true; /** * block until file writing done. */ boolean waitforfiledone() { synchronized (waitfilesync) { try { while (!filedone) waitfilesync.wait(); } catch (exception e) { } } return filesuccess; } /** * event handler file writer. */ public void datasinkupdate(datasinkevent evt) { if (evt instanceof endofstreamevent) { synchronized (waitfilesync) { filedone = true; waitfilesync.notifyall(); } } else if (evt instanceof datasinkerrorevent) { synchronized (waitfilesync) { filedone = true; filesuccess = false; waitfilesync.notifyall(); } } } public static void main(string args[]) { // changed method bit if (args.length == 0) prusage(); // parse arguments. int = 0; int width = -1, height = -1, framerate = -1; vector<string> inputfiles = new vector<string>(); string rootdir = null; string outputurl = null; while (i < args.length) { if (args[i].equals("-w")) { i++; if (i >= args.length) prusage(); width = new integer(args[i]).intvalue(); } else if (args[i].equals("-h")) { i++; if (i >= args.length) prusage(); height = new integer(args[i]).intvalue(); } else if (args[i].equals("-f")) { i++; if (i >= args.length) prusage(); // new integer(args[i]).intvalue(); framerate = integer.parseint(args[i]); } else if (args[i].equals("-o")) { i++; if (i >= args.length) prusage(); outputurl = args[i]; } else if (args[i].equals("-i")) { i++; if (i >= args.length) prusage(); rootdir = args[i]; } else { system.out.println("."); prusage(); } i++; } if (rootdir == null) { system.out .println("since no input (-i) forder provided, assuming jar inside jpegs folder."); rootdir = (new file(".")).getabsolutepath(); } inputfiles = getimagefilespathsvector(rootdir); if (inputfiles.size() == 0) prusage(); if (outputurl == null) { outputurl = (new file(rootdir)).getabsolutepath() + file.separator + "pngs2movie.mov"; } if (!outputurl.tolowercase().startswith("file:///")) { outputurl = "file:///" + outputurl; } // check output file extension. if (!outputurl.tolowercase().endswith(".mov")) { prusage(); outputurl += ".mov"; system.out .println("outputurl should ending mov. making happen.\nnow outputurl is: " + outputurl); } if (width < 0 || height < 0) { prusage(); system.out.println("trying guess movie size first image"); bufferedimage firstimageinfolder = getfirstimageinfolder(rootdir); width = firstimageinfolder.getwidth(); height = firstimageinfolder.getheight(); system.out.println("width = " + width); system.out.println("height = " + height); } // check frame rate. if (framerate < 1) framerate = 30; // generate output media locators. medialocator oml; if ((oml = createmedialocator(outputurl)) == null) { system.err.println("cannot build media locator from: " + outputurl); system.exit(0); } jpegimagestomovie imagetomovie = new jpegimagestomovie(); imagetomovie.doit(width, height, framerate, inputfiles, oml); system.exit(0); } private static bufferedimage getfirstimageinfolder(string rootdir) { file rootfile = new file(rootdir); string[] list = (rootfile).list(); bufferedimage bufferedimage = null; (string filepath : list) { if (!filepath.tolowercase().endswith(".png") && !filepath.tolowercase().endswith(".png")) { continue; } try { bufferedimage = imageio.read(new file(rootfile .getabsolutefile() + file.separator + filepath)); break; } catch (ioexception e) { e.printstacktrace(); } } return bufferedimage; } static void prusage() { system.err .println("usage: java jpegimagestomovie [-w <width>] [-h <height>] [-f <frame rate>] [-o <output url>] -i <input jpeg files dir path>"); // system.exit(-1); } /** * create media locator given string. */ @suppresswarnings("unused") public static medialocator createmedialocator(string url) { medialocator ml; if (url.indexof(":") > 0 && (ml = new medialocator(url)) != null) return ml; if (url.startswith(file.separator)) { if ((ml = new medialocator("file:" + url)) != null) return ml; } else { string file = "file:" + system.getproperty("user.dir") + file.separator + url; if ((ml = new medialocator(file)) != null) return ml; } return null; } // ///////////////////////////////////////////// // // inner classes. // ///////////////////////////////////////////// /** * datasource read list of jpeg image files , turn * stream of jmf buffers. datasource not seekable or positionable. */ class imagedatasource extends pullbufferdatasource { imagesourcestream streams[]; imagedatasource(int width, int height, int framerate, vector<string> images) { streams = new imagesourcestream[1]; streams[0] = new pngimagesourcestream(width, height, framerate, images); } public void setlocator(medialocator source) { } public medialocator getlocator() { return null; } /** * content type of raw since sending buffers of video frames * without container format. */ public string getcontenttype() { return contentdescriptor.raw; } public void connect() { } public void disconnect() { } public void start() { } public void stop() { } /** * return imagesourcestreams. */ public pullbufferstream[] getstreams() { return streams; } /** * have derived duration number of frames , * frame rate. purpose of program, it's not necessary. */ public time getduration() { return duration_unknown; } public object[] getcontrols() { return new object[0]; } public object getcontrol(string type) { return null; } } /** * source stream go along imagedatasource. */ class imagesourcestream implements pullbufferstream { vector<string> images; int width, height; videoformat format; int nextimage = 0; // index of next image read. boolean ended = false; public imagesourcestream(int width, int height, int framerate, vector<string> images) { this.width = width; this.height = height; this.images = images; format = new videoformat(videoformat.jpeg, new dimension(width, height), format.not_specified, format.bytearray, (float) framerate); } /** * should never need block assuming data read files. */ public boolean willreadblock() { return false; } /** * called processor read frame worth of video * data. */ public void read(buffer buf) throws ioexception { // check if we've finished frames. if (nextimage >= images.size()) { // done. set endofmedia. system.err.println("done reading images."); buf.seteom(true); buf.setoffset(0); buf.setlength(0); ended = true; return; } string imagefile = (string) images.elementat(nextimage); nextimage++; system.err.println(" - reading image file: " + imagefile); // open random access file next image. randomaccessfile rafile; rafile = new randomaccessfile(imagefile, "r"); byte data[] = null; // check input buffer type & size. if (buf.getdata() instanceof byte[]) data = (byte[]) buf.getdata(); // check see given buffer big enough frame. if (data == null || data.length < rafile.length()) { data = new byte[(int) rafile.length()]; buf.setdata(data); } // read entire jpeg image file. rafile.readfully(data, 0, (int) rafile.length()); system.err.println(" read " + rafile.length() + " bytes."); buf.setoffset(0); buf.setlength((int) rafile.length()); buf.setformat(format); buf.setflags(buf.getflags() | buffer.flag_key_frame); // close random access file. rafile.close(); } /** * return format of each video frame. jpeg. */ public format getformat() { return format; } public contentdescriptor getcontentdescriptor() { return new contentdescriptor(contentdescriptor.raw); } public long getcontentlength() { return 0; } public boolean endofstream() { return ended; } public object[] getcontrols() { return new object[0]; } public object getcontrol(string type) { return null; } } class pngimagesourcestream extends imagesourcestream { public pngimagesourcestream(int width, int height, int framerate, vector<string> images) { super(width, height, framerate, images); // configure new format rgb format format = new rgbformat(new dimension(width, height), format.not_specified, format.bytearray, framerate, 24, // 24 bits per pixel 1, 2, 3); // red, green , blue masks when data in form of byte[] } public void read(buffer buf) throws ioexception { // check if we've finished frames. if (nextimage >= images.size()) { // done. set endofmedia. system.err.println("done reading images."); buf.seteom(true); buf.setoffset(0); buf.setlength(0); ended = true; return; } string imagefile = (string) images.elementat(nextimage); nextimage++; system.err.println(" - reading image file: " + imagefile); // read png image bufferedimage image = imageio.read(new file(imagefile)); boolean hasalpha = image.getcolormodel().hasalpha(); dimension size = format.getsize(); // convert 32-bit rgba 24-bit rgb byte[] imagedata = convertto24bit(hasalpha, image.getraster().getpixels(0, 0, size.width, size.height, (int[]) null)); buf.setdata(imagedata); system.err.println(" read " + imagedata.length + " bytes."); buf.setoffset(0); buf.setlength(imagedata.length); buf.setformat(format); buf.setflags(buf.getflags() | buffer.flag_key_frame); } private void convertintbytetobyte(int[] src, int srcindex, byte[] out, int outindex) { // note: int[] returned bufferedimage.getraster().getpixels() // int[] // each int value 1 color i.e. first 4 ints // contain rgba values first pixel int r = src[srcindex]; int g = src[srcindex + 1]; int b = src[srcindex + 2]; out[outindex] = (byte) (r & 0xff); out[outindex + 1] = (byte) (g & 0xff); out[outindex + 2] = (byte) (b & 0xff); } private byte[] convertto24bit(boolean hasalpha, int[] input) { int datalength = input.length; int newsize = (hasalpha ? datalength * 3 / 4 : datalength); byte[] converteddata = new byte[newsize]; // every 4 int values of original array (rgba) write 3 // bytes (rgb) output array // if there no alpha (i.e. rgb image) convert int byte (int = 0, j = 0; < datalength; += 3, j += 3) { convertintbytetobyte(input, i, converteddata, j); if (hasalpha) { i++; // skip byte if original image has // int transparency } } return converteddata; } } }
and make video doing following
public void makevideo (string movfile) throws malformedurlexception { jpegimagestomovie imagetomovie = new jpegimagestomovie(); vector<string> imglist = new vector <string>(); file f = new file(javcapture.tmplocation + "\\tmp\\"); file[] filelist = f.listfiles(); (int = 0; < filelist.length; i++) { imglist.add(filelist[i].getabsolutepath()); } medialocator ml; if ((ml = imagetomovie.createmedialocator(movfile)) == null) { system.exit(0); } setwidth(); setheight(); imagetomovie.doit(width, height, (1000/125), imglist, ml); }
Comments
Post a Comment