日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用ffmpeg将h264视频文件转Mp4格式保存

發(fā)布時間:2023/12/9 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用ffmpeg将h264视频文件转Mp4格式保存 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

其實具體的代碼ffmpeg的官方demo做的功能已經(jīng)很完善了,自己也就修修補補,下面直接上代碼

#include "stdafx.h" #include <iostream> #include <stdio.h> #include <tchar.h>//這里是個坑,不加extern "C",死活編譯不過 extern "C" {#include "libavformat/avformat.h" };using namespace std;int _tmain(int argc, _TCHAR* argv[]) {AVOutputFormat *ofmt = NULL;//創(chuàng)建輸入AVFormatContext對象和輸出AVFormatContext對象AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;AVPacket pkt;const char *in_filename;int ret, i;int stream_index = 0;int *stream_mapping = NULL;int stream_mapping_size = 0;in_filename = “C:\\test.h264”;//產(chǎn)生對應(yīng)的錄像文件名SYSTEMTIME systime;GetLocalTime(&systime);char cTime[128];sprintf(cTime,"%4d/%02d/%02d %02d:%02d:%02d.%03d\n",systime.wYear,systime.wMonth,systime.wDay,systime.wHour,systime.wMinute, systime.wSecond,systime.wMilliseconds);char out_filename[128];sprintf_s(out_filename,128,"%s.mp4",cTime);//打開視頻文件if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {//LogMessage::DebugLogInfo("ReCode", "打開視頻流失敗");return -1;}//獲取視頻文件信息if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {//LogMessage::DebugLogInfo("ReCode", "獲取視頻流信息失敗");return -1;}//打印信息//av_dump_format(ifmt_ctx, 0, in_filename, 0);//輸出文件分配空間avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);if (!ofmt_ctx) {//LogMessage::DebugLogInfo("ReCode", "輸出文件分配空間分配失敗");return -1;}stream_mapping_size = ifmt_ctx->nb_streams;stream_mapping = (int *)av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));if (!stream_mapping) {//LogMessage::DebugLogInfo("ReCode", "獲取mapping失敗");return -1;}ofmt = ofmt_ctx->oformat;for (i = 0; i < ifmt_ctx->nb_streams; i++) {AVStream *out_stream;AVStream *in_stream = ifmt_ctx->streams[i];AVCodecParameters *in_codecpar = in_stream->codecpar;if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {stream_mapping[i] = -1;continue;}stream_mapping[i] = stream_index++;out_stream = avformat_new_stream(ofmt_ctx, NULL);if (!out_stream) {//fprintf(stderr, "Failed allocating output stream\n");//LogMessage::DebugLogInfo("ReCode", "分配流對象失敗");return -1;}ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);if (ret < 0) {//LogMessage::DebugLogInfo("ReCode", "拷貝視頻code失敗");return -1;}out_stream->codecpar->codec_tag = 0;}//打開文件if (!(ofmt->flags & AVFMT_NOFILE)) {ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);if (ret < 0) {//LogMessage::DebugLogInfo("ReCode", "打開輸出文件失敗");return -1;}}//開始寫入文件頭ret = avformat_write_header(ofmt_ctx, NULL);if (ret < 0) {//LogMessage::DebugLogInfo("ReCode", "寫入文件頭失敗");return -1;}int m_frame_index = 0;//開始讀取視頻流,并獲取pkt信息while (1) {AVStream *in_stream, *out_stream;ret = av_read_frame(ifmt_ctx, &pkt);if (ret < 0)break;in_stream = ifmt_ctx->streams[pkt.stream_index];if (pkt.stream_index >= stream_mapping_size ||stream_mapping[pkt.stream_index] < 0) {av_packet_unref(&pkt);continue;}pkt.stream_index = stream_mapping[pkt.stream_index];out_stream = ofmt_ctx->streams[pkt.stream_index];//從攝像頭直接保存的h264文件,重新編碼時得自己加時間戳,不然轉(zhuǎn)換出來的是沒有時間的if(pkt.pts==AV_NOPTS_VALUE){//Write PTSAVRational time_base1=in_stream->time_base;//Duration between 2 frames (us)int64_t calc_duration=(double)AV_TIME_BASE/av_q2d(in_stream->r_frame_rate);//Parameterspkt.pts=(double)(m_frame_index*calc_duration)/(double)(av_q2d(time_base1)*AV_TIME_BASE);pkt.dts=pkt.pts;pkt.duration=(double)calc_duration/(double)(av_q2d(time_base1)*AV_TIME_BASE);}/* copy packet */pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);pkt.pos = -1;ret = av_interleaved_write_frame(ofmt_ctx, &pkt);if (ret < 0) {break;}av_packet_unref(&pkt);m_frame_index++;}av_write_trailer(ofmt_ctx);avformat_close_input(&ifmt_ctx);// close output if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))avio_closep(&ofmt_ctx->pb);avformat_free_context(ofmt_ctx);av_freep(&stream_mapping);return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的使用ffmpeg将h264视频文件转Mp4格式保存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。