Commit 4dc864dc authored by angermue's avatar angermue
Browse files

Improved audio handling by supporting *.mp2 audio files and *.mp3 files which...

Improved audio handling by supporting *.mp2 audio files and *.mp3 files which were created by an older ttt version still using JMF 2.1.1a

git-svn-id: https://www2.in.tum.de/repos/ttt/trunk@34 0463f305-d864-43cb-8a47-61cf597d4139
parent ac2199fe
......@@ -70,6 +70,16 @@ public class FlashContext extends FlashActionHelper {
public static boolean flash_debug = false;
private boolean batch;
/**
* Checks whether it is possible to create a swf file.
* @param recording
* @return True if there is a mp3 file, or wav or mp2 file which can be encoded to mp3 file using lame.
* @throws IOException
*/
public static boolean isCreationPossible(Recording recording) throws IOException {
return recording.getExistingFileBySuffix("mp3").exists() || (LameEncoder.isLameAvailable() && recording.getExistingFileBySuffix(new String[] {"wav","mp2"}).exists());
}
static public void createFlash(Recording recording, boolean batch) throws IOException {
FlashContext flashContext = new FlashContext(recording, batch);
......@@ -176,36 +186,54 @@ public class FlashContext extends FlashActionHelper {
protected void initializeSound() throws IOException {
// initialise sound
File file = null;
try {
File file = recording.getExistingFileBySuffix("mp3");
if (file.exists() == false) {
//try to encode audio file if mp3 file doesn't exist
if (LameEncoder.convertAudioFile(Constants.getExistingFile(recording.fileDesktop.getCanonicalPath(), Constants.AUDIO_FILE), recording.getFileBySuffix("mp3"), batch)) {
if (!batch) {
progressMonitor.setProgress(30);
}
System.out.println(" audio converted to mpeg layer 3");
file = recording.getExistingFileBySuffix("mp3");
} else {
//canceled by user
progressMonitor = null;
return;
}
}
System.out.println(" loading audio from " + file);
FileInputStream mp3 = new FileInputStream(file);
blocks = new ArrayList();
System.out.print(" transform audio to flash ... ");
head = MP3Helper.streamingBlocks(mp3, frameRate, blocks);
System.out.println("done");
mp3.close();
file = recording.getExistingFileBySuffix(new String[] {"mp3","mp2","wav"});
if (file.getName().toLowerCase().endsWith(".mp3")) {
//if there is already a mp3 file no additional audio encoding is necessary
try {
getSoundStreamHead(file);
return;
} catch (Exception e) {
/*if the mp3 file header can not be retrieved the audio file was probably encoded to mpeg layer II format by an older ttt version
* still using JMF 2.1.1a. That's why rename the audio file to *.mp2 and encode it to mpeg layer III correctly via lame.
*/
file.renameTo(file = new File(file.getAbsolutePath() + ".mp2"));
}
}
//try to encode audio file to mp3 format
if (LameEncoder.convertAudioFile(file, recording.getFileBySuffix("mp3"), batch)) {
if (!batch) {
progressMonitor.setProgress(30);
}
System.out.println(" audio converted to mpeg layer 3");
file = recording.getExistingFileBySuffix("mp3");
} else {
//canceled by user
progressMonitor = null;
return;
}
getSoundStreamHead(file);
} catch (Exception e) {
if (file != null && file.getName().toLowerCase().endsWith("mp3.mp2")) {
//if the audio file was renamed from *.mp3 to *.mp3.mp2 and the audio encoding failed undo renaming
file.renameTo(new File(file.getAbsolutePath().substring(0,file.getAbsolutePath().length()-4)));
}
System.out.println(" audio failed - cannot create flash movie");
throw new IOException("Cannot transcode audio to flash");
}
}
protected void getSoundStreamHead(File file) throws IOException {
System.out.println(" loading audio from " + file);
FileInputStream mp3 = new FileInputStream(file);
blocks = new ArrayList();
System.out.print(" transform audio to flash ... ");
head = MP3Helper.streamingBlocks(mp3, frameRate, blocks);
System.out.println("done");
mp3.close();
}
/*******************************************************************************************************************
* main processing loop
******************************************************************************************************************/
......
......@@ -63,13 +63,16 @@ public class LameEncoder {
* @return True: Conversion succeeded.<br>False: Canceled by user.
*/
public static boolean convertAudioFile(File inFile, File outFile, String options, boolean batch) throws Exception {
System.out.println("----------------------------------------------");
System.out.println("LameEncoder");
System.out.println("----------------------------------------------");
System.out.println("Encoding mp3 file");
long startTime = System.currentTimeMillis(); //time measurement
String lameCmd = Exec.getCommand(LAME); //get lame command
if (lameCmd == null) {
throw new IOException("lame not found");
}
System.out.println("Encoding mp3 file");
long startTime = System.currentTimeMillis(); //time measurement
}
outFile.delete(); //delete outFile in order to test success after encoding
final Exec exec = new Exec();
......@@ -95,7 +98,7 @@ public class LameEncoder {
});
timer.start();
//call lame
//call lame
exec.createListenerStream();
i = exec.exec(new String[] {lameCmd,options,inFile.getPath(), outFile.getPath()});
......@@ -120,7 +123,8 @@ public class LameEncoder {
outFile.delete();
throw new IOException("unable to encode audio file using lame");
}
System.out.println("Done in " + Constants.getStringFromTime((int)(System.currentTimeMillis()-startTime)));
System.out.println("Done in " + Constants.getStringFromTime((int)(System.currentTimeMillis()-startTime)));
System.out.println("----------------------------------------------");
return true;
}
......
......@@ -62,7 +62,7 @@ public class PodcastCreator {
* @throws IOException
*/
public static boolean isCreationPossible(Recording recording) throws IOException {
return Exec.getCommand(FFMPEG) != null && Exec.getCommand(MP4BOX) != null && (recording.getExistingFileBySuffix("mp3").exists() || recording.getExistingFileBySuffix("wav").exists());
return Exec.getCommand(FFMPEG) != null && Exec.getCommand(MP4BOX) != null && (recording.getExistingFileBySuffix(new String[] {"wav","mp3","mp2"}).exists());
}
......@@ -74,8 +74,10 @@ public class PodcastCreator {
* @throws Exception
*/
public static boolean createPodcast(Recording recording, boolean batch) throws Exception {
long startTime = System.currentTimeMillis();
System.out.println("----------------------------------------------");
System.out.println("PodcastCreator");
System.out.println("----------------------------------------------");
System.out.println("Creating mp4 podcast");
//Check whether the necessary applications are available.
......@@ -94,15 +96,13 @@ public class PodcastCreator {
}
}
//Get audio file
File audioFile = recording.getExistingFileBySuffix("wav");
File audioFile = recording.getExistingFileBySuffix(new String[] {"wav","mp3","mp2"});
if (audioFile.exists() == false) {
audioFile = recording.getExistingFileBySuffix("mp3");
if (audioFile.exists() == false) {
throw new IOException("No audio file found");
}
throw new IOException("No audio file found");
}
//Initialization
long startTime = System.currentTimeMillis();
File outMovieFile = recording.getFileBySuffix("mp4"); //final output
outMovieFile.delete();
File outMovieTmpFile = recording.getFileBySuffix("tmp.mp4"); //temporary output for joined slide movies
......@@ -227,6 +227,7 @@ public class PodcastCreator {
throw new IOException("unable to add audio stream using ffmpeg");
}
System.out.println("Podcast created in " + Constants.getStringFromTime((int)(System.currentTimeMillis()-startTime)));
System.out.println("----------------------------------------------");
return true;
}
}
......@@ -107,20 +107,19 @@ public class PostProcessorPanel extends GradientPanel {
ocrCheckBox
.setToolTipText("generate optimized input for optical character recognition (see full text search help)");
//the conversion of a wav file to a mp3 file is only accessible if lame is found and if there exists a wav file for the recording
if (LameEncoder.isLameAvailable() && recording.getExistingFileBySuffix("wav").exists()) {
mp3CheckBox.setToolTipText("generate a mp3 audio file from wav file");
//the conversion of a audio file to a mp3 file is only accessible if lame is found and if there exists a wav or mp2 file of the recording
if (LameEncoder.isLameAvailable() && recording.getExistingFileBySuffix(new String[] {"wav","mp2"}).exists()) {
mp3CheckBox.setToolTipText("generate a mp3 audio file from wav or mp2 file");
} else {
if (LameEncoder.isLameAvailable()) {
mp3CheckBox.setToolTipText("wav file not found");
mp3CheckBox.setToolTipText("audio file not found");
} else {
mp3CheckBox.setToolTipText("lame not found");
}
mp3CheckBox.setSelected(false);
mp3CheckBox.setEnabled(false);
}
//the creation of a flash movie is only accessible if there is a mp3 file or a mp3 file can be created via lame
if (recording.getExistingFileBySuffix("mp3").exists() || LameEncoder.isLameAvailable() && recording.getExistingFileBySuffix("wav").exists()) {
}
if (FlashContext.isCreationPossible(recording)) {
flashCheckBox.setToolTipText("generate a flash/swf version of this recording");
if (recording.getExistingFileBySuffix("mp3").exists() == false) {
flashCheckBox.addItemListener(new ItemListener() {
......@@ -137,7 +136,7 @@ public class PostProcessorPanel extends GradientPanel {
});
}
} else {
flashCheckBox.setToolTipText("mp3 file not found");
flashCheckBox.setToolTipText("audio file not found");
flashCheckBox.setSelected(false);
flashCheckBox.setEnabled(false);
}
......@@ -1372,10 +1371,10 @@ public class PostProcessorPanel extends GradientPanel {
// NOTE: store thumbnails now, because flash generation may fail due to insufficient memory
storeRecordingIfNeeded();
//convert wav file
//convert audio file
try {
if (mp3CheckBox.isSelected()) {
LameEncoder.convertAudioFile(recording.getExistingFileBySuffix("wav"), recording.getFileBySuffix("mp3"), batch);
LameEncoder.convertAudioFile(recording.getExistingFileBySuffix(new String[] {"wav","mp2"}), recording.getFileBySuffix("mp3"), batch);
}
} catch (Exception e) {
TTT.showMessage("MP3 creation failed: " + e);
......
......@@ -170,6 +170,17 @@ public class Recording extends MessageProducerAdapter implements Runnable, Actio
File file = getFileBySuffix(suffix);
return file.exists() ? file : getFileBySuffix(suffix.toUpperCase());
}
public File getExistingFileBySuffix(String[] suffix) throws IOException {
for (String curSuffix: suffix) {
File file = getFileBySuffix(curSuffix);
if (file.exists()) {
return file;
}
}
return getFileBySuffix(suffix[0]);
}
void read(String filename) throws IOException {
// open file
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment