ffmpeg中将avi文件恢复,或者MP4转到avi
·
int nListCnt=0, ret;
ST_VIDEO_DB_DATA *pstVideoDbData;
SearchList.clear();
QDateTime qttime;
qttime= QDateTime::currentDateTime();
QString str = qttime.toString("yyyyMMdd_hhmmss");
char out_file[265] = {0};
strcpy(out_file,tr("/msata/video/1-%1.avi").arg(str).toUtf8().data());
printf("=====================out_file:%s\n",out_file);
if(flg == 0)
{
db_listAllVideoDbData(&nListCnt, &pstVideoDbData);
for(int i=0;i<nListCnt;i++)
{
if((access(pstVideoDbData[i].file_path,F_OK))==-1)
{
continue;
}
printf("===========================pstVideoDbData.file_path:%s\n",pstVideoDbData[i].file_path);
printf("===========================pstVideoDbData.stEndTime year:%d month:%d date:%d hour:%d minute:%d second:%d\n", \
pstVideoDbData[i].stEndTime.year, pstVideoDbData[i].stEndTime.month,pstVideoDbData[i].stEndTime.date,\
pstVideoDbData[i].stEndTime.hour,pstVideoDbData[i].stEndTime.minute,pstVideoDbData[i].stEndTime.second);
if(pstVideoDbData[i].stEndTime.year == 1969 && pstVideoDbData[i].stEndTime.month == 12 && \
pstVideoDbData[i].stEndTime.date == 31 && pstVideoDbData[i].stEndTime.hour == 23 \
&& pstVideoDbData[i].stEndTime.minute == 59 && pstVideoDbData[i].stEndTime.second == 59)
{
printf("===========================000\n");
AVFormatContext *ifmt_ctx = NULL;
AVFormatContext *ofmt_ctx = NULL;
int videoindex = -1;
int audioindex = -1;
int videoindex_out=0;
int audioindex_out=0;
int frame_index=0;
printf("===========================111\n");
int ret = avformat_open_input(&ifmt_ctx, pstVideoDbData[i].file_path, NULL, NULL);
printf("===========================111ret:%d\n",ret);
if (ret < 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf) - 1);
printf("===========================open %s failed:%s\n", pstVideoDbData[i].file_path, buf);
if(ifmt_ctx) {
avformat_close_input(&ifmt_ctx);
}
return;
}
printf("===========================222\n");
ret = avformat_find_stream_info(ifmt_ctx, NULL);
printf("===========================222ret:%d\n",ret);
if (ret < 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf) - 1);
printf("===========================avformat_find_stream_info %s failed:%s\n", pstVideoDbData[i].file_path, buf);
if(ifmt_ctx) {
avformat_close_input(&ifmt_ctx);
}
return;
}
printf("stream number:%d\n", ifmt_ctx->nb_streams);
printf("===========================333\n");
if(!ifmt_ctx) {
printf("========================fmt ctx is NULL\n");
return;
}
printf("===========================444\n");
ret = avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL,out_file);
printf("===========================444ret:%d\n",ret);
if(ret < 0) {
char errbuf[1024] = {0};
av_strerror(ret, errbuf, sizeof(errbuf) - 1);
printf("avformat_alloc_output_context2 failed:%s\n", errbuf);
return;
}
printf("===========================555\n");
for (uint32_t i = 0; i < ifmt_ctx->nb_streams; i++)//nb_streams码流编号(音频或视频)
{
AVStream *in_stream = ifmt_ctx->streams[i];// 音频流、视频流、字幕流
//如果是音频流,则打印音频的信息
if (AVMEDIA_TYPE_AUDIO == in_stream->codecpar->codec_type)
{
printf("===========================Audio 666\n");
AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
if (!out_stream) {
printf( "Failed allocating output stream\n");
ret = AVERROR_UNKNOWN;
}
printf("===========================Audio 777\n");
audioindex_out=out_stream->index;
//Copy the settings of AVCodecContext
if (avcodec_copy_context(out_stream->codec, in_stream->codec) < 0) {
printf( "Failed to copy context from input to output stream codec context\n");
}
printf("===========================Audio 888\n");
out_stream->codec->codec_tag = 0;
/*
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
*/
printf("----- Audio info:\n");
if (AV_CODEC_ID_AAC == in_stream->codecpar->codec_id)
{
printf("audio codec:AAC\n");
}
else if (AV_CODEC_ID_MP3 == in_stream->codecpar->codec_id)
{
printf("audio codec:MP3\n");
}
else
{
printf("audio codec_id:%d\n", in_stream->codecpar->codec_id);
}
if(in_stream->duration != AV_NOPTS_VALUE)
{
int duration_audio = (in_stream->duration) * av_q2d(in_stream->time_base);
//将音频总时长转换为时分秒的格式打印到控制台上
printf("audio duration: %02d:%02d:%02d\n",
duration_audio / 3600, (duration_audio % 3600) / 60, (duration_audio % 60));
}
else
{
printf("audio duration unknown");
}
printf("\n");
audioindex = i; // 获取音频的索引
}
else if (AVMEDIA_TYPE_VIDEO == in_stream->codecpar->codec_type) //如果是视频流,则打印视频的信息
{
printf("===========================video 666\n");
AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
if (!out_stream) {
printf( "Failed allocating output stream\n");
ret = AVERROR_UNKNOWN;
}
videoindex_out=out_stream->index;
//Copy the settings of AVCodecContext
printf("===========================video 777\n");
if (avcodec_copy_context(out_stream->codec, in_stream->codec) < 0) {
printf( "Failed to copy context from input to output stream codec context\n");
}
printf("===========================video 888\n");
out_stream->codec->codec_tag = 0;
/*
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
*/
//break;
printf("----- video info:\n");
printf("fps:%lffps\n", av_q2d(in_stream->avg_frame_rate));
if (AV_CODEC_ID_MPEG4 == in_stream->codecpar->codec_id) //视频压缩编码格式
{
printf("video codec:MPEG4\n");
}
else if (AV_CODEC_ID_H264 == in_stream->codecpar->codec_id) //视频压缩编码格式
{
printf("video codec:H264\n");
}
else
{
printf("video codec_id:%d\n", in_stream->codecpar->codec_id);
}
// 视频帧宽度和帧高度
printf("width:%d height:%d\n", in_stream->codecpar->width,
in_stream->codecpar->height);
//视频总时长,单位为秒。注意如果把单位放大为毫秒或者微妙,音频总时长跟视频总时长不一定相等的
if(in_stream->duration != AV_NOPTS_VALUE)
{
int duration_video = (in_stream->duration) * av_q2d(in_stream->time_base);
printf("video duration: %02d:%02d:%02d\n",
duration_video / 3600,
(duration_video % 3600) / 60,
(duration_video % 60)); //将视频总时长转换为时分秒的格式打印到控制台上
}
else
{
printf("video duration unknown");
}
printf("\n");
videoindex = i;
}
}
ret = avio_open(&ofmt_ctx->pb, out_file, AVIO_FLAG_WRITE);
printf("===========================555ret:%d\n",ret);
if(ret < 0) {
char errbuf[1024] = {0};
av_strerror(ret, errbuf, sizeof(errbuf) - 1);
printf("avio_open %s failed:%s\n",out_file, errbuf);
return;
}
printf("===========================666\n");
if(!ofmt_ctx) {
printf("fmt ctx is NULL\n");
return;
}
ret = avformat_write_header(ofmt_ctx, NULL);
printf("===========================666ret:%d\n",ret);
if(ret < 0) {
char errbuf[1024] = {0};
av_strerror(ret, errbuf, sizeof(errbuf) - 1);
printf("avformat_write_header failed:%s\n", errbuf);
return;
}
AVPacket pkt;
while (1)
{
AVStream *in_stream, *out_stream;
//Get an AVPacket
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
break;
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
//Convert PTS/DTS
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;
/*
if (pkt.stream_index == 0) {
AVPacket fpkt = pkt;
int a = av_bitstream_filter_filter(filter,
out_stream->codec, NULL, &fpkt.data, &fpkt.size,
pkt.data, pkt.size, pkt.flags & AV_PKT_FLAG_KEY);
pkt.data = fpkt.data;
pkt.size = fpkt.size;
}
*/
//Write
if (av_interleaved_write_frame(ofmt_ctx, &pkt) < 0) {//将AVPacket(存储视频压缩码流数据)写入文件
printf( "Error muxing packet\n");
break;
}
printf("Write %8d frames to output file\n",frame_index);
av_free_packet(&pkt);
frame_index++;
}
ret = av_write_trailer(ofmt_ctx);
printf("===========================ret:%d\n",ret);
if(ret != 0) {
char errbuf[1024] = {0};
av_strerror(ret, errbuf, sizeof(errbuf) - 1);
printf("================================av_write_trailer failed:%s\n", errbuf);
if(ofmt_ctx) {
avformat_close_input(&ofmt_ctx);
}
return;
}
printf("===========================");
}
SearchList.append(pstVideoDbData[i]);
}
db_releaseVideoDbData();
}
更多推荐


所有评论(0)