androidffmpeg使用
⑴ 如何在Android用FFmpeg解碼圖像
fetch code
用git把ffmpeg(我用的github上FFmpeg-Android)和x264(vlc的官方git)分別都clone下來。
build x264
在x264目錄裡面寫一個myconfig.sh(其實直接把這些命令打在終端也行,問題是有的時候需要改來改去,不如寫個文件改起來方便)
export NDK=/opt/android-ndk
export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt
export PLATFORM=$NDK/platforms/android-14/arch-arm
export PREFIX=/home/mingkai/softwares/x264
./configure \
--enable-pic \
--enable-static \
--enable-shared \
--disable-asm \
--disable-cli \
--host=arm-linux \
--cross-prefix="/opt/android-ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-"
\
--sysroot=$PLATFORM \
--prefix=$PREFIX
其中prefix貌似直接用"arm-linux-androideabi-「也可以。
然後可以make和make install了。(記得改PREFIX等環境變數)
build FFmpeg
這個是從github上FFmpeg-Android裡面的FFmpeg-Android.sh裡面改了改一些參數。
最主要的是FFMPEG_FLAGS,裡面都是一些關於FFmpeg的參數設定,尤其是是否啟用encoder/decoder之類的。
還有一點就是再下面EXTRA_CFLAGS裡面加上
「-I/path/to/x264/include」
EXTRA_LDFLAGS裡面加上
「-L/path/to/x264/lib -lx264」。
⑵ 如何在Android用FFmpeg解碼圖像
這涉及到兩個問題,一個是解碼,另一個是顯示,解碼問題要先交叉編譯ffmpeg,然後參考下面的解碼流程
avcodec_register_all();
/*
########################################
[1]
########################################
*/
av_register_all();
/*
// Open video file
########################################
[2]
########################################
*/
pFormatCtx = avformat_alloc_context();
if(avformat_open_input(&pFormatCtx, filename, NULL, NULL)!=0)
return -1; // Couldn't open file
// Retrieve stream information
/*
########################################
[3]
########################################
*/
if(avformat_find_stream_info(pFormatCtx,NULL)<0)
return -1; // Couldn't find stream information
// Dump information about file onto standard error
// mp_format(pFormatCtx, 0, argv[1], 0);
// Find the first video stream
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
if(videoStream==-1)
return -1; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
// Open codec
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
pFrame=avcodec_alloc_frame();
// Allocate an picture structure
avpicture_alloc(&picture, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
// Determine required buffer size and allocate buffer
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
if(img_convert_ctx == NULL)
{
fprintf(stderr, "Cannot initialize the conversion context!\n");
exit(1);
}
// Read frames and save first five frames to disk
/*
########################################
[4]
########################################
*/
i=0;
dirtyRegion.set(android::Rect(0x3FFF, 0x3FFF));
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
&packet);
// Did we get a video frame?
if(frameFinished) {
// Convert the image from its native format to RGB
sws_scale(img_convert_ctx,
pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
picture.data, picture.linesize);
}
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
pFrame為解碼後的數據,將它顯示在畫布上,就完成了FFMEPG解碼
⑶ 如何在Android用FFmpeg解碼圖像
CC libavcodec/log2_tab.o
CC libavutil/log2_tab.o
CC libswresample/log2_tab.o
AR libavcodec/libavcodec.a
LD libavutil/libavutil.so.52
AR libavutil/libavutil.a
AR libswresample/libswresample.a
LD libavcodec/libavcodec.so.55
LD libswresample/libswresample.so.0
LD libswscale/libswscale.so.2
LD libavformat/libavformat.so.55
INSTALL libavformat/libavformat.a
INSTALL libavformat/libavformat.so
STRIP install-libavformat-shared
INSTALL libavcodec/libavcodec.a
INSTALL libavcodec/libavcodec.so
STRIP install-libavcodec-shared
INSTALL libswresample/libswresample.a
INSTALL libswresample/libswresample.so
STRIP install-libswresample-shared
INSTALL libswscale/libswscale.a
INSTALL libswscale/libswscale.so
STRIP install-libswscale-shared
INSTALL libavutil/libavutil.a
INSTALL libavutil/libavutil.so
STRIP install-libavutil-shared
INSTALL libavformat/avformat.h
INSTALL libavformat/avio.h
INSTALL libavformat/version.h
INSTALL libavformat/libavformat.pc
INSTALL libavcodec/avcodec.h
INSTALL libavcodec/avfft.h
INSTALL libavcodec/dxva2.h
INSTALL libavcodec/old_codec_ids.h
INSTALL libavcodec/vaapi.h
INSTALL libavcodec/vda.h
INSTALL libavcodec/vdpau.h
INSTALL libavcodec/version.h
INSTALL libavcodec/xvmc.h
INSTALL libavcodec/libavcodec.pc
INSTALL libswresample/swresample.h
INSTALL libswresample/version.h
INSTALL libswresample/libswresample.pc
INSTALL libswscale/swscale.h
INSTALL libswscale/version.h
INSTALL libswscale/libswscale.pc
INSTALL libavutil/adler32.h
INSTALL libavutil/aes.h
INSTALL libavutil/attributes.h
INSTALL libavutil/audio_fifo.h
INSTALL libavutil/audioconvert.h
INSTALL libavutil/avassert.h
INSTALL libavutil/avstring.h
INSTALL libavutil/avutil.h
INSTALL libavutil/base64.h
INSTALL libavutil/blowfish.h
INSTALL libavutil/bprint.h
INSTALL libavutil/bswap.h
INSTALL libavutil/buffer.h
INSTALL libavutil/channel_layout.h
INSTALL libavutil/common.h
INSTALL libavutil/cpu.h
INSTALL libavutil/crc.h
INSTALL libavutil/error.h
INSTALL libavutil/eval.h
INSTALL libavutil/fifo.h
INSTALL libavutil/file.h
INSTALL libavutil/frame.h
INSTALL libavutil/hmac.h
INSTALL libavutil/imgutils.h
INSTALL libavutil/intfloat.h
INSTALL libavutil/intfloat_readwrite.h
INSTALL libavutil/intreadwrite.h
INSTALL libavutil/lfg.h
INSTALL libavutil/log.h
INSTALL libavutil/mathematics.h
INSTALL libavutil/md5.h
INSTALL libavutil/mem.h
INSTALL libavutil/murmur3.h
INSTALL libavutil/dict.h
INSTALL libavutil/old_pix_fmts.h
INSTALL libavutil/opt.h
INSTALL libavutil/parseutils.h
INSTALL libavutil/pixdesc.h
INSTALL libavutil/pixfmt.h
INSTALL libavutil/random_seed.h
INSTALL libavutil/rational.h
INSTALL libavutil/ripemd.h
INSTALL libavutil/samplefmt.h
INSTALL libavutil/sha.h
INSTALL libavutil/sha512.h
INSTALL libavutil/time.h
INSTALL libavutil/timecode.h
INSTALL libavutil/timestamp.h
INSTALL libavutil/version.h
INSTALL libavutil/xtea.h
INSTALL libavutil/lzo.h
INSTALL libavutil/avconfig.h
INSTALL libavutil/libavutil.pc
link ffmpeg.
二、新建一個Android工程,在工程目錄下新建一個jni文件夾,在文件夾下新建一個ffmpeg文件夾,用來放ffmpeg相關的頭文件。在ffmpeg文件夾下新建Android.mk文件用來預先載入ffmpeg動態庫。Android.mk文件內容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := /path/to/build/output/libffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)
三、在jni下新建Android.mk文件和Application.mk兩個文件用來指定編譯的順序和編譯的平台以及對應的cpu指令集。
Application.mk
APP_ABI := armeabi
APP_PLATFORM := android-9
Android.mk
include $(call all-subdir-makefiles)
四、編寫JNI文件,用來綁定java文件與.c文件的交互,文件內容如下:
/*
* ffmpeg_jni.c
*
* Created on: Sep 1, 2014
* Author: clarck
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <jni.h>
#include "../include/ffmpeg_logger.h"
#include "../include/ffmpeg.h"
// 指定要注冊的類,對應完整的java類名
#define JNIREG_CLASS "com/clarck/android/ffmpeg/MainActivity"
JNIEXPORT void JNICALL native_setDataSource(JNIEnv *env, jclass classzz, jstring path) {
char *filepath = ffmpeg_jstringTostr(env, path);
ffmpeg_setDataSource(filepath);
}
//Java和JNI函數的綁定
static JNINativeMethod method_table[] = {
{ "setDataSource", "(Ljava/lang/String;)V", native_setDataSource }
};
//注冊native方法到java中
static int registerNativeMethods(JNIEnv *env, const char *className,
JNINativeMethod *gMethods, int numMethods) {
jclass clazz;
clazz = (*env)->FindClass(env, className);
if (clazz == NULL) {
return JNI_FALSE;
}
if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {
return JNI_FALSE;
}
return JNI_TRUE;
}
//調用注冊方法
int register_ndk_load(JNIEnv *env) {
return registerNativeMethods(env, JNIREG_CLASS, method_table,
(int) (sizeof(method_table) / sizeof(method_table[0])));
}
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env = NULL;
jint result = -1;
if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
return result;
}
register_ndk_load(env);
//返回JNI的版本
return JNI_VERSION_1_6;
}
五、編寫ffmpeg調用函數,內容如下:
/*
* ffmpeg.c
*
* Created on: Sep 1, 2014
* Author: clarck
*/
#include <jni.h>
#include <android/native_window_jni.h>
#include "../include/ffmpeg.h"
#include "../include/ffmpeg_logger.h"
#include "../ffmpeg/include/libavcodec/avcodec.h"
#include "../ffmpeg/include/libavformat/avformat.h"
#include "../ffmpeg/include/libavutil/pixfmt.h"
#include "../ffmpeg/include/libswscale/swscale.h"
char* ffmpeg_jstringTostr(JNIEnv* env, jstring jstr) {
char* pStr = NULL;
jclass jstrObj = (*env)->FindClass(env, "java/lang/String");
jstring encode = (*env)->NewStringUTF(env, "utf-8");
jmethodID methodId = (*env)->GetMethodID(env, jstrObj, "getBytes",
"(Ljava/lang/String;)[B");
jbyteArray byteArray = (jbyteArray) (*env)->CallObjectMethod(env, jstr,
methodId, encode);
jsize strLen = (*env)->GetArrayLength(env, byteArray);
jbyte *jBuf = (*env)->GetByteArrayElements(env, byteArray, JNI_FALSE);
if (jBuf > 0) {
pStr = (char*) malloc(strLen + 1);
if (!pStr) {
return NULL ;
}
memcpy(pStr, jBuf, strLen);
pStr[strLen] = 0;
}
(*env)->ReleaseByteArrayElements(env, byteArray, jBuf, 0);
return pStr;
}
void ffmpeg_setDataSource(char *file_path) {
LOGI("ffmpeg_setDataSource:%s", file_path);
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameYUV;
AVPacket *packet;
uint8_t *out_buffer;
static struct SwsContext *img_convert_ctx;
int videoStream, i, numBytes;
int ret, got_picture;
av_register_all();
pFormatCtx = avformat_alloc_context();
if (avformat_open_input(&pFormatCtx, file_path, NULL, NULL) != 0) {
LOGE("can『t open the file. \n");
return;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
LOGE("Could『t find stream infomation.\n");
return;
}
videoStream = 1;
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
}
}
if (videoStream == -1) {
LOGE("Didn『t find a video stream.\n");
return;
}
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
LOGE("Codec not found.\n");
return;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
LOGE("Could not open codec.\n");
return;
}
pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();
numBytes = avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width,
pCodecCtx->height);
out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,
pCodecCtx->width, pCodecCtx->height);
int y_size = pCodecCtx->width * pCodecCtx->height;
packet = (AVPacket *) malloc(sizeof(AVPacket));
av_new_packet(packet, y_size);
av_mp_format(pFormatCtx, 0, file_path, 0);
while (av_read_frame(pFormatCtx, packet) >= 0) {
if (packet->stream_index == videoStream) {
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,
packet);
LOGI("avcodec_decode_video2 ret:%d", ret);
if (ret < 0) {
LOGE("decode error.\n");
return;
}
if (got_picture) {
//TODO 此處可以將解碼出來的圖片保存起來。
}
}
av_free_packet(packet);
}
av_free(out_buffer);
av_free(pFrameYUV);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
}
六、編寫Android.mk用來編譯相關的.c文件,內容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
FFMPEG_PATH := ../ffmpeg
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(FFMPEG_PATH)/include
LOCAL_MODULE := ffmpeg_player
LOCAL_SRC_FILES += ffmpeg_jni.c
LOCAL_SRC_FILES += ffmpeg.c
LOCAL_SHARED_LIBRARIES := ffmpeg
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
七、編寫java文件中相關執行調用方法
package com.clarck.android.ffmpeg;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setDataSource("/sdcard/a.mp4");
}
public native void setDataSource(String path);
static {
System.loadLibrary("ffmpeg");
System.loadLibrary("ffmpeg_player");
}
}
⑷ 如何在Android上集成ffmpeg
下面把具體編譯步驟描述如下,假定NDK安裝在~/android-ndk-r7:
1. 首先從FFmpeg官網下載最新的release版本源碼ffmpeg-0.11.tar.gz解壓縮到Android源碼樹的ffmpeg/下。
2 准備一個編譯腳本build_android.sh並放在ffmpeg/下面,這個腳本也是Rockplayer提供的,需做一些修改,其內容附在後面。我目前用的也會附在後面。
3 在ffmpeg目錄下運行./build_android.sh開始編譯FFmpeg,編譯好的libffmpeg.so會放在文件夾android裡面,一共有3個版本分別對應3種ARM體系結構,包括armv7-a、armv7-a-vfp、armv6_vfp,根據所運行的硬體平台選取其中一個版本。為了編譯使用FFmpeg的程序時可以方便地找到libffmpeg.so,可將它復制到$OUT/system/lib/和$OUT/obj/lib/,當然這一步也可以加在build_android.sh中做。
4. 接下來就是編譯可執行文件ffmpeg了,這個工具可以在命令行下完成FFmpeg提供的幾乎所有功能包括編碼、解碼、轉碼等,也是用來調試和驗證很有用的工具。其實上述編譯完後在$ANDROID_BUILD_TOP/external/ffmpeg/下也會生成ffmpeg,但是在設備上無法運行。為了編出能在設備上運行的ffmpeg,可以寫一個簡單的Android.mk,
⑸ 如何在Android上集成ffmpeg
下面把具體編譯步驟描述如下,假定NDK安裝在~/android-ndk-r7:
1. 首先從FFmpeg官網下載最新的release版本源碼ffmpeg-0.11.tar.gz解壓縮到Android源碼樹的ffmpeg/下。
2 准備一個編譯腳本build_android.sh並放在ffmpeg/下面,這個腳本也是Rockplayer提供的,需做一些修改,其內容附在後面。咱目前用的也會附在後面。
3 在ffmpeg目錄下運行./build_android.sh開始編譯FFmpeg,編譯好的libffmpeg.so會放在文件夾android裡面,一共有3個版本分別對應3種ARM體系結構,包括armv7-a、armv7-a-vfp、armv6_vfp,根據所運行的硬體平台選取其中一個版本。為了編譯使用FFmpeg的程序時可以方便地找到libffmpeg.so,可將它復制到$OUT/system/lib/和$OUT/obj/lib/,當然這一步也可以加在build_android.sh中做。
4. 接下來就是編譯可執行文件ffmpeg了,這個工具可以在命令行下完成FFmpeg提供的幾乎所有功能包括編碼、解碼、轉碼等,也是用來調試和驗證很有用的工具。其實上述編譯完後在$ANDROID_BUILD_TOP/external/ffmpeg/下也會生成ffmpeg,但是在設備上無法運行。為了編出能在設備上運行的ffmpeg,可以寫一個簡單的Android.mk,
⑹ 如何在Android上集成ffmpeg
下面把具體編譯步驟描述如下,假定NDK安裝在~/android-ndk-r7:
1. 首先從FFmpeg官網下載最新的release版本源碼ffmpeg-0.11.tar.gz解壓縮到Android源碼樹的ffmpeg/下。
2 准備一個編譯腳本build_android.sh並放在ffmpeg/下面,這個腳本也是Rockplayer提供的,需做一些修改,其內容附在後面。目前用的也會附在後面。
3 在ffmpeg目錄下運行./build_android.sh開始編譯FFmpeg,編譯好的libffmpeg.so會放在文件夾android裡面,一共有3個版本分別對應3種ARM體系結構,包括armv7-a、armv7-a-vfp、armv6_vfp,根據所運行的硬體平台選取其中一個版本。為了編譯使用FFmpeg的程序時可以方便地找到libffmpeg.so,可將它復制到$OUT/system/lib/和$OUT/obj/lib/,當然這一步也可以加在build_android.sh中做。
4. 接下來就是編譯可執行文件ffmpeg了,這個工具可以在命令行下完成FFmpeg提供的幾乎所有功能包括編碼、解碼、轉碼等,也是用來調試和驗證很有用的工具。其實上述編譯完後在$ANDROID_BUILD_TOP/external/ffmpeg/下也會生成ffmpeg,但是在設備上無法運行。為了編出能在設備上運行的ffmpeg,可以寫一個簡單的Android.mk,
⑺ android 中使用ffmpeg,將視頻加入字幕,用ass文件,在PC上可以,但在Android上一直不行
理論上來講:任何視頻格式都是支持外掛字幕的 迅雷看看也是支持外掛字幕的 沒顯示字幕的可能性有兩個: 1、播放器不支持外掛字幕(顯然應該不是播放器的問題,有可能你關閉了外掛字幕功能,你可以試著重新安裝迅雷看看) 2、視頻文件名跟字幕文件名不同(在同一個文件夾下要播放器自動載入外掛字幕,首先必須保證視頻跟字幕在同一個文件夾內,然後要保證文件名稱一樣,這里說的文件名稱不包括後綴名 舉個例子: 視頻文件名為:ZXCV.mp4 ,,這里我們可以看到這個視頻是一個名稱為:ZXCV的MP4格式的視頻文件,ZXCV為為視頻名稱,「.mp4」則為文件的後綴名,即為文件種類 要保證外掛字幕文件順利的自動載入,那麼它的文件名就必須為:「ZXCV.ass」) 明白?
⑻ 如何在Android用FFmpeg解碼圖像
創建一個VideoPicture結構體用來保存解碼出來的圖像。
LOCAL_PATH := $(call my-dir)
###########################
#
# SDL shared library
#
###########################
include $(CLEAR_VARS)
LOCAL_MODULE := SDL2
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_SRC_FILES := \
$(subst $(LOCAL_PATH)/,, \
$(wildcard $(LOCAL_PATH)/src/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/mmy/*.c) \
$(LOCAL_PATH)/src/atomic/SDL_atomic.c \
$(LOCAL_PATH)/src/atomic/SDL_spinlock.c.arm \
$(wildcard $(LOCAL_PATH)/src/core/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) \
$(wildcard $(LOCAL_PATH)/src/dynapi/*.c) \
$(wildcard $(LOCAL_PATH)/src/events/*.c) \
$(wildcard $(LOCAL_PATH)/src/file/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/mmy/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/loadso/dlopen/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/filesystem/mmy/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*/*.c) \
$(wildcard $(LOCAL_PATH)/src/stdlib/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/pthread/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/unix/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/test/*.c))
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
include $(BUILD_SHARED_LIBRARY)
###########################
#
# SDL static library
#
###########################
#LOCAL_MODULE := SDL2_static
#LOCAL_MODULE_FILENAME := libSDL2
#LOCAL_SRC_FILES += $(LOCAL_PATH)/src/main/android/SDL_android_main.c
#LOCAL_LDLIBS :=
#LOCAL_EXPORT_LDLIBS := -Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
#include $(BUILD_STATIC_LIBRARY)
二、參考[原]如何在Android用FFmpeg解碼圖像, 在工程中新建一個ffmpeg文件夾,將與ffmpeg相關的頭文件include進來。ffmpeg文件夾下的Android.mk內容:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := /path/to/build/ffmpeg/libffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)
三、新建player文件夾,用來編寫解碼與顯示文件。player.c文件內容:
/*
* SDL_Lesson.c
*
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include <android/native_window_jni.h>
#include "SDL.h"
#include "SDL_thread.h"
#include "SDL_events.h"
#include "../include/logger.h"
#include "../ffmpeg/include/libavcodec/avcodec.h"
#include "../ffmpeg/include/libavformat/avformat.h"
#include "../ffmpeg/include/libavutil/pixfmt.h"
#include "../ffmpeg/include/libswscale/swscale.h"
int main(int argc, char *argv[]) {
char *file_path = argv[1];
LOGI("file_path:%s", file_path);
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameYUV;
AVPacket *packet;
uint8_t *out_buffer;
SDL_Texture *bmp = NULL;
SDL_Window *screen = NULL;
SDL_Rect rect;
SDL_Event event;
static struct SwsContext *img_convert_ctx;
int videoStream, i, numBytes;
int ret, got_picture;
av_register_all();
pFormatCtx = avformat_alloc_context();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
LOGE("Could not initialize SDL - %s. \n", SDL_GetError());
exit(1);
}
if (avformat_open_input(&pFormatCtx, file_path, NULL, NULL) != 0) {
LOGE("can't open the file. \n");
return -1;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
LOGE("Could't find stream infomation.\n");
return -1;
}
videoStream = 1;
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
}
}
LOGI("videoStream:%d", videoStream);
if (videoStream == -1) {
LOGE("Didn't find a video stream.\n");
return -1;
}
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
LOGE("Codec not found.\n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
LOGE("Could not open codec.\n");
return -1;
}
pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();
//---------------------------init sdl---------------------------//
screen = SDL_CreateWindow("My Player Window", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, pCodecCtx->width, pCodecCtx->height,
SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);
SDL_Renderer *renderer = SDL_CreateRenderer(screen, -1, 0);
bmp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);
//-------------------------------------------------------------//
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
numBytes = avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width,
pCodecCtx->height);
out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,
pCodecCtx->width, pCodecCtx->height);
rect.x = 0;
rect.y = 0;
rect.w = pCodecCtx->width;
rect.h = pCodecCtx->height;
int y_size = pCodecCtx->width * pCodecCtx->height;
packet = (AVPacket *) malloc(sizeof(AVPacket));
av_new_packet(packet, y_size);
av_mp_format(pFormatCtx, 0, file_path, 0);
while (av_read_frame(pFormatCtx, packet) >= 0) {
if (packet->stream_index == videoStream) {
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,
packet);
if (ret < 0) {
LOGE("decode error.\n");
return -1;
}
LOGI("got_picture:%d", got_picture);
if (got_picture) {
sws_scale(img_convert_ctx,
(uint8_t const * const *) pFrame->data,
pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data,
pFrameYUV->linesize);
////iPitch 計算yuv一行數據占的位元組數
SDL_UpdateTexture(bmp, &rect, pFrameYUV->data[0], pFrameYUV->linesize[0]);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bmp, &rect, &rect);
SDL_RenderPresent(renderer);
}
SDL_Delay(50);
}
av_free_packet(packet);
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
SDL_Quit();
exit(0);
break;
default:
break;
}
⑼ 如何運行從Android的命令行的ffmpeg
在前面編譯FFmpeg類庫編譯ffmpeg時,編譯腳本中使用了參數 –disable-ffmpeg,所以是不會生成ffmpeg工具的,即使生成了,
在Android應用下也無法直接使用。
但是,FFmpeg 命令行是十分強大易用的,我們可以用命令行完成大部分的需求,So,Let』s Do it.
http://blog.csdn.net/chengkaizone/article/details/52984091