本帖最后由 安静0923 于 2014-7-4 12:53 编辑
帮我看看代码写的有什么问题,封装的TS文件播放几秒之后就自动关闭了。
[C++] 纯文本查看 复制代码 int CMFCVideoView::CreateTSMuxer(CString lpszPath)
{
int ret; // Succeed = 0,Fail = 1
// const char* pszFileName = "output002.ts";
//CString to const char*
const size_t strsize=(lpszPath.GetLength()+1)*2;
char * pszFileName= new char[strsize];
size_t sz=0;
wcstombs_s(&sz,pszFileName,strsize,lpszPath,_TRUNCATE);
atoi((const char*)pszFileName);
AVOutputFormat *fmt;
AVCodec *video_codec;
AVStream *m_pVideoSt;
av_register_all();
avformat_alloc_output_context2(&m_pOc, NULL, NULL, pszFileName);
if (!m_pOc)
{
avformat_alloc_output_context2(&m_pOc, NULL, "mpeg", pszFileName);
}
if (!m_pOc)
{
return 1;
}
fmt = m_pOc->oformat;
if (fmt->video_codec != AV_CODEC_ID_NONE)
{
m_pVideoSt = AddStream(m_pOc, &video_codec, fmt->video_codec);
}
if (m_pVideoSt)
{
OpenVideo(m_pOc, video_codec, m_pVideoSt);
}
av_dump_format(m_pOc, 0, pszFileName, 1);
// open the output file, if needed
if (!(fmt->flags & AVFMT_NOFILE))
{
ret = avio_open(&m_pOc->pb, pszFileName, AVIO_FLAG_WRITE);
if (ret < 0)
{
CString str;
str.Format(_T("could not open %s: %s\n"), pszFileName);
MessageBox(str);
return 1;
}
}
// Write the stream header, if any
ret = avformat_write_header(m_pOc, NULL);
if (ret < 0)
{
MessageBox(_T("Error occurred when opening output file"));
return 1;
}
return 0;
}
[C++] 纯文本查看 复制代码 AVStream* CMFCVideoView::AddStream(AVFormatContext* oc, AVCodec** codec, enum AVCodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
// find the encoder
*codec = avcodec_find_encoder(codec_id);
if (!*codec)
{
CString str;
str.Format(_T("could not find encoder for %s"), avcodec_get_name(codec_id));
MessageBox(str);
return NULL;
}
st = avformat_new_stream(oc, *codec);
if (!st)
{
MessageBox(_T("could not allocate stream"));
return NULL;
}
st->id = oc->nb_streams-1;
c = st->codec;
vi = st->index;
switch ((*codec)->type)
{
case AVMEDIA_TYPE_AUDIO:
c->sample_fmt = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
c->bit_rate = 64000;
c->sample_rate = AUDIO_SAMPLESPERSEC;
c->channels = 2;
break;
case AVMEDIA_TYPE_VIDEO:
c->codec_id = codec_id;
c->bit_rate = 40000;
c->width = 1920;
c->height = 1080;
c->time_base.den = 25;
c->time_base.num = 1;
c->gop_size = 12;
c->pix_fmt = PIX_FMT_YUV420P;
if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO)
{
c->max_b_frames = 2;
}
if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO)
{
c->mb_decision = 2;
}
break;
default:
break;
}
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
{
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
return st;
}
[AppleScript] 纯文本查看 复制代码 void CMFCVideoView::OpenVideo(AVFormatContext* oc, AVCodec* codec, AVStream* st)
{
int ret;
AVCodecContext *c = st->codec;
// open the codec
ret = avcodec_open2(c, codec, NULL);
if (ret < 0)
{
MessageBox(_T("could not open video codec"));
}
} [C++] 纯文本查看 复制代码 void CMFCVideoView::WriteVideoFrame(AVFormatContext *&m_pOc,void* p, int len)
{
int ret;
if ( 0 > vi )
{
MessageBox(_T("vi less than 0"));
return;
}
AVStream *pst = m_pOc->streams[vi];
AVPacket pkt;
AVCodecContext *c = pst->codec;
av_init_packet( &pkt );
pkt.flags |= (0 >= GetVopType(p, len)) ? AV_PKT_FLAG_KEY : 0;
pkt.stream_index = pst->index;
pkt.data = (uint8_t*)p;
pkt.size = len;
// Wait for key frame
if ( waitkey )
if ( 0 == ( pkt.flags & AV_PKT_FLAG_KEY ) )
return;
else
waitkey = 0;
if (c->coded_frame->pts != AV_NOPTS_VALUE)
pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, pst->time_base);
ret = av_interleaved_write_frame( m_pOc, &pkt );
if (ret < 0)
{
MessageBox(_T("cannot write video frame"));
}
} [AppleScript] 纯文本查看 复制代码 void CMFCVideoView::DestroyTSMuxer(AVFormatContext*& m_pOc)
{
waitkey = -1;
vi = -1;
if (m_pOc)
av_write_trailer(m_pOc);
if (m_pOc && !(m_pOc->oformat->flags & AVFMT_NOFILE))
avio_close(m_pOc->pb);
if (m_pOc)
{
avformat_free_context(m_pOc);
m_pOc = NULL;
}
}
|