将各种格式flv,avi,mp4等的文件,转码成MP4(无音频)

news/2025/2/14 7:21:15/

需要的配置请参考:http://blog.csdn.net/geolo/article/details/48439747

/** test14.cpp** 将各种格式flv,mp4的文件,转码成MP4*/#include <string.h>
#include <math.h>
#include "test06.h"#define __STDC_CONSTANT_MACROSint flush_encoder_14(AVFormatContext *fmt_ctx,unsigned int stream_index){int ret;int got_frame;AVPacket enc_pkt;if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &CODEC_CAP_DELAY))return 0;while (1) {enc_pkt.data = NULL;enc_pkt.size = 0;av_init_packet(&enc_pkt);ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,NULL, &got_frame);av_frame_free(NULL);if (ret < 0)break;if (!got_frame){ret=0;break;}LOGE("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);/* mux encoded frame */ret = av_write_frame(fmt_ctx, &enc_pkt);if (ret < 0)break;}return ret;
}int Test14(){LOGE("Test06 start \n");AVFormatContext *mInFormatCtx;AVCodecContext  *mInCodecCtx;AVCodec         *mInCodec;AVStream        *mInStream;AVFormatContext* pFormatCtx;AVOutputFormat* fmt;AVStream* video_st;AVCodecContext* pCodecCtx;AVCodec* pCodec;AVPacket pkt;uint8_t* picture_buf;AVFrame* pFrame;int picture_size;int y_size;int framecnt = 0;char *mVideoFileName = "/sdcard/22.flv";//FILE *in_file = fopen("src01_480x272.yuv", "rb"); //Input raw YUV dataFILE *in_file = fopen(mVideoFileName, "rb");   //Input raw YUV dataint in_w=480,in_h=272;                              //Input data's width and heightint framenum = 1000;                                   //Frames to encode//const char* out_file = "src01.h264";              //Output Filepath//const char* out_file = "src01.ts";//const char* out_file = "src01.hevc";const char* out_file = "/sdcard/test14.mp4";LOGE("Test06 run 01 \n");av_register_all();avcodec_register_all();avformat_network_init();//Method1.pFormatCtx = avformat_alloc_context();//Guess Formatfmt = av_guess_format(NULL, out_file, NULL);pFormatCtx->oformat = fmt;LOGE("Test06 run 02 \n");//Method 2.//avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);//fmt = pFormatCtx->oformat;AVCodec *_avcodecV = avcodec_find_encoder(AV_CODEC_ID_H264);video_st = avformat_new_stream(pFormatCtx, _avcodecV);//video_st->time_base.num = 1;//video_st->time_base.den = 25;LOGE("Test06 run 03 \n");if (video_st==NULL){LOGE("video_st==NULL \n");return -1;}//Param that must setpCodecCtx = video_st->codec;pCodecCtx->flags   |= CODEC_FLAG_QSCALE;pCodecCtx->qmin          = 0  ; //0是无损压缩。pCodecCtx->qmax          = 22 ; //20-30是比较合适的范围。pCodecCtx->bit_rate      = 0;pCodecCtx->gop_size      = 12;pCodecCtx->width         = in_w;pCodecCtx->height        = in_h;pCodecCtx->time_base.num = 1;//分子pCodecCtx->time_base.den = 25;//分母pCodecCtx->pix_fmt       = AV_PIX_FMT_YUV420P;pCodecCtx->profile       = FF_PROFILE_H264_BASELINE ;pCodecCtx->max_b_frames  = 0;pCodecCtx->codec_id      = fmt->video_codec;pCodecCtx->codec_type    = AVMEDIA_TYPE_VIDEO;//H264//  pCodecCtx->me_range = 16;//  pCodecCtx->max_qdiff = 4;//  pCodecCtx->qcompress = 0.6;//Optional Paramif (fmt->flags & AVFMT_GLOBALHEADER){pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;}// Set OptionAVDictionary *param = 0;//H.264if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {av_dict_set(&param, "preset", "slow", 0);av_dict_set(&param, "tune", "zerolatency", 0);//av_dict_set(&param, "profile", "main", 0);}//H.265if(pCodecCtx->codec_id == AV_CODEC_ID_H265){av_dict_set(&param, "preset", "ultrafast", 0);av_dict_set(&param, "tune", "zero-latency", 0);}//Show some Informationav_dump_format(pFormatCtx, 0, out_file, 1);LOGE("Test06 run 04 \n");pCodec = avcodec_find_encoder(pCodecCtx->codec_id);if (!pCodec){LOGE("Can not find encoder! \n");return -1;}if (avcodec_open2(pCodecCtx, pCodec,&param) < 0){LOGE("Failed to open encoder! \n");return -1;}pFrame = av_frame_alloc();picture_size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);picture_buf = (uint8_t *)av_malloc(picture_size);avpicture_fill((AVPicture *)pFrame, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);//Open output URLif (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0){LOGE("Failed to open output file! \n");return -1;}//Write File Headeravformat_write_header(pFormatCtx,NULL);av_new_packet(&pkt,picture_size);y_size = pCodecCtx->width * pCodecCtx->height;LOGE("Test06 run 05 \n");//   解码部分  //mInFormatCtx = avformat_alloc_context();if(avformat_open_input(&mInFormatCtx, mVideoFileName, NULL, NULL)!=0){return -1;}if(avformat_find_stream_info(mInFormatCtx, NULL)<0){return -1;}av_dump_format(mInFormatCtx, 0, mVideoFileName, 0);LOGE("Test06 run 06 \n");int                  videoStream;videoStream = -1;videoStream = av_find_best_stream(mInFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &mInCodec, 0);if(mInCodec == NULL) {LOGE( "Unsupported pCodec!\n");return -1; // Codec not found}mInCodecCtx = mInFormatCtx->streams[videoStream]->codec;AVDictionary    *optionsDict = NULL;if(avcodec_open2(mInCodecCtx, mInCodec, &optionsDict)<0){LOGE("---- 不能打开 视频 解码库 ---");return -1;}LOGE("videoStream == %d \n", videoStream);struct SwsContext    *sws_ctx = NULL;sws_ctx = sws_getContext(mInCodecCtx->width,mInCodecCtx->height,mInCodecCtx->pix_fmt,in_w,in_h,AV_PIX_FMT_YUV420P,SWS_FAST_BILINEAR,NULL, NULL, NULL);AVFrame              *mInFrame;mInFrame = av_frame_alloc();if(mInFrame == NULL){LOGE("Failed to pFrameYUV==NULL ! \n");return -1;}AVFrame              *pFrameYUV;pFrameYUV = av_frame_alloc();int numBytes = avpicture_get_size(PIX_FMT_YUV420P, in_w, in_h);uint8_t* buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));avpicture_fill((AVPicture *) pFrameYUV, buffer, PIX_FMT_YUV420P,in_w, in_h);LOGE("start encoder! \n");//初始化SwsContextSwsContext *rgbToyuvcxt = sws_getContext(in_w,in_h,AV_PIX_FMT_RGB24,in_w,in_h,AV_PIX_FMT_YUV420P,SWS_POINT,NULL,NULL,NULL);AVFrame *m_pRGBFrame =  av_frame_alloc(); //RGB帧数据int rgbnumBytes = avpicture_get_size(AV_PIX_FMT_RGB24, in_w, in_h);///for (int i=0; i < framenum; i++){AVPacket                packet;if(av_read_frame(mInFormatCtx, &packet) >= 0 ){if (packet.stream_index == videoStream) {int completed;avcodec_decode_video2(mInCodecCtx, mInFrame, &completed,&packet);// Decode video framesws_scale(sws_ctx,(uint8_t const * const *)mInFrame->data,mInFrame->linesize,0,mInCodecCtx->height,pFrameYUV->data,pFrameYUV->linesize);pFrame->data[0] = pFrameYUV ->data[0]; // YpFrame->data[1] = pFrameYUV ->data[1]; // UpFrame->data[2] = pFrameYUV ->data[2]; // V/**///PTS//pFrame->pts = pFrameYUV-> pts;//在编码前设置好,结果的pkt的配置av_free_packet(&pkt);av_init_packet(&pkt);pkt.pts = framecnt;pkt.dts = pkt.pts ;//pkt.duration = 1;int got_picture=0;//Encodeint ret = avcodec_encode_video2(pCodecCtx, &pkt,pFrame, &got_picture);if(ret < 0){LOGE("Failed to encode! \n");return -1;}if (got_picture == 1){LOGE("Succeed to encode frame: %5d\t , size:%5d\n",framecnt,pkt.size);//pkt.flags |= AV_PKT_FLAG_KEY;pkt.stream_index = video_st->index;framecnt++;ret = av_interleaved_write_frame(pFormatCtx, &pkt);av_free_packet(&pkt);}}}}//Flush Encoderint ret = flush_encoder_14(pFormatCtx,0);if (ret < 0) {LOGE("Flushing encoder failed\n");return -1;}//Write file trailerav_write_trailer(pFormatCtx);//Cleanif (video_st){avcodec_close(video_st->codec);av_free(pFrame);av_free(picture_buf);}avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);fclose(in_file);return 0;
}

