sucese / phoenix

The one-stop solution for taking pictures / videos, picture / video selection, editing and compression on the Android platform.
Apache License 2.0
1.62k stars 288 forks source link

能否提供一下压缩不同长宽比的视频文件对应的方法 #11

Closed lknlll closed 6 years ago

lknlll commented 6 years ago

demo中提供的方法如果不是16:9的视频则会报错

lknlll commented 6 years ago

OutputFormatUnavailableException

sucese commented 6 years ago

如果你是单独调用视频压缩,可以使用MediaFormatStrategyPresets.createAndroid480pFormatStrategy()来改变压缩策略,里面有480p,720p,以及指定宽高比等策略,如果是想在Phoenix.with()里配置,目前还没有这个配置参数,这周我会加一下。后续在内部做视频压缩的时候也会根据视频的宽高比选择合适的策略,不再固定480p。

final MediaEntity result = mediaEntity;

final File compressFile;
try {
    File compressCachePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "phoenix");
    compressCachePath.mkdir();
    compressFile = File.createTempFile("compress", ".mp4", compressCachePath);
} catch (IOException e) {
    Toast.makeText(context, "Failed to create temporary file.", Toast.LENGTH_LONG).show();
    return;
}
MediaTranscoder.Listener listener = new MediaTranscoder.Listener() {
    @Override
    public void onTranscodeProgress(double progress) {

    }

    @Override
    public void onTranscodeCompleted() {
        result.setCompressed(true);
        result.setCompressPath(compressFile.getAbsolutePath());
    }

    @Override
    public void onTranscodeCanceled() {

    }

    @Override
    public void onTranscodeFailed(Exception exception) {
    }
};
try {
    MediaTranscoder.getInstance().asyncTranscodeVideo(mediaEntity.getLocalPath(), compressFile.getAbsolutePath(),
            MediaFormatStrategyPresets.createAndroid480pFormatStrategy(), listener);
} catch (IOException e) {
    e.printStackTrace();
}
lknlll commented 6 years ago

Android480pFormatStrategy中的createVideoOutputFormat()方法

lknlll commented 6 years ago

Line 63 判断输入视频的宽高比,如果不是16:9的话就过不了啊,请问这个问题怎么解决

sucese commented 6 years ago

这个确实是限制在16:9的,其他宽高比的视频在输出的时候可能会在有些机型上造成无法播放的问题,具体可以看下Supported Media Formats

如果想要解决这个问题,目前只能把输入视频裁剪成16:9,目前该库还没有支持这个功能,后续考虑添加。