How to write FLAC files in java - java

I have a requirement to write FLAC files in java. Earlier I was writing the audio input into a WAV file and then converting it to FLAC file using a external converter
I was looking into JFlac to find any API through which I can write FLAC files. I found that AudioFileFormat.TYPE in java supports only the following file formats - AIFC, AIFF, SND, AU, WAVE .
I would like to have a method where I can capture the audio from the microphone and, using an API such as Audiosystem.write, write it to a FLAC file instead of WAV file.
Please suggest a method or an API that can solve my problem.

You can use this lib. Here is a simple example using version 0.2.3 (javaFlacEncoder-0.2.3-all.tar.gz). Extract the dowloaded file, and then import javaFlacEncoder-0.2.3.jar to your project. For more documentation, see here:
package fr.telecomParisTech;
import java.io.File;
import javaFlacEncoder.FLAC_FileEncoder;
public class SoundConverter {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FLAC_FileEncoder flacEncoder = new FLAC_FileEncoder();
File inputFile = new File("hello.wav");
File outputFile = new File("hello.flac");
flacEncoder.encode(inputFile, outputFile);
System.out.println("Done");
}
}

You can write audio stream directly to a FLAC file with javaflacencoder:
AudioSystem.write(audioInputStream, FLACFileWriter.FLAC, new File("E:\\temp.flac"));

If you want to change sample rates,
use ffmpeg
like this
ffmpeg -i sourcefile.wav -ar 16000 targetfile.flac

Related

How to write Java AudioInputStream to MP3?

Now I have an AudioInputStream, using the following code I can write it to a WAVE file. While what I want is an MP3 file, what should I do?
AudioInputStream ais= new AudioInputStream(bais1, audioFormat, bufferSize);
try {
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File("demoFile.wav")
);
}
catch(Exception e) {
e.printStackTrace();
}
In my project, the cache is not big enough to store big files, which means solution like using a tool to convert WAVE to MP3 is not allowed(WAVE file is too big).
Here is a github library of java audio utilities that include claims of being able to encode mp3.
pududits.soundlibs
I haven't used the mp3 libraries, only ogg/vorbis decoding. I'd be tempted to try the JOrbis encoder for ogg/vorbis before getting into mp3's.

Using Java - Encrypting sd card video by writing a random byte into the file