http://www.ppmy.cn/news/264980.html

相关文章

MP4文件格式入门

什么是MP4 首先&#xff0c;介绍下封装格式。多媒体封装格式&#xff08;也叫容器格式&#xff09;&#xff0c;是指按照一定的规则&#xff0c;将视频数据、音频数据等&#xff0c;放到一个文件中。常见的 MKV、AVI 以及本文介绍的 MP4 等&#xff0c;都是封装格式。 MP4是最…

MP4视频格式解析

一、MP4格式介绍 参考&#xff1a;https://zhuanlan.zhihu.com/p/457888765 参考&#xff1a;https://blog.51cto.com/u_13861442/5169957 mp4或称MPEG-4 Part 14&#xff0c;是一种多媒体容器格式&#xff0c;扩展名为.mp4。 MP4 文件格式又被称为 MPEG-4 Part 14&#xf…

下载腾讯视频(mp4 格式)

下载腾讯视频&#xff08;mp4 格式&#xff09; 问题描述&#xff1a; 离线腾讯视频是 qlv 格式的&#xff0c;只能使用腾讯视频软件打开。想想办法&#xff0c;能不能将 qlv 格式转换成 mp4 格式的视频。 百度&#xff0c;Google 了半天&#xff0c;找了一堆下载器&#xf…

