Converting into FLV using Java - java

does anybody know how to convert any kind of video format into flv using java, i have been searching for a java api for converting video but it seems that there is no such thing but there might be a way to do it, i mean to make something like youtube service does converting the videos, but using java, i need a web application that can show videos into FLv Format but to be uploaded in any format, if somebody has made something like this please let me know how or any idea,
thanks.

Using xuggler, here is a simple piece of code to do exactly what you asked for:
public class AnyMediaConverter {
public void main(String[] args) {
//assumes the following: arg0 is input file and arg1 is output file
IMediaReader reader = ToolFactory.makeReader(args[0]);
IMediaWriter writer = ToolFactory.makeWriter(args[1], reader);
writer.open();
writer.setForceInterleave(true);
IContainerFormat outFormat = IContainerFormat.make();
outFormat.setOutputFormat("flv", args[1], null);
IContainer container = writer.getContainer();
container.open(args[1], IContainer.Type.WRITE, outFormat);
writer.addVideoStream(0, 0, ICodec.findEncodingCodecByName("flv"), 320, 240);
writer.addAudioStream(1, 0, ICodec.findEncodingCodecByName("libmp3lame"), 2, 44100);
reader.addListener(writer);
while (reader.readPacket() == null);
}
}
now try doing that in JMF or FMJ or whatever (if you want a headache)

None in Java comes directly to mind, even Java's own media framework JMF doesn't support FLV, but you may find this overview of Open Source Flash Projects useful. If any non-Java commandline tool turns out to be useful for you, then you could execute it from inside Java using Runtime#exec() (tutorial here) or preferably ProcessBuilder (tutorial here).

There is a wrapper for ffmpeg that plugs into JMF: Fobs4JMF

Related

Is there a way to check the length of a MultipartFile upload if it's a video?

Is there a an elegant way to check a video's length in milliseconds when uploaded as a MultipartFile?
public long checkVideoLength(MultipartFile video) {
long length = 0;
if(video.getContentType().equals("video/mp4")) {
//Resolve the video's length if it's an mp4
}
return length;
}
I've been unable to find an elegant solution to this.
You need a library that can read the file and tell you the length. For example, using Caprica's VLCJ, which wraps VLC, once you point a MediaPlayer to your file, you can ask it for its length in seconds. I am not sure whether this will work without an interface, though.
Another option is Xuggler, which wraps FFMPEG. There, you can write the following to open a file and ask for its duration:
IMediaReader reader = ToolFactory.makeReader(filename);
IContainer container = reader.getContainer();
if (container.queryStreamMetaData() >= 0) {
long duration = container.getDuration(); // <-- got it
}
Note that there is a lot of variability in what you can expect from an .mp4 file (mp4 is a container, not an encoding) - so beware malformed or unsupported variants.
Both VLCJ and Xuggler are GPLv3; you may need to isolate your program from them as plugins unless your code will also be GPL'd, depending on how you interpret java linking.
Another option is to call a system script that provides you the number. This dispenses with Java integration, but forces you to keep the script updated and have its system dependency available. For ubuntu linux, these can work:
ffprobe file.mp4 -show_format_entry duration
or
avprobe file.mp4 -show_format_entry duration
or
$ mediainfo --Inform="Video;%Duration%" [inputfile]
all of them found in this answer

Problems with java and reading data from a CD drive in linux

I have been trying to write a simple audio ripper that i can use to learn how diffrent CODEC's work but i have got stuck on the first step, i cant get my program to read from the CD, the folowing code is what i have been trying to use
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class learning
{
public static void main(String[] args) throws IOException
{
File cd = new File( "/dev/sr0" );
RandomAccessFile rawAccess = new RandomAccessFile( cd, "r" );
byte[] content = new byte[20];
rawAccess.seek(19613);
rawAccess.readFully(content);
System.out.println(content);
}
}
but it gives me the folowing error
Exception in thread "main" java.io.IOException: Input/output error
at java.io.RandomAccessFile.readBytes(Native Method)
at java.io.RandomAccessFile.read(RandomAccessFile.java:355)
at java.io.RandomAccessFile.readFully(RandomAccessFile.java:414)
at java.io.RandomAccessFile.readFully(RandomAccessFile.java:394)
at learning.main(learning.java:21)
and i cant figure out why i get this, i though maby RandomFileAccess wasn't the right class to use but the only thing i could find said this should work
Any help on how to read CD's from java would be much appreciated.
Cheers Daniel
First of all you should mount on a directory which the user logged in the OS Linux have access. E.g. : /mnt/cdrom or /media/cdrom
After that open your mp3 file or audio file :
File cd = new File( "/dev/sr0/track1.mp3" );
or
File cd = new File( "/dev/sr0/track1.dat" );
(don't forget the extension of the Audio or Mp3 file).
This isn't exactly the same, but I suspect it will get you going...
Is there a platform independent way (Java?) to read an audio CD's TOC?
If you don't want to use the built in stuff (for educational reasons), http://www.tritonus.org/ is open source so you might be able to look at how they did it.
This question has a related problem and answer that is pertinent to your problem: Mount and unmount hard drives
According to the linked question and accepted answer, the answer is both "Yes" and "No". You can provide a Java API that use adapter pattern for native interface, but you will also have to do a number of things, thus making the solution not purely Java, but a hybrid:
create Java interfaces that support mount/unmount commands create
classes that implements interfaces as native methods create native
implementations of this commands in C or other language. One
implemantation for OS (Win, Mac, Linux) pack it to one jar build
small factory that provide implementation of interface and load
native libraries
If /dev/sr0 is directory, then there is a problem. Please try passing one of the audio files as parameter e.g.
File cd = new File( "/dev/sr0/track1" );
That should be working.

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");
}

JAAD stopping other providers from working

I'm using JAAD with SPI to play m4a files through JavaSound, which I have working fine.
However, I'd like to support a number of formats in this way - but whenever I try and play another format, JAAD seems to try to deal with it and then fail (obviously because it only deals with AAC.)
I'm assuming this is a bug in JAAD, since all the other SPI libraries play fine with each other. Is there a nice way to work around it until it's fixed, or is there another AAC library that works with Javasound which I can use?
There is a workaround using jaad created by Michael Berry: this is the url
https://code.google.com/p/quelea-projection/source/browse/Quelea/src/org/quelea/sound/AudioTrack.java?spec=svn0352523f49cf20d41d1a7dc098af1db38000cc6d&r=0352523f49cf20d41d1a7dc098af1db38000cc6d
Since it took me a while to find berry150's code, here is the solution:
First, you have to order the jars in the classpath so that JLayer, MP3SPI and Tritonous Share are loaded before JAAD. Then for getting the AudioInputStream, use the following code:
if (getAudioFormat().equals(".mp3")) {
audioStream = AudioSystem.getAudioInputStream(file); // Obtains an audio input stream of the song
}
else if (getAudioFormat().equals(".m4a")){
audioStream = new AACAudioFileReader().getAudioInputStream(file);
}
So what happens is that if the audio is mp3, the getAudioStreamMethod() from Javasound will be called first since its JAR was loaded first. If the audio is .m4a, a new instance of the ACCAudioFileReader() is created and the getAudioInputStream() of the JAAD library is called.

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