Java uses Jave to convert video format to 3GP

Source: Internet
Author: User

/**
* Convert uploaded video to 3gp SD format
*
* @param resourceId
* @return
*/
public static map<string, object> to3gp (String resourceId) {
String SourcePath = "E:";
String Sourcefilepath = SourcePath + "/" + resourceId;
Logger.info ("--------------------sourcefilepath:" + Sourcefilepath);
String Targetsourceid = (resourceid.substring (0,resourceid.lastindexof ("."))) + ". 3gp";
String Targetfilepath = SourcePath + "/" + Targetsourceid;
Logger.info ("--------------------targetfilepath:" + Targetfilepath);


map<string, object> returnmap = new hashmap<string, object> ();
File Source = new file (Sourcefilepath);
File target = new file (Targetfilepath);
Encoder Encoder = new Encoder ();
Multimediainfo Info=null;
if (!source.exists ()) {
Logger.info ("Source not exists");
}
try {
info = encoder.getinfo (source);
} catch (Exception e) {
Logger.error (e);
}
Get video information about a source file
Videoinfo videoinfo = Info.getvideo ();
int vodeobitrate=videoinfo.getbitrate (); Total bit Rate
int videoframerate= (int) videoinfo.getframerate ();//Frame rate
String Videodecoder=videoinfo.getdecoder ();
Videosize videosize = Videoinfo.getsize ();



Get audio information for a source file
Audioinfo audioinfo = Info.getaudio ();
int audiobitrate=audioinfo.getbitrate ();//Bit rate
String Audiodecoder=audioinfo.getdecoder ();
int audiochannel = Audioinfo.getchannels ();//Channel
int audiosimplingrate = Audioinfo.getsamplingrate ();//Audio sampling frequency


Audioattributes audio = new Audioattributes ();
Audio.setcodec ("LIBFAAC");
Audio.setbitrate (New Integer (8));
Audio.setsamplingrate (New Integer (AUDIOSIMPLINGRATE/2));
Audio.setchannels (New Integer (Audiochannel));


Videoattributes video = new Videoattributes ();
Video.setcodec ("MPEG4");
Video.setbitrate (New Integer (100000));
Video.setframerate (New Integer (16));
Video.setsize (New videosize ((int) (Videosize.getwidth ()/4), (int) (Videosize.getheight ()/4));


Encodingattributes attrs = new Encodingattributes ();
Attrs.setformat ("3gp");
Attrs.setaudioattributes (audio);
Attrs.setvideoattributes (video);


try {
Encoder.encode (source, target, attrs);
} catch (Exception e) {
Logger.error (e);
Targetsourceid= "";
}

Logger.info ("--------------------sourcesize:" + source.length ());
Logger.info ("--------------------targetsize:" + target.length ());
Returnmap.put ("ResourceID", Targetsourceid);
Integer resourcesize= (int) target.length ();
Returnmap.put ("Resourcesize", resourcesize);
return returnmap;
}

Java uses Jave to convert video format to 3GP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.