HTML5移动端视频播放器MP4

1.前言&#xff1a;移动端可以播放MP4文件格式&#xff0c;HTML5的video标签实现了播放器。但是在移动端上初始化显示界面不好看&#xff0c;所以&#xff0c;这里将video包装起来&#xff0c;界面至少不丑。 <!doctype html> <html lang"en"><head&…

m3u8格式转成MP4以及可播放格式

前文:已经好几个月没有写博客了&#xff0c;因为在忙于复习高等数学。今天看数学视频的时候想把视屏分享给小伙伴&#xff0c;但是当我将视屏发给我的小伙伴的时候发现原本200多MB的视屏传给他只有几K大小&#xff0c;这有点像我们小时候从电脑上拷贝游戏都只是拷贝一个快捷方式…

Node服务器 - koa框架

1 koa的基本使用 2 koa的参数解析 3 koa响应和错误 4 koa静态服务器 5 koa的源码解析 6 和express对比 koa的基本使用过程 const Koa require(koa)// 创建app对象 const app new Koa()// 注册中间件(middleware) // koa的中间件有两个参数: ctx/next app.use((ctx, next…

使用TypeScript实现一个浏览器事件的集中管理

使用TypeScript实现一个浏览器事件的集中管理 1. 浏览器事件模型2. EventTarget接口3.使用TypeScript实现浏览器事件的集中管理 1. 浏览器事件模型 浏览器的事件模型是一种基于事件驱动的编程模型&#xff0c;用于处理用户与浏览器交互时触发的各种事件。它包括三个主要阶段&a…

【云原生网关】Apache ShenYu 使用详解

目录 一、前言 二、Apache ShenYu 介绍 2.1 为什么叫ShenYu 2.2 ShenYu特点 2.3 ShenYu架构图 2.4 shenyu数据同步原理 2.4.1 Zookeeper数据同步原理 三、Apache ShenYu 安装部署 3.1 部署流程 3.1.1 创建 Docker Network 3.1.2 拉取Apache ShenYu Admin镜像 3.1.3…