Skip to main content

how to merge audio and video? and create video (AVI, MPEG, etc)

No replies
shinlei
Offline
Joined: 2008-10-24
Points: 0

hi,

well, what i m in the right Thread? cause i newbie and i needed help

actualy i m trying to making video using JMF library, and i can create video .MOV and audio .WAV, and then i m trying to merge them, but after that i just can hear the audio and the video do not show out.

i quess the question: Has anyone know to merge them?

this is my code for merge them
______________________________________________________________________

private void doMerge() {
int i = 0;
processors = new Processor[sourcesURLs.size()];
dataOutputs = new DataSource[sourcesURLs.size()];

for (i = 0; i < sourcesURLs.size(); i++) {
String source = (String) sourcesURLs.elementAt(i);
MediaLocator ml = new MediaLocator(source);
ProcessorModel pm = new MyPM(ml);
try {
processors[i] = Manager.createRealizedProcessor(pm);
dataOutputs[i] = processors[i].getDataOutput();
processors[i].start();
} catch (Exception e) {
System.err.println("Failed to create a processor: " + e);
System.exit(-1);
}
}

// Merge the data sources from the individual processors
try {
merger = Manager.createMergingDataSource(dataOutputs);
merger.connect();
merger.start();
} catch (Exception ex) {
System.err.println("Failed to merge data sources: " + ex);
System.exit(-1);
}
if (merger == null) {
System.err.println("Failed to merge data sources");
System.exit(-1);
}
/*
try {
Player p = Manager.createPlayer(merger);
new com.sun.media.ui.PlayerWindow(p);
} catch (Exception e) {
System.err.println("Failed to create player " + e);
}
*/

// Create the output processor
ProcessorModel outputPM = new MyPMOut(merger);

try {
outputProcessor = Manager.createRealizedProcessor(outputPM);
outputDataSource = outputProcessor.getDataOutput();
} catch (Exception exc) {
System.err.println("Failed to create output processor: " + exc);
System.exit(-1);
}

try {
outputLocator = new MediaLocator(outputFile);
outputDataSink = Manager.createDataSink(outputDataSource,
outputLocator);
outputDataSink.open();
} catch (Exception exce) {
System.err.println("Failed to create output DataSink: " + exce);
System.exit(-1);
}

outputProcessor.addControllerListener(this);
outputDataSink.addDataSinkListener(this);
System.err.println("Merging...");
try {
outputDataSink.start();
outputProcessor.start();
} catch (Exception excep) {
System.err.println("Failed to start file writing: " + excep);
System.exit(-1);
}
int count = 0;

while (!done) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException ie) {
}

// Just check if the merging has been cancelled.
// The first try is for a NullPointerException,
// in case the progress bar has not been
// initiated.
try {
if (myProgressBar.cancelled) {
try {
outputDataSink.stop();
outputProcessor.stop();
done = true;
System.out.println("Stopped merge datasink");
} catch (Exception e) {
System.out.println("Couldn't stop merge datasink");
System.out.println(e);
}
}
} catch (Exception e) {}

if (outputProcessor != null &&
(int)(outputProcessor.getMediaTime().getSeconds()) > count) {
System.err.print(".");
count = (int)(outputProcessor.getMediaTime().getSeconds());
}

}

if (outputDataSink != null) {
outputDataSink.close();
}
synchronized (this) {
if (outputProcessor != null) {
outputProcessor.close();
}
}
System.err.println("Done!");
}
______________________________________________________________________

Another problems i want to create video (AVI, MPEG, etc) and i stuck XD

i just can creating video .MOV

The code for creating video
______________________________________________________________________