I am trying this code in java, as android internally uses java as well.
My question is about how do I corrupt (encrypt) a mp4 video, so that even if some body copies those videos, it wouldn't play.
I tried corrupting a mp4 video by adding "hi" to ending location of that file, so that the file be corrupted and vlc player can't play it. I could able to successfully corrupt the video. VLC no more able to play video.
While decrypting we are reading the content except last 2 characters "hi" from that file, and copied the content into another file, to check if I can decrypt properly.
But problem is de-crypted video is not playing properly.VLC is not reporting any error, but a green color window is shown in place of video, sound is also not coming.
After decrypting the size of the file is proper as expected.
My doubt is when I am reading the data of mp4 file and copying into other file, some how mp4 file properties are missing, that is the reason why vlc might not be able to play the way it is supposed to play.If that is the case, Are there any ways to set mp4 properties manually to a decrypted file, so that vlc can play it properly?
I know that there are many cipher classes to do this, but unfortunately encrypting and decrypting a video file of size 50mb could be too slow. I don't want a delay of more than 5 seconds to my video users.I tried already cipher but it is slow, taking more than 30 seconds some times which might irritate genuine video users who paid for the videos.
I just want a very light weight security model, which can protect my videos to some extent (As you know locks are not meant for thieves).
If any tried a simple algorithm like this, to corrupt mp4 video manually and decrypt on the fly before playing, please help me. Apart from this we are also hiding the files, changing the extension of the file, and splitting a video into smaller files which will be merged through our application just before playing.
My code which i tried, pasting here.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributes;
public class Testing {
public static void main(String[] args) {
File file = new File("e:/ascii.mp4"); //original file path
File file2 = new File("e:/satish.mp4"); //encrypted file path
File file3 = new File("e:/satish2.mp4"); //decrypted file path
try {
String s = new String(Files.readAllBytes(file.toPath()));
s = s+"hi"; //appending hi to mp4 content
FileWriter fw = new FileWriter(file2);
fw.write(s); //writing mp4 content + hi to encrypted file
fw.close();
String s2 = new String(Files.readAllBytes(file2.toPath()));
s2 = s2.substring(0, s2.length()-1-2); //reading content except last 2 characters.
FileWriter fw2 = new FileWriter(file3); //decrypted file
fw2.write(s2); //writing content to decrypted file.
fw2.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
Finally I could able to get the solution for this.
I used the logic of this question. with little modifications which I specified there in that link itself. I could able to decrypt my 52 mb video in just 4 seconds.

best approach for extract / extracting images / image sequence from videos / video file in Java

Well, there is FFMPEG and some Java bindings and wrappers for it but I need to distribute for each specific platform the right binary file of FFMPEG.
Isnt there any plain Java solution or library without any dependencies like FFMPEG for converting a video fle to an image sequence?
Solutions like FFMPEG, XUGGLER or JMF (abandoned) are not suitable. Is there really no pure Java solution for this?
Maybe for specific video codecs / files at least?
I just want to extract the images from the video file to jpeg / png files and save them to the disk
Is there really no pure Java solution for [extracting images from a video stream]?
Let's see. You have to:
Decode the video.
Present the decoded images at least as fast as 24 images / second. I suppose you can skip this step.
Save the decoded images.
It appears that decoding the video would be the most challenging step. People and companies have spent years developing codecs (encoder / decoder) for various video formats.
There's a project on SourceForge, JMF wrapper for ffmpeg, that has developed a few pure Java video codecs. Perhaps you can look at their source code and see how to develop a Java video codec for yourself.
You can look for other pure Java video codecs if you wish.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber.Exception;
public class Read{
public static void main(String []args) throws IOException, Exception, InterruptedException, ExecutionException
{
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber("C:/Users/Digilog/Downloads/Test.mp4");
frameGrabber.start();
IplImage i;
try {
for(int ii=0;ii<frameGrabber.getLengthInFrames();ii++){
i = frameGrabber.grab();
BufferedImage bi = i.getBufferedImage();
String path = "D:/Image/Image"+ii+".png";
ImageIO.write(bi,"png", new File(path));
}
frameGrabber.stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
There is a pure Java implementation of the following codecs: H.264 ( AVC ), MPEG 1/2, Apple ProRes, JPEG; and the following file formats: MP4 ( ISO BMF, QuickTime ), Matroska, MPEG PS and MPEG TS.
The library is called JCodec ( http://www.jcodec.org ).
It has very little documentation for now but the development team is constantly working on this.
Here's how you can simply grab a frame from an MP4 file ( sample from their web site ):
int frameNumber = 150;
BufferedImage frame = FrameGrab.getFrame(new File("filename.mp4"), frameNumber);
ImageIO.write(frame, "png", new File("frame_150.png"));
To add JCodec to your project you can simply add below to your pom.xml:
<dependency>
<groupId>org.jcodec</groupId>
<artifactId>jcodec</artifactId>
<version>0.1.3</version>
</dependency>
For latest version, see here.

How to compress video file to 3gp or mp4 in java?

i am having a video file which i need to convert to 3gp, mp4 using java.
is there any library to do this or any sample ?
if any of you have done or know the sample please guide me to do the above or provide me the example.
thanks
If you MUST use Java, check Java Media Framework.
http://www.oracle.com/technetwork/java/javase/tech/index-jsp-140239.html
Use a tool like ffmpeg (ffmpeg.org) or mconvert (from MPlayer) or VLC. You can call them from Java using the ProcessBuilder or Commons Exec.
You may have look at pandastream. But it is a web service.
Compressing video in java you can use IVCompressor
is very easy to use
for more details go https://techgnious.github.io/IVCompressor/
simple code
<dependency>
<groupId>io.github.techgnious</groupId>
<artifactId>IVCompressor</artifactId>
<version>1.0.1</version>
</dependency>
public static void main(String[] args) throws VideoException, IOException {
IVCompressor compressor = new IVCompressor();
IVSize customRes = new IVSize();
customRes.setWidth(400);
customRes.setHeight(300);
File file = new File("D:/Testing/20.mp4");
compressor.reduceVideoSizeAndSaveToAPath(file,VideoFormats.MP4,ResizeResolution.R480P,"D:/Testing/Custome");
}

could not get audio input stream from input URL

I am trying to run a sound file in Java using this code:
public class Audio3
{
public static void main(String[] args) throws Exception
{
URL soundFile =new URL(
"http://everyayah.com/data/Ghamadi_40kbps/audhubillah.mp3");
AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
AudioPlayer.player.start(ais);
}
}
I am getting this exception
javax.sound.sampled.UnsupportedAudioFileException:
could not get audio input stream from input URL
Any idea what could be the reason?
According to the JavaSound info. page.
MP3 decoding support
The Java Sound API does not support many formats of sampled sound internally. In a 1.6.0_24 Oracle JRE getAudioFileTypes() will generally return {WAVE, AU, AIFF}. An MP3 decoder at least, is close by. The mp3plugin.jar of the Java Media Framework supports decoding MP3s.
I can vouch for that information since I've successfully loaded MP3s using Java Sound and the MP3 SPI (& also wrote the info. page ;) ). The JMF installer download is becoming hard to find, but you can get the mp3plugin.jar direct from where I put it for use in JWS apps.

Categories

Resources