Java 如何使用jcodec更改视频分辨率
我需要降低一些mp4文件的视频分辨率,我决定使用。我试图搜索一些例子如何做到这一点,但没有找到任何例子。关于这方面的任何信息都会很有用。我也在寻找答案,但没有找到任何答案 因此,我研究了代码,意识到在将位图传递给JCodec之前缩放位图可以解决问题 这是我使用的类。请注意,这是Android的代码,它使用最新的Android JCodecJava 如何使用jcodec更改视频分辨率,java,android,blackberry-10,video-processing,Java,Android,Blackberry 10,Video Processing,我需要降低一些mp4文件的视频分辨率,我决定使用。我试图搜索一些例子如何做到这一点,但没有找到任何例子。关于这方面的任何信息都会很有用。我也在寻找答案,但没有找到任何答案 因此,我研究了代码,意识到在将位图传递给JCodec之前缩放位图可以解决问题 这是我使用的类。请注意,这是Android的代码,它使用最新的Android JCodec public class DDVideoEncoder { private static final String TAG = DDVideoEn
public class DDVideoEncoder {
private static final String TAG = DDVideoEncoder.class.getSimpleName();
private SequenceEncoder encoder;
public DDVideoEncoder(String filepath) throws IOException, OutOfMemoryError {
File out = new File(filepath);
encoder = new SequenceEncoder(out);
}
// returns false on out of memory error.
public Boolean addFrame(String filePath, int newSize) throws IOException {
// newSize indicates size of bitmap in percent
Bitmap bi = getResizedBitmap(filePath, newSize);
try { encoder.encodeImage(bi); }
catch (OutOfMemoryError outOfMemoryError) { Log.d(TAG, "encodeImage:" + outOfMemoryError); return false; }
catch (NullPointerException nil) { Log.d(TAG, "encodeImage:"+nil); return false; }
return true;
}
public void finishEncoding(){
try { encoder.finish(); }
catch (IOException io) {
Log.d("DDVideoEncoder", "IOException"); }
}
// newSize indicates size of bitmap in percent of original.
//decodes image and scales it to reduce memory consumption
private Bitmap getResizedBitmap(String filePath, int newSize) {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
//BitmapFactory.decodeStream(new FileInputStream(f),null,o);
BitmapFactory.decodeFile(filePath, o);
//The new size we want to scale to
final int REQUIRED_SIZE=newSize*10;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
//return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
return BitmapFactory.decodeFile(filePath, o2);
}
}
然后可以像这样使用类:
try {
DDVideoEncoder tmpEncoder = new DDVideoEncoder(dir+vidName);
// write x frames
Log.d("test", "adding frame");
try { tmpEncoder.addFrame(path, 50); }
catch (OutOfMemoryError outOfMemoryError){ Log.d("test", "oom"); break; }
catch (IOException io) { Log.d("CactusMemChecker", io.getMessage()); break; }
catch (IndexOutOfBoundsException oob) { Log.d("test", "oob"); break; }
// finish up
tmpEncoder.finishEncoding();
} catch (IOException e) {
Log.d("test", e.getMessage());
testResult = false;
} catch (OutOfMemoryError e) {
Log.d("test", e.getMessage());
testResult = false;
} catch (IndexOutOfBoundsException e) {
Log.d("test", e.getMessage());
testResult = false;
}
做了一些研究,jcodec不是为android设计的,你会遇到很多问题。。您应该使用:
jcodec不是为android制作的。另外,我不能使用ffmpeg
,因为我正在将应用程序移植到黑莓10位图的路径