public class JpegImagesToMovieMod extends Thread implements ControllerListener, DataSinkListener {

// Vector inputFiles;
MediaLocator oml;
int width = -1, height = -1, frameRate = 1;
private DataList JPGImages = null;
public int picCnt = 0;
public boolean finished = false;

public boolean doIt(int width, int height, int frameRate, MediaLocator outML) {
ImageDataSource ids = new ImageDataSource(width, height, frameRate);
Processor p;

try {
System.err.println("- create processor for the image datasource ...");
p = Manager.createProcessor(ids);
} catch (Exception e) {
System.err.println("Yikes! Cannot create a processor from the data source.");
return false;
}

p.addControllerListener(this);

// Put the Processor into configured state so we can set
// some processing options on the processor.
p.configure();
if (!waitForState(p, p.Configured)) {
System.err.println("Failed to configure the processor.");
return false;
}

// Set the output content descriptor to QuickTime.
p.setContentDescriptor(new ContentDescriptor(FileTypeDescriptor.QUICKTIME));

// Query for the processor for supported formats.
// Then set it on the processor.
TrackControl tcs[] = p.getTrackControls();
Format f[] = tcs[0].getSupportedFormats();
if (f == null || f.length <= 0) {
System.err.println("The mux does not support the input format: " + tcs[0].getFormat());
return false;
}

tcs[0].setFormat(f[0]);

System.err.println("Setting the track format to: " + f[0]);

// We are done with programming the processor. Let's just
// realize it.
p.realize();
if (!waitForState(p, p.Realized)) {
System.err.println("Failed to realize the processor.");
return false;
}

// Now, we'll need to create a DataSink.
DataSink dsink;
if ((dsink = createDataSink(p, outML)) == null) {
System.err.println("Failed to create a DataSink for the given output MediaLocator: " + outML);
return false;
}

dsink.addDataSinkListener(this);
fileDone = false;

System.err.println("start processing...");

// OK, we can now start the actual transcoding.
try {
p.start();
dsink.start();
} catch (IOException e) {
System.err.println("IO error during processing");
return false;
}

// Wait for EndOfStream event.
waitForFileDone();

// Cleanup.
try {
dsink.close();
} catch (Exception e) {}
p.removeControllerListener(this);

System.err.println("...done processing.");
wakeUp(); // wake up waiting threads.

return true;
}

/**
* Create the DataSink.
*/
DataSink createDataSink(Processor p, MediaLocator outML) {

DataSource ds;

if ((ds = p.getDataOutput()) == null) {
System.err.println("Something is really wrong: the processor does not have an 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 the DataSink: " + e);
return null;
}

return dsink;
}

Object waitSync = new Object();
boolean stateTransitionOK = true;

/**
* Block until the processor has transitioned to the given state.
* Return false if the 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 is done.
*/
boolean waitForFileDone() {
synchronized (waitFileSync) {
try {
while (!fileDone)
waitFileSync.wait();
} catch (Exception e) {}
}
return fileSuccess;
}

/**
* Event handler for the 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();
}
}
}

JpegImagesToMovieMod(String args[]) {

if (args.length == 0)
prUsage();

// Parse the arguments.
int i = 0;
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();
frameRate = new Integer(args[i]).intValue();
} else if (args[i].equals("-o")) {
i++;
if (i >= args.length)
prUsage();
outputURL = args[i];
}
i++;
}

if (outputURL == null)
prUsage();
if (width < 0 || height < 0) {
System.err.println("Please specify the correct image size.");
prUsage();
}

// Check the frame rate.
if (frameRate < 1)
frameRate = 1;

if ((oml = createMediaLocator(outputURL)) == null) {
System.err.println("Cannot build media locator from: " + outputURL);
System.exit(0);
}
}

public void setDataList(DataList JPGIm) {
JPGImages = JPGIm;
}

// called by other classes to wait for processor to finish writing mov file.
public synchronized void waitFor() {
try {
while (!finished) {
wait();
}
} catch (InterruptedException ie) {
System.err.println("Exception while waiting for movieprocessor " + ie);
}
}

// wakes up waiting threads.
public synchronized void wakeUp() {
finished = true;
notifyAll();
}

public void run() {
doIt(width, height, frameRate, oml);
}

static void prUsage() {
System.err.println("Usage: java JpegImagesToMovie -w -h -f -o ...");
System.exit(-1);
}

/**
* Create a media locator from the given string.
*/
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.
///////////////////////////////////////////////

/**
* A DataSource to read from a list of JPEG image files and
* turn that into a stream of JMF buffers.
* The DataSource is not seekable or positionable.
*/
class ImageDataSource extends PullBufferDataSource {

PullBufferStream streams[];

ImageDataSource(int width, int height, int frameRate) {
streams = new PullBufferStream[1];
streams[0] = new ImageSourceStream(width, height, frameRate);
}

public void setLocator(MediaLocator source) {
}

public MediaLocator getLocator() {
return null;
}

/**
* Content type is of RAW since we are sending buffers of video
* frames without a container format.
*/
public String getContentType() {
return ContentDescriptor.RAW;
}

public void connect() {
}

public void disconnect() {
}

public void start() {
}

public void stop() {
}

/**
* Return the ImageSourceStreams.
*/
public PullBufferStream[] getStreams() {
return streams;
}

/**
* We could have derived the duration from the number of
* frames and frame rate. But for the purpose of this 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;
}
}

/**
* The source stream to go along with ImageDataSource.
*/
class ImageSourceStream implements PullBufferStream {

int width, height;
VideoFormat format;
float frameRate;
long seqNo = 0;

boolean ended = false;

public ImageSourceStream(int width, int height, int frameRate) {
this.width = width;
this.height = height;
this.frameRate = (float)frameRate;

format = new VideoFormat(VideoFormat.JPEG,
new Dimension(width, height),
Format.NOT_SPECIFIED,
Format.byteArray,
(float)frameRate);
}

/**
* We should never need to block assuming data are read from files.
*/
public boolean willReadBlock() {
return false;
}

/**
* This is called from the Processor to read a frame worth
* of video data.
*/
public void read(Buffer buf) throws IOException {

// Check if we've finished all the frames.
if (JPGImages.finished) {
// We are done. Set EndOfMedia.
System.err.println("Done reading all images.");
System.err.println("Frames: " + JPGImages.totalPics);
System.err.println("Missed frames: " +
(JPGImages.sentPics - JPGImages.totalPics));
buf.setEOM(true);
buf.setOffset(0);
buf.setLength(0);
ended = true;
return;
}

float time1 = seqNo * (1000 / frameRate) * 1000000;
long time = (long)time1;
buf.setTimeStamp(time);

buf.setSequenceNumber(seqNo++);

byte[] picBytes = JPGImages.readNode(); // read the next image in line
// in the DataList.
byte data[] = null;

// Read the entire JPEG image from the file.
data = picBytes;
buf.setData(data);

buf.setOffset(0);
buf.setLength((int)picBytes.length);
buf.setFormat(format);
buf.setFlags(buf.getFlags() | buf.FLAG_KEY_FRAME);

}

/**
* Return the format of each video frame. That will be 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;
}
}
}
______________________________________________________________________

With this class i just can creating video .MOV, can anyone know how to making this code can create video (AVI, MPEG, etc)...

I m sorry if i m in the wrong thread, if yes, i hope just moved this thread and pm me okay....

Thanks,