Android 音視頻深入 六 使用FFmpeg播放視頻(附源碼下載)

來源:互聯網
上載者:User

標籤:release   資源   etc   1.0   mpeg   ndk   學習   dia   mpi   

本篇項目地址,求star
https://github.com/979451341/Audio-and-video-learning-materials/tree/master/FFmpeg%E6%92%AD%E6%94%BE%E8%A7%86%E9%A2%91

首先FFmpeg是c語言寫的,所以我們需要NDK的技術,然後我使用的NDK使用Cmake的,一開始就是說如何將FFmpeg匯入項目,使用我的方法匯入FFmpeg不用一分鐘。

這個需要大家先在上面的代碼地址裡下載項目代碼
因為FFmpeg這個基於android的so檔案如何產生的我不寫出來,我也是直接用別人檔案,直接使用我項目裡的就好了

1.FFmpeg簡單的說明

多媒體視頻處理工具FFmpeg有非常強大的功能包括視頻採集功能、視頻格式轉換、視頻抓圖、給視頻加浮水印等。

他的功能有7大部分完整

libavcodec:提供範圍更廣的轉碼器的實現。

libavformat:實現流媒體協議,容器格式和基本的I/O訪問。

libavutil:包括校正,解壓縮和各種實用功能。

libavfilter:提供了一個平均改變解碼音頻和視頻通過過濾器鏈。

libavdevice:提供抽象訪問捕獲和重放裝置。

libswresample:實現音訊混合和重採樣程式。

libswscale:實現色彩轉換和縮放程式。

2.環境配置

將下載的項目裡jniLibs和cpp粘貼到自己建立的項目的main檔案夾下

我還需要在app module的build.gradle添加代碼,在defaultConfig裡添加ndk支援的類型,還有給Cmake添加參數,在android下匯入CMakeLists檔案,例子代碼如下:

android {
compileSdkVersion 26
defaultConfig {
applicationId "jonesx.videoplayer"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
abiFilters ‘armeabi‘
}
externalNativeBuild {
cmake {
arguments ‘-DANDROID_TOOLCHAIN=clang‘,‘-DANDROID_STL=gnustl_static‘
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
}

3.代碼說明

首先就是能夠使用cpp檔案夾下的VideoPlayer的代碼,那我們就需要建立一個VideoPlayer的java類

public class VideoPlayer {

static {    System.loadLibrary("VideoPlayer");}public static native int play(Object surface);

}

使用這個play函數,直接在SurfaceView的surfaceCreated函數裡開啟線程使用

@Overridepublic void surfaceCreated(SurfaceHolder holder) {    new Thread(new Runnable() {        @Override        public void run() {            VideoPlayer.play(surfaceHolder.getSurface());        }    }).start();}

那重點來了,說一說VideoPlayer用到了FFmpeg哪些東西

擷取視頻格式的環境,開啟MP4檔案

AVFormatContext *pFormatCtx = avformat_alloc_context();

if (avformat_open_input(&pFormatCtx, file_name, NULL, NULL) != 0) {    LOGD("Couldn‘t open file:%s\n", file_name);    return -1; // Couldn‘t open file}

查看是否有流,如果那就看是否有視頻流

if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {    LOGD("Couldn‘t find stream information.");    return -1;}int videoStream = -1, i;for (i = 0; i < pFormatCtx->nb_streams; i++) {    if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO        && videoStream < 0) {        videoStream = i;    }}if (videoStream == -1) {    LOGD("Didn‘t find a video stream.");    return -1; // Didn‘t find a video stream}

獲得視頻解碼器環境,然後看這個解碼器是否能夠開啟

AVCodecContext *pCodecCtx = pFormatCtx->streams[videoStream]->codec;// Find the decoder for the video streamAVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL) {    LOGD("Codec not found.");    return -1; // Codec not found}if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {    LOGD("Could not open codec.");    return -1; // Could not open codec}

通過surface擷取目前手機螢幕給這個Surface的記憶體空間

// 擷取native windowANativeWindow *nativeWindow = ANativeWindow_fromSurface(env, surface);// 擷取視頻寬高int videoWidth = pCodecCtx->width;int videoHeight = pCodecCtx->height;// 設定native window的buffer大小,可自動展開ANativeWindow_setBuffersGeometry(nativeWindow, videoWidth, videoHeight,                                 WINDOW_FORMAT_RGBA_8888);ANativeWindow_Buffer windowBuffer;if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {    LOGD("Could not open codec.");    return -1; // Could not open codec}

轉格式

struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,
AV_PIX_FMT_RGBA,
SWS_BILINEAR,
NULL,
NULL,
NULL);

首先這個解碼是在一個迴圈裡,然後解碼,和之前一樣一幀一幀的解碼,但是如果一幀太大那就下一次迴圈裡繼續解碼

avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

釋放資源

av_free(buffer);av_free(pFrameRGBA);// Free the YUV frameav_free(pFrame);// Close the codecsavcodec_close(pCodecCtx);// Close the video fileavformat_close_input(&pFormatCtx);

完了,說是完了,這隻是開始,我對FFmpeg的學習也是開始,以後我可能斷斷續續的分享我使用FFmpeg的心得。

Android 音視頻深入 六 使用FFmpeg播放視頻(附源碼下載)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.