Anonymous
Видео не сжато ffmpeg.
Сообщение
Anonymous » 08 окт 2024, 12:51
Я написал обертку для JNI на языке cpp для сжатия видео. Но сжатия не произошло, и выходное видео точно такое же, как входное.
Я хочу запустить команду ниже:
ffmpeg -i input.mp4 -vcodec libx264 -crf 30 -movflags +faststart output.mp4
Код: Выделить всё
#include
#include
#include
extern "C" {
#include
#include
#include
#include
#include
#include
}
using namespace std;
#define LOG_TAG "VideoNative"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define FFMPEG_LOG_TAG "FFMPEG_VideoNative"
#define FLOGD(...) __android_log_print(ANDROID_LOG_DEBUG, FFMPEG_LOG_TAG, __VA_ARGS__)
#define FLOGI(...) __android_log_print(ANDROID_LOG_INFO, FFMPEG_LOG_TAG, __VA_ARGS__)
#define FLOGE(...) __android_log_print(ANDROID_LOG_ERROR, FFMPEG_LOG_TAG, __VA_ARGS__)
AVFormatContext *initInputFormatContext(const char *path);
AVFormatContext *initOutputFormatContext(const char *path);
void copyAvFormatContext(AVFormatContext **inputContext, AVFormatContext **outputContext);
void writeFileHeader(AVFormatContext *formatContext);
static void ffmpeg_log_callback(void *ptr, int level, const char *fmt, va_list vargs) {
if (level pb, outputPath, AVIO_FLAG_WRITE, nullptr, nullptr) < 0) {
LOGE("error to open file");
}
writeFileHeader(outputFormatContext);
AVPacket *inputPaket = av_packet_alloc();
if (!inputPaket) {
LOGE("packet not find");
}
while (av_read_frame(inputFormatContext, inputPaket) >= 0) {
if (av_interleaved_write_frame(outputFormatContext, inputPaket) < 0) {
LOGE("can not write frame");
}
}
if (av_write_trailer(outputFormatContext) < 0) {
LOGE("can not write trailer");
}
}
void writeFileHeader(AVFormatContext *formatContext) {
AVDictionary *option = nullptr;
if (avformat_write_header(formatContext, &option) < 0) {
LOGE("write header fail");
return;
}
av_dict_free(&option);
}
void copyAvFormatContext(AVFormatContext **inputContext, AVFormatContext **outputContext) {
int numStream = static_cast((*inputContext)->nb_streams);
for (int i = 0; i < numStream; i++) {
AVStream *inputStream = (*inputContext)->streams[i];
AVCodecParameters *inputCodecParams = inputStream->codecpar;
AVCodec *outputCodec = avcodec_find_encoder(inputCodecParams->codec_id);
AVStream *outputStream = avformat_new_stream(*outputContext, outputCodec);
AVCodecContext *codecContext = avcodec_alloc_context3(outputCodec);
codecContext->codec_id = AV_CODEC_ID_H264;
av_opt_set(codecContext->priv_data, "crf", "30", 0);
av_opt_set(codecContext->priv_data, "movflags", "faststart", 0);
if (avcodec_parameters_from_context(outputStream->codecpar, codecContext) < 0) {
LOGE("cant set option" );
}
AVCodecParameters *outputCodecParams = outputStream->codecpar;
avcodec_parameters_copy(outputCodecParams, inputCodecParams);
}
}
AVFormatContext *initOutputFormatContext(const char *path) {
AVFormatContext *formatContext = avformat_alloc_context();
if (avformat_alloc_output_context2(&formatContext, nullptr, "mp4", path) < 0) {
LOGE("open output fail");
return nullptr;
}
return formatContext;
}
AVFormatContext *initInputFormatContext(const char *path) {
AVFormatContext *formatContext = avformat_alloc_context();
if (avformat_open_input(&formatContext, path, nullptr, nullptr) != 0) {
LOGE("open input fail");
return nullptr;
}
if (avformat_find_stream_info(formatContext, nullptr) < 0) {
LOGE("stream fail");
return nullptr;
}
return formatContext;
}
extern "C"
JNIEXPORT jint JNICALL
Java_ir_nasim_core_modules_file_FileNativeLoader_compressFile(JNIEnv *env, jobject thiz,
jstring input_path,
jstring output_path) {
compress(env->GetStringUTFChars(input_path, JNI_FALSE),
env->GetStringUTFChars(output_path, JNI_FALSE));
return 0;
}
Я попробовал примеры кодов, но ни один из них не сработал.
Я был бы признателен, если бы вы помогли мне решить эту проблему.>
Подробнее здесь:
https://stackoverflow.com/questions/790 ... ith-ffmpeg
1728381086
Anonymous
Я написал обертку для JNI на языке cpp для сжатия видео. Но сжатия не произошло, и выходное видео точно такое же, как входное. Я хочу запустить команду ниже: ffmpeg -i input.mp4 -vcodec libx264 -crf 30 -movflags +faststart output.mp4 [code]#include #include #include extern "C" { #include #include #include #include #include #include } using namespace std; #define LOG_TAG "VideoNative" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) #define FFMPEG_LOG_TAG "FFMPEG_VideoNative" #define FLOGD(...) __android_log_print(ANDROID_LOG_DEBUG, FFMPEG_LOG_TAG, __VA_ARGS__) #define FLOGI(...) __android_log_print(ANDROID_LOG_INFO, FFMPEG_LOG_TAG, __VA_ARGS__) #define FLOGE(...) __android_log_print(ANDROID_LOG_ERROR, FFMPEG_LOG_TAG, __VA_ARGS__) AVFormatContext *initInputFormatContext(const char *path); AVFormatContext *initOutputFormatContext(const char *path); void copyAvFormatContext(AVFormatContext **inputContext, AVFormatContext **outputContext); void writeFileHeader(AVFormatContext *formatContext); static void ffmpeg_log_callback(void *ptr, int level, const char *fmt, va_list vargs) { if (level pb, outputPath, AVIO_FLAG_WRITE, nullptr, nullptr) < 0) { LOGE("error to open file"); } writeFileHeader(outputFormatContext); AVPacket *inputPaket = av_packet_alloc(); if (!inputPaket) { LOGE("packet not find"); } while (av_read_frame(inputFormatContext, inputPaket) >= 0) { if (av_interleaved_write_frame(outputFormatContext, inputPaket) < 0) { LOGE("can not write frame"); } } if (av_write_trailer(outputFormatContext) < 0) { LOGE("can not write trailer"); } } void writeFileHeader(AVFormatContext *formatContext) { AVDictionary *option = nullptr; if (avformat_write_header(formatContext, &option) < 0) { LOGE("write header fail"); return; } av_dict_free(&option); } void copyAvFormatContext(AVFormatContext **inputContext, AVFormatContext **outputContext) { int numStream = static_cast((*inputContext)->nb_streams); for (int i = 0; i < numStream; i++) { AVStream *inputStream = (*inputContext)->streams[i]; AVCodecParameters *inputCodecParams = inputStream->codecpar; AVCodec *outputCodec = avcodec_find_encoder(inputCodecParams->codec_id); AVStream *outputStream = avformat_new_stream(*outputContext, outputCodec); AVCodecContext *codecContext = avcodec_alloc_context3(outputCodec); codecContext->codec_id = AV_CODEC_ID_H264; av_opt_set(codecContext->priv_data, "crf", "30", 0); av_opt_set(codecContext->priv_data, "movflags", "faststart", 0); if (avcodec_parameters_from_context(outputStream->codecpar, codecContext) < 0) { LOGE("cant set option" ); } AVCodecParameters *outputCodecParams = outputStream->codecpar; avcodec_parameters_copy(outputCodecParams, inputCodecParams); } } AVFormatContext *initOutputFormatContext(const char *path) { AVFormatContext *formatContext = avformat_alloc_context(); if (avformat_alloc_output_context2(&formatContext, nullptr, "mp4", path) < 0) { LOGE("open output fail"); return nullptr; } return formatContext; } AVFormatContext *initInputFormatContext(const char *path) { AVFormatContext *formatContext = avformat_alloc_context(); if (avformat_open_input(&formatContext, path, nullptr, nullptr) != 0) { LOGE("open input fail"); return nullptr; } if (avformat_find_stream_info(formatContext, nullptr) < 0) { LOGE("stream fail"); return nullptr; } return formatContext; } extern "C" JNIEXPORT jint JNICALL Java_ir_nasim_core_modules_file_FileNativeLoader_compressFile(JNIEnv *env, jobject thiz, jstring input_path, jstring output_path) { compress(env->GetStringUTFChars(input_path, JNI_FALSE), env->GetStringUTFChars(output_path, JNI_FALSE)); return 0; } [/code] Я попробовал примеры кодов, но ни один из них не сработал. Я был бы признателен, если бы вы помогли мне решить эту проблему.> Подробнее здесь: [url]https://stackoverflow.com/questions/79065378/video-is-not-compressed-with-ffmpeg[/url]