#include "demuxing.h"
FILE * OpenOutputFile(FILE * FOutputFile,char * OutputFileName)
{
FOutputFile = fopen(OutputFileName, "w");
if (NULL != FOutputFile)
{
fclose(FOutputFile);
fopen(OutputFileName, "w");//文件清空
fclose(FOutputFile);
FOutputFile = NULL;
}
else
{
printf("打开输出文件失败!\n");
}
FOutputFile = fopen(OutputFileName, "wb");
return FOutputFile;
}
int open_codec_context(int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaType type)
{
int ret;
AVStream *st;
AVCodecContext *dec_ctx = NULL;
AVCodec *dec = NULL;
ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
if (ret < 0)
{
printf("Could not find %s stream in input file '%s'\n",av_get_media_type_string(type), INPUTTSFILENAME);
return ret;
}
else
{
*stream_idx = ret;
st = fmt_ctx->streams[*stream_idx];
/* find decoder for the stream */
dec_ctx = st->codec;
dec = avcodec_find_decoder(dec_ctx->codec_id);
if (!dec)
{
printf("Failed to find %s codec\n",av_get_media_type_string(type));
return ret;
}
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0)
{
printf("Failed to open %s codec\n",av_get_media_type_string(type));
return ret;
}
}
return 0;
}
int decode_packet(int *got_frame, int cached)
{
int ret = 0;
AVSampleFormat m_format;
if (pkt.stream_index == video_stream_idx)
{
/* decode video frame */
ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
if (ret < 0)
{
printf("Error decoding video frame\n");
return ret;
}
if (*got_frame)
{
printf("video_frame%s n:%d nb_samples:%d\n",cached ? "(cached)" : "", video_frame_count++, frame->nb_samples);
/* copy decoded frame to destination buffer:
* this is required since rawvideo expects non aligned data */
av_image_copy(video_yuv_data, video_yuv_linesize, (const uint8_t **)(frame->data), frame->linesize, video_dec_ctx->pix_fmt, video_dec_ctx->width, video_dec_ctx->height);
/* write to rawvideo file */
fwrite(video_yuv_data[0], 1, video_yuv_bufsize, video_yuv_file);
}
}
else if (pkt.stream_index == audio_stream_idx)
{
/* decode audio frame */
ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt);
if (ret < 0)
{
printf("Error decoding audio frame\n");
return ret;
}
switch(frame->format)
{
case -1:
m_format = AV_SAMPLE_FMT_NONE;
break;
case 0:
m_format = AV_SAMPLE_FMT_U8;
break;
case 1:
m_format = AV_SAMPLE_FMT_S16;
break;
case 2:
m_format = AV_SAMPLE_FMT_S32;
break;
case 3:
m_format = AV_SAMPLE_FMT_FLT;
break;
case 4:
m_format = AV_SAMPLE_FMT_DBL;
break;
case 5:
m_format = AV_SAMPLE_FMT_U8P;
break;
case 6:
m_format = AV_SAMPLE_FMT_S16P;
break;
case 7:
m_format = AV_SAMPLE_FMT_S32P;
break;
case 8:
m_format = AV_SAMPLE_FMT_FLTP;
break;
case 9:
m_format = AV_SAMPLE_FMT_DBLP;
break;
case 10:
m_format = AV_SAMPLE_FMT_NB;
break;
default:
printf("frame->format = %d ERROR \n",frame->format);
}
if (*got_frame)
{
printf("audio_frame%s n:%d nb_samples:%d\n", cached ? "(cached)" : "", audio_frame_count++, frame->nb_samples);
ret = av_samples_alloc(audio_pcm_data, &audio_pcm_linesize, frame->channels, frame->nb_samples, m_format, 1);
if (ret < 0)
{
printf( "Could not allocate audio buffer\n");
return AVERROR(ENOMEM);
}
/* TODO: extend return code of the av_samples_* functions so that this call is not needed */
audio_pcm_bufsize =av_samples_get_buffer_size(NULL, frame->channels, frame->nb_samples, m_format, 1);
/* copy audio data to destination buffer:
* this is required since rawaudio expects non aligned data */
av_samples_copy(audio_pcm_data, frame->data, 0, 0, frame->nb_samples, frame->channels, m_format);
/* write to rawaudio file */
fwrite(audio_pcm_data[0], 1, audio_pcm_linesize, audio_pcm_file);
av_freep(&audio_pcm_data[0]);
}
}
return ret;
}
int zwg_save_frame()
{
//音频
if (pkt.stream_index == audio_stream_idx)
{
fwrite(pkt.data, 1, pkt.size, audio_MP3_file);
}
//视频
else if (pkt.stream_index == video_stream_idx )
{
fwrite(pkt.data, 1, pkt.size, video_H264_file);
}
return 1;
}
int get_format_from_sample_fmt(const char **fmt, enum AVSampleFormat sample_fmt)
{
int i;
struct sample_fmt_entry
{
enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
} sample_fmt_entries[] = {
{ AV_SAMPLE_FMT_U8, "u8", "u8" },
{ AV_SAMPLE_FMT_S16, "s16be", "s16le" },
{ AV_SAMPLE_FMT_S32, "s32be", "s32le" },
{ AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
{ AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
};
*fmt = NULL;
for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++)
{
struct sample_fmt_entry *entry = &sample_fmt_entries[i];
if (sample_fmt == entry->sample_fmt)
{
*fmt = AV_NE(entry->fmt_be, entry->fmt_le);
return 0;
}
}
printf("sample format %s is not supported as output format\n",av_get_sample_fmt_name(sample_fmt));
return -1;
}
int Zwg_Demuxing()
{
if (avformat_open_input(&fmt_ctx, INPUTTSFILENAME, NULL, NULL) < 0) //打开要别应用的输入文件
{
printf("Could not open source file %s\n", INPUTTSFILENAME);
return getchar();
}
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) //查看流信息
{
printf("Could not find stream information\n");
return getchar();
}
av_dump_format(fmt_ctx, 0, INPUTTSFILENAME, 0); //输入信息错误写入
video_yuv_file = OpenOutputFile(video_yuv_file,OUTPUTYUVFILENAME);
audio_pcm_file = OpenOutputFile(audio_pcm_file,OUTPUTPCMFILENAME);
video_H264_file = OpenOutputFile(video_H264_file,OUTPUTH264FILENAME);
audio_MP3_file = OpenOutputFile(audio_MP3_file,OUTPUTMP3FILENAME);
if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) //得到视频信息的CONTEXT
{
video_stream = fmt_ctx->streams[video_stream_idx];
video_dec_ctx = video_stream->codec;
//分配解码后YUV数据空间
ret = av_image_alloc(video_yuv_data, video_yuv_linesize,video_dec_ctx->width, video_dec_ctx->height,video_dec_ctx->pix_fmt, 1);
if (ret < 0)
{
printf("Could not allocate raw video buffer\n");
}
video_yuv_bufsize = ret;
}
if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0)
{
int nb_planes;
audio_stream = fmt_ctx->streams[audio_stream_idx];
audio_dec_ctx = audio_stream->codec;
nb_planes = av_sample_fmt_is_planar(audio_dec_ctx->sample_fmt) ?audio_dec_ctx->channels : 1;
audio_pcm_data = (uint8_t **)av_mallocz(sizeof(uint8_t *) * nb_planes);
if (!audio_pcm_data)
{
printf("Could not allocate audio data buffers\n");
ret = AVERROR(ENOMEM);
}
}
if (!audio_stream && !video_stream)
{
printf("Could not find audio or video stream in the input, aborting\n");
ret = 1;
return getchar();
}
frame = avcodec_alloc_frame(); //分配空间
if (!frame)
{
printf("Could not allocate frame\n");
return getchar();
}
av_init_packet(&pkt); // initialize packet, set data to NULL, let the demuxer fill it
pkt.data = NULL;
pkt.size = 0;
if (video_stream)
{
printf("Demuxing video from file '%s' into '%s' and '%s'\n", INPUTTSFILENAME, OUTPUTYUVFILENAME,OUTPUTH264FILENAME);
}
if (audio_stream)
{
printf("Demuxing video from file '%s' into '%s'
没有合适的资源?快使用搜索试试~ 我知道了~
FFMPEG_TS_DEMUX_本地文件_1
共112个文件
h:76个
dll:8个
lib:8个
5星 · 超过95%的资源 需积分: 9 89 下载量 32 浏览量
2012-11-22
17:39:27
上传
评论 1
收藏 14.28MB 7Z 举报
温馨提示
FFMPEG 解复用 ts 文件程序,增加中间 存储到本地步骤。初学者可以参考下。
资源推荐
资源详情
资源评论
收起资源包目录
FFMPEG_TS_DEMUX_本地文件_1 (112个子文件)
demuxing.cpp 9KB
Main.cpp 425B
mt.dep 65B
avcodec-54.dll 14.53MB
avformat-54.dll 2.78MB
avfilter-3.dll 1.65MB
avdevice-54.dll 382KB
swscale-2.dll 334KB
avutil-52.dll 232KB
postproc-52.dll 174KB
swresample-0.dll 108KB
Dedmuxing.exe 35KB
avcodec.h 164KB
avformat.h 76KB
avfilter.h 31KB
opt.h 27KB
pixfmt.h 23KB
avio.h 17KB
intreadwrite.h 15KB
old_pix_fmts.h 14KB
common.h 12KB
swscale.h 12KB
old_codec_ids.h 10KB
avfiltergraph.h 10KB
samplefmt.h 9KB
audioconvert.h 8KB
inttypes.h 8KB
pixdesc.h 8KB
stdint.h 8KB
swresample.h 8KB
mem.h 8KB
imgutils.h 8KB
avstring.h 7KB
log.h 7KB
parseutils.h 7KB
eval.h 6KB
bprint.h 6KB
xvmc.h 6KB
avutil.h 5KB
timecode.h 5KB
vda.h 5KB
dict.h 5KB
buffersink.h 5KB
error.h 5KB
fifo.h 5KB
avcodec.h 4KB
mathematics.h 4KB
cpu.h 4KB
audio_fifo.h 4KB
vaapi.h 4KB
rational.h 4KB
attributes.h 4KB
version.h 4KB
version.h 4KB
asrc_abuffer.h 3KB
version.h 3KB
avfft.h 3KB
vdpau.h 3KB
bswap.h 3KB
buffersrc.h 3KB
postprocess.h 3KB
crc.h 3KB
timestamp.h 3KB
version.h 2KB
file.h 2KB
blowfish.h 2KB
avdevice.h 2KB
dxva2.h 2KB
version.h 2KB
avassert.h 2KB
lzo.h 2KB
lfg.h 2KB
base64.h 2KB
sha.h 2KB
Information.h 2KB
version.h 2KB
aes.h 2KB
xtea.h 2KB
intfloat.h 2KB
version.h 2KB
version.h 2KB
intfloat_readwrite.h 1KB
adler32.h 1KB
random_seed.h 1KB
md5.h 1KB
time.h 1KB
demuxing.h 809B
avconfig.h 210B
BuildLog.htm 15KB
vc90.idb 203KB
Dedmuxing.ilk 432KB
avcodec.lib 65KB
avutil.lib 57KB
avfilter.lib 39KB
avformat.lib 31KB
swresample.lib 23KB
swscale.lib 9KB
postproc.lib 3KB
avdevice.lib 2KB
Dedmuxing.exe.embed.manifest 663B
共 112 条
- 1
- 2
朱韦刚
- 粉丝: 754
- 资源: 76
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页