Android利用ffmpeg做視頻裁剪

根據自己需求,編譯so,或者用網上搜現成的;https://github.com/FFmpeg/FFmpeg


時長裁切:

ffmpeg -i src.mp4 -ss 3 -t 10 out.mp4

3表示起始時間,10表示長度,也就表示視頻裁切出來是3-13s的視頻;


尺寸裁切:

ffmpeg -i src.mp4 -strict -2 -vf crop=1080:500:0:420 -preset fast out.mp4

1080表示寬度;500表示高度;0表示X起始點;420表示Y起始點;

 -preset fast 會保持原有清晰度;



//-ss 0 -t 5  時間裁切
//-strict -2 -vf crop=500:500:0:100   尺寸裁切
String cmd = "/data/data/" + context.getPackageName() + "/ffmpeg" + " -y -i "
        + srcVideoPath
        + " -ss " + start + " -t " + duration
        + " -strict -2 -vf crop=" + width + ":" + height + ":" + x + ":" + y + " -preset fast "
        + destPath;
-y 會覆蓋輸出文件;


Github源碼:https://github.com/miLLlulei/VideoCrop ,歡迎大家來star,謝謝;




下面是一些其他方法:

/**
 * 獲取本地視頻信息
 */
public static LocalVideoBean getLocalVideoInfo(String path) {
    LocalVideoBean info = new LocalVideoBean();
    info.src_path = path;

    android.media.MediaMetadataRetriever mmr = new android.media.MediaMetadataRetriever();
    try {
        mmr.setDataSource(path);
        info.duration = (Long.valueOf(mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION)));
        info.width = Integer.valueOf(mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
        info.height = Integer.valueOf(mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        mmr.release();
    }
    return info;
}

    /**
     * 獲取視頻幀列表
     *
     * @param path
     * @param count    期望個數
     * @param width    期望壓縮後寬度
     * @param height   期望壓縮後高度
     * @param listener
     */
    public static void getLocalVideoBitmap(final String path, final int count, final int width, final int height, final OnBitmapListener listener) {
        AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() {
            @Override
            protected Object doInBackground(Object... params) {
                android.media.MediaMetadataRetriever mmr = new android.media.MediaMetadataRetriever();
                try {
                    mmr.setDataSource(path);
                    long duration = (Long.valueOf(mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION))) * 1000;
                    long inv = (duration / count);

                    for (long i = 0; i < duration; i += inv) {
                        //注意getFrameAtTime方法的timeUs 是微妙, 1us * 1000 * 1000 = 1s
                        Bitmap bitmap = mmr.getFrameAtTime(i, MediaMetadataRetriever.OPTION_CLOSEST);
//                        Log.d(VideoFFCrop.TAG, "getFrameAtTime "+ i + "===" + bitmap.getWidth() + "===" + bitmap.getHeight());
                        Bitmap destBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
                        Log.d(VideoFFCrop.TAG, "getFrameAtTime " + i + "===" + destBitmap.getWidth() + "===" + destBitmap.getHeight());
                        bitmap.recycle();

                        publishProgress(destBitmap);
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                } finally {
                    mmr.release();
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Object... values) {
                if (listener != null) {
                    listener.onBitmapGet((Bitmap) values[0]);
                }
            }

            @Override
            protected void onPostExecute(Object result) {

            }
        };
        task.execute();
    }

    public interface OnBitmapListener {
        void onBitmapGet(Bitmap bitmap);
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章