舉個例子:原視頻有7M,直接在Linux下執行裝換命令是正常的,而用該Java代碼執行該命令時視頻只能轉換1M;而且轉換成MP4的時候,視頻無法播放。
❷ java是如何編碼解碼的
[java]view plain
Stringname="Iam小明";
toHex(name.toCharArray());
<spanstyle="font-size:18px;white-space:pre;"></span><prename="code"class="java">try{
byte[]iso8859=name.getBytes("ISO-8859-1");
[java]view plain
toHex(iso8859);
[java]view plain
byte[]gb2312=name.getBytes("GB2312");
[java]view plain
toHex(gb2312);
[java]view plain
byte[]gbk=name.getBytes("GBK");
[java]view plain
toHex(gbk);
[java]view plain
}
[java]view plain
Stringstr="小米";
byte[]b=str.getBytes("UTF-8");
[java]view plain
publicbyte[]getBytes(StringcharsetName)
{
if(charsetName==null)thrownewNullPointerException();
returnStringCoding.encode(charsetName,value,0,value.length);
}
[java]view plain
staticbyte[]encode(StringcharsetName,char[]ca,intoff,intlen)
{
StringEncoderse=deref(encoder);
Stringcsn=(charsetName==null)?"ISO-8859-1":charsetName;
if((se==null)||!(csn.equals(se.requestedCharsetName())
||csn.equals(se.charsetName()))){
se=null;
try{
Charsetcs=lookupCharset(csn);//生成字元集實例
if(cs!=null)
se=newStringEncoder(cs,csn);
}catch(IllegalCharsetNameExceptionx){}
if(se==null)
(csn);
set(encoder,se);
}
returnse.encode(ca,off,len);
}
[java]view plain
(Stringcsn){
if(Charset.isSupported(csn)){
try{
returnCharset.forName(csn);
}catch(UnsupportedCharsetExceptionx){
thrownewError(x);
}
}
returnnull;
}
[java]view plain
privateStringEncoder(Charsetcs,Stringrcn){
this.requestedCharsetName=rcn;
this.cs=cs;
this.ce=cs.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
this.isTrusted=(cs.getClass().getClassLoader0()==null);
}
❸ 急急急!!!java 用ffmpeg將上傳的視頻轉成 .mp4 .ogg .webm 3種格式的視頻,沒思路,麻煩大神給點思路
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class ConvertVideo {
private final static String PATH = "c:\\ffmpeg\\input\\c.mp4";
public static void main(String[] args) {
if (!checkfile(PATH)) {
System.out.println(PATH + " is not file");
return;
}
if (process()) {
System.out.println("ok");
}
}
private static boolean process() {
int type = checkContentType();
boolean status = false;
if (type == 0) {
System.out.println("直接將文件轉為flv文件");
status = processFLV(PATH);// 直接將文件轉為flv文件
} else if (type == 1) {
String avifilepath = processAVI(type);
if (avifilepath == null)
return false;// avi文件沒有得到
status = processFLV(avifilepath);// 將avi轉為flv
}
return status;
}
private static int checkContentType() {
String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.length())
.toLowerCase();
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if (type.equals("avi")) {
return 0;
} else if (type.equals("mpg")) {
return 0;
} else if (type.equals("wmv")) {
return 0;
} else if (type.equals("3gp")) {
return 0;
} else if (type.equals("mov")) {
return 0;
} else if (type.equals("mp4")) {
return 0;
} else if (type.equals("asf")) {
return 0;
} else if (type.equals("asx")) {
return 0;
} else if (type.equals("flv")) {
return 0;
}
// 對ffmpeg無法解析的文件格式(wmv9,rm,rmvb等),
// 可以先用別的工具(mencoder)轉換為avi(ffmpeg能解析的)格式.
else if (type.equals("wmv9")) {
return 1;
} else if (type.equals("rm")) {
return 1;
} else if (type.equals("rmvb")) {
return 1;
}
return 9;
}
private static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
// 對ffmpeg無法解析的文件格式(wmv9,rm,rmvb等), 可以先用別的工具(mencoder)轉換為avi(ffmpeg能解析的)格式.
private static String processAVI(int type) {
List<String> commend = new ArrayList<String>();
commend.add("c:\\ffmpeg\\mencoder");
commend.add(PATH);
commend.add("-oac");
commend.add("lavc");
commend.add("-lavcopts");
commend.add("acodec=mp3:abitrate=64");
commend.add("-ovc");
commend.add("xvid");
commend.add("-xvidencopts");
commend.add("bitrate=600");
commend.add("-of");
commend.add("avi");
commend.add("-o");
commend.add("c:\\ffmpeg\\output\\a.avi");
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return "c:\\ffmpeg\\output\\a.avi";
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static boolean processFLV(String oldfilepath) {
if (!checkfile(PATH)) {
System.out.println(oldfilepath + " is not file");
return false;
}
// 文件命名
Calendar c = Calendar.getInstance();
String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000);
List<String> commend = new ArrayList<String>();
commend.add("c:\\ffmpeg\\ffmpeg");
commend.add("-i");
commend.add(oldfilepath);
commend.add("-ab");
commend.add("56");
commend.add("-ar");
commend.add("22050");
commend.add("-qscale");
commend.add("8");
commend.add("-r");
commend.add("15");
commend.add("-s");
commend.add("600x500");
commend.add("c:\\ffmpeg\\output\\a.flv");
try {
Runtime runtime = Runtime.getRuntime();
Process proce = null;
String cmd = "";
String cut = " c:\\ffmpeg\\ffmpeg.exe -i "
+ oldfilepath
+ " -y -f image2 -ss 8 -t 0.001 -s 600x500 c:\\ffmpeg\\output\\"
+ "a.jpg";
String cutCmd = cmd + cut;
proce = runtime.exec(cutCmd);
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
❹ 在java Base64編碼數據解碼問題,怎麼解決
import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; // 將 s 進行 BASE64 編碼 public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); } // 將 BASE64 編碼的字元串 s 進行解碼 public static String getFromBASE64(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }