ffmpeg如何压缩mp3音频文件后为什么音频播放时间减少了

因为工作中有部分内容是使用PHP来获取音/视频信息以及对音视频文件进行处理,遇到了一些问题,所以一路搜过来。
因为视频处理必然离不开FFmpeg,网上抄了一段:
FFmpeg是领先的多媒体框架。
在这个框架中包含有各种工具,每一个用于完成特定的功能。例如:
ffserver能够将多媒体文件转化为用于实时广播的流;
ffprobe用于分析多媒体流;
ffplay可以当作一个简易的媒体播放器;
ffmpeg则能够转换多媒体文件格式。
注意:ffmpeg和FFmpeg不是同一个东西。FFmpeg是框架,而ffmpeg是一个其中的一个功能。
FFmpeg自然成了搜索的关键词,搜到了这篇文章:FFMPEG获取视频播放时长:
上文中提及了一种方式,其实跟我的非常相似,我这里也有其他方案一起整理下,可能对遇到类似问题的同学有帮助;
处理或获取多媒体文件(音/视频)信息目前确定要用FFmpeg,有以下2中方式
使用php-ffmpeg扩展
依赖:php-ffmpeg扩展优点:使用方便建议:不推荐,对视频需求要求不高可考虑使用(1.项目比较老,年久失修,无人维护;2. 少新的视频格式可能会不支持;)
该方案需要安装php-ffmpeg扩展,项目地址:
在编译过程中遇到了一些问题,通过Google/Bing/Youdao/Soso/Baidu找到方案处理了,在百度网盘,纯绿色无污染,无ghost,可放心使用,不放心可以下载后与原项目代码对比后使用
下载地址:
# 安装php-ffmpeg扩展
./configure --with-php-config=/usr/local/php/bin/php-config
make install
# php.ini中增加
extension = ffmpeg.so
# 重启php-fpm或httpd
Github上还有2项目,不过没用过,给出链接PHP-FFMpeg:FFmpeg官网推荐了此方式并给出项目地址,应该靠谱
phpvideotoolkit-v2: 如果那位用过还请给点意见 ^_^
使用PHP调用命令
题外话:PHP有很多函数可以直接调用shell,如:system,passthru,exec,shell_exec等,一般情况下不推荐使用,而且强烈建议在php.ini中使用disable_functions禁用这些危险函数(各种骇客,脚本小子,后门作者的最爱),特殊需要可以用php -c指定另外的php.ini配置
接下来要说的是获取媒体文件信息《》文中的基本思路相同,都是使用php函数来调用shell命令, 不同的是没有使用ffmpeg命令,该文中是使用正则来匹配命令输出结果,而实际上FFmpeg提供了获取媒体文件的命令,还可以输出json格式处理起来非常方便:
FFmpeg编译安装比较麻烦(底层依赖很多,),不过官方提供了各个平台的二进制文件供下载,直接下载解压即可执行;
# 以json格式输出媒体文件信息
ffprobe -v quiet -print_format json -show_format -show_streams input.mp4
# 输出信息如下:
"streams": [
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "High",
"codec_type": "video",
"codec_time_base": "1/30",
"codec_tag_string": "avc1",
"codec_tag": "0x",
"width": 480,
"height": 268,
"has_b_frames": 2,
"sample_aspect_ratio": "0:1",
"display_aspect_ratio": "0:1",
"pix_fmt": "yuv420p",
"level": 21,
"chroma_location": "left",
"is_avc": "1",
"nal_length_size": "4",
"r_frame_rate": "15/1",
"avg_frame_rate": "15/1",
"time_base": "1/15",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 1483,
"duration": "98.866667",
"bit_rate": "260496",
"bits_per_raw_sample": "8",
"nb_frames": "1483",
"disposition": {
"default": 1,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0
"creation_time": " 00:00:00",
"language": "und",
"handler_name": "VideoHandler"
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "LC",
"codec_type": "audio",
"codec_time_base": "1/44100",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "44100",
"channels": 2,
"channel_layout": "stereo",
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/44100",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 4353024,
"duration": "98.708027",
"bit_rate": "41825",
"nb_frames": "4251",
"disposition": {
"default": 1,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0
"creation_time": " 00:00:00",
"language": "und",
"handler_name": "SoundHandler"
"format": {
"filename": "input.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "0.000000",
"duration": "98.867000",
"size": "3790585",
"bit_rate": "306721",
"probe_score": 100,
"major_brand": "isom",
"minor_version": "512",
"compatible_brands": "isomiso2avc1mp41",
"creation_time": " 00:00:00",
"encoder": "Lavf52.64.2"
# 可以直接使用 json_decode 来处理,非常方便
原文中的方式稍繁琐一点,不过用正则去提取信息也算是一种思路,以下是ffmpeg命令输出结果
ffmpeg -i input.mp4
# 输出结果
ffmpeg version 2.5.4 Copyright (c)
the FFmpeg developers
built on Feb 15 :10 with Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
configuration: --prefix=/usr/local/Cellar/ffmpeg/2.5.4 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-vda
54. 15.100 / 54. 15.100
libavcodec
56. 13.100 / 56. 13.100
libavformat
56. 15.102 / 56. 15.102
libavdevice
3.100 / 56.
libavfilter
libavresample
libswscale
libswresample
libpostproc
3.100 / 53.
# ===& 以下一行是输入的第一个文件信息
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
major_brand
minor_version
compatible_brands: isomiso2avc1mp41
creation_time
: Lavf52.64.2
# ===& 以下一行是 媒体文件时长和比特率
Duration: 00:01:38.87, start: 0.000000, bitrate: 306 kb/s
# ===& 以下一行是 第1个媒体流信息(一般是视频,也有可能有是音频)
Stream #0:0(und): Video: h264 (High) (avc1 / 0x), yuv420p, 480x268, 260 kb/s, 15 fps, 15 tbr, 15 tbn, 30 tbc (default)
creation_time
handler_name
: VideoHandler
# ===& 以下一行是 第2个媒体流信息
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 41 kb/s (default)
# ===& 如果有字母 还会有第3个媒体流信息
creation_time
handler_name
: SoundHandler
At least one output file must be specified
# 接下来使用正则匹配获取相关信息
除了获取视频信息以外还可以调用ffmpeg命令对媒体做更多的事情,包括但不限于:
ffmpeg -y –i input.flv –r 1 –q:v 2 –f image2 output.jpg
拆分/合并视频
ffmpeg -i input.flv -ss 4 -t 16 output.mp4
从视频中提取音/视频
ffmpeg -i input.flv -vn -c:a copy ouput.mp3
ffmpeg -i input input.flv -an -c:v libx264 output.mp4
给视频添加文字/图片水印
ffmpeg -i input.flv -vf "drawtext=text=teachmyself:fontfile=/Library/Fonts/Arial.ttf:fontsize=16:fontcolor=yellow@0.8:x=w-tw-16:y=h-th-16" -c:v libx264 -c:a copy output.mp4
给视频添加倒影效果
ffmpeg -i input.flv -vf 'split[up][down]; [up]pad=iw:ih*2'[up]; [down]vflip[down]; [up][down]overlay=0:h' output.mp4
非常强大,因为大家常见的暴风影音,QQ影音,格式工厂等大多播放器及视频处理软件都是用它包了个壳...
大多信息来自网络,时间久远,无法一一列出来源往见谅,描述有误望给予指正,感激不尽!
这里还有几篇文章推荐下:
《FFMPEG视音频编解码零基础学习方法》:
官网wiki《PHP中使用FFmpeg》:
英文教程《A FFmpeg Tutorial For Beginners》:
ffmpeg 翻译文档 (ffmpeg-all 包含重要组件)(热心人士翻译的ffmpeg文档)
搜索引擎写的有点多,我要把百度列到最后以表对该垃圾搜索的各种不满
你可能感兴趣的文章
17 收藏,4.2k
5 收藏,4k
1 收藏,2.8k
本文采用 署名-相同方式共享 3.0 中国大陆许可协议,分享、演绎需署名且使用相同方式共享
分享到微博?
你好!看起来你挺喜欢这个内容,但是你还没有注册帐号。 当你创建了帐号,我们能准确地追踪你关注的问题,在有新答案或内容的时候收到网页和邮件通知。还能直接向作者咨询更多细节。如果上面的内容有帮助,记得点赞 (????)? 表示感谢。
明天提醒我
我要该,理由是:
扫扫下载 App
SegmentFault
一起探索更多未知?您当前的位置: >
[FFmpeg]基于FFmpeg技术的视音频编解码
时间: 16:59来源:雷霄骅  极酷网页播放器
转码器在视音频编解码处理的程序中,属于一个比较复杂的东西。因为它结合了视频的解码和编码
本文介绍一个简单的基于FFmpeg的转码器。转码器在视音频编解码处理的程序中,属于一个比较复杂的东西。因为它结合了视频的解码和编码。一个视 频播放器,一般只包含解码功能;一个视频编码工具,一般只包含编码功能;而一个视频转码器,则需要先对视频进行解码,然后再对视频进行编码,因而相当于解 码器和编码器的结合。下图例举了一个视频的转码流程。输入视频的封装格式是FLV,视频编码标准是H.264,音频编码标准是AAC;输出视频的封装格式 是AVI,视频编码标准是MPEG2,音频编码标准是MP3。从流程中可以看出,首先从输入视频中分离出视频码流和音频压缩码流,然后分别将视频码流和音 频码流进行解码,获取到非压缩的像素数据/音频采样数据,接着将非压缩的像素数据/音频采样数据重新进行编码,获得重新编码后的视频码流和音频码流,最后 将视频码流和音频码流重新封装成一个文件。
本文介绍的视频转码器正是使用FFMPEG类库从编程的角度实现了上述流程。
下面贴上代码,代码是从FFmpeg的例子改编的,平台是VC2010,类库版本是
/*&&*最简单的基于FFmpeg的转码器&&*Simplest&FFmpeg&Transcoder&&*&&*雷霄骅&Lei&Xiaohua&&*&&*中国传媒大学/数字电视技术&&*Communication&University&of&China&/&DigitalTV&Technology&&*http://blog.csdn.net/leixiaohua1020&&*&&*本程序实现了视频格式之间的转换。是一个最简单的视频转码程序。&&*&&*/&&&#include&&stdafx.h&&extern&&C&&{&#include&&libavcodec/avcodec.h&&#include&&libavformat/avformat.h&&#include&&libavfilter/avfiltergraph.h&&#include&&libavfilter/avcodec.h&&#include&&libavfilter/buffersink.h&&#include&&libavfilter/buffersrc.h&&#include&&libavutil/avutil.h&&#include&&libavutil/opt.h&&#include&&libavutil/pixdesc.h&&};&&&&&&&static&AVFormatContext&*ifmt_&static&AVFormatContext&*ofmt_&typedef&struct&FilteringContext{&&&&&AVFilterContext*buffersink_&&&&&AVFilterContext*buffersrc_&&&&&AVFilterGraph*filter_&}&FilteringC&static&FilteringContext&*filter_&static&int&open_input_file(const&char&*filename)&{&&&&&int&&&&&&unsigned&int&i;&&&&&ifmt_ctx&=NULL;&&&&&if&((ret&=&avformat_open_input(&ifmt_ctx,filename,&NULL,&NULL))&&)&{&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannot&openinput&file\n&);&&&&&&&&&return&&&&&&}&&&&&if&((ret&=&avformat_find_stream_info(ifmt_ctx,&NULL))&)&{&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannot&findstream&information\n&);&&&&&&&&&return&&&&&&}&&&&&for&(i&=&0;&i&&nb_&i++)&{&&&&&&&&&AVStream*&&&&&&&&AVCodecContext&*codec_&&&&&&&&&stream&=ifmt_ctx-streams[i];&&&&&&&&&codec_ctx&=stream-&&&&&&&&&/*&Reencode&video&&&audio&and&remux&subtitles&etc.&*/&&&&&&&&&if&(codec_ctx-codec_type&==&AVMEDIA_TYPE_VIDEO&&&&&&&&&&&&&&&&&||codec_ctx-codec_type&==&AVMEDIA_TYPE_AUDIO)&{&&&&&&&&&&&&&/*&Open&decoder&*/&&&&&&&&&&&&&ret&=avcodec_open2(codec_ctx,&&&&&&&&&&&&&&&&&&&&avcodec_find_decoder(codec_ctx-codec_id),&NULL);&&&&&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Failed&toopen&decoder&for&stream&#%u\n&,&i);&&&&&&&&&&&&&&&&&return&&&&&&&&&&&&&&}&&&&&&&&&}&&&&&}&&&&av_dump_format(ifmt_ctx,&0,&filename,&0);&&&&&return&0;&}&static&int&open_output_file(const&char&*filename)&{&&&&&AVStream*out_&&&&&AVStream*in_&&&&&AVCodecContext*dec_ctx,&*enc_&&&&&AVCodec*&&&&&int&&&&&&unsigned&int&i;&&&&&ofmt_ctx&=NULL;&&&&avformat_alloc_output_context2(&ofmt_ctx,&NULL,&NULL,&filename);&&&&&if&(!ofmt_ctx)&{&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Could&notcreate&output&context\n&);&&&&&&&&&return&AVERROR_UNKNOWN;&&&&&}&&&&&for&(i&=&0;&i&&nb_&i++)&{&&&&&&&&&out_stream=&avformat_new_stream(ofmt_ctx,&NULL);&&&&&&&&&if&(!out_stream)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Failedallocating&output&stream\n&);&&&&&&&&&&&&&return&AVERROR_UNKNOWN;&&&&&&&&&}&&&&&&&&&in_stream&=ifmt_ctx-streams[i];&&&&&&&&&dec_ctx&=in_stream-&&&&&&&&&enc_ctx&=out_stream-&&&&&&&&&if&(dec_ctx-codec_type&==&AVMEDIA_TYPE_VIDEO&&&&&&&&&&&&&&&&&||dec_ctx-codec_type&==&AVMEDIA_TYPE_AUDIO)&{&&&&&&&&&&&&&/*&in&this&example,&we&choose&transcoding&to&same&codec&*/&&&&&&&&&&&&&encoder=&avcodec_find_encoder(dec_ctx-codec_id);&&&&&&&&&&&&&/*&In&this&example,&we&transcode&to&same&properties(picture&size,&&&&&&&&&&&&&*&sample&rate&etc.).&These&properties&can&be&changed&for&output&&&&&&&&&&&&&*&streams&easily&using&filters&*/&&&&&&&&&&&&&if&(dec_ctx-codec_type&==&AVMEDIA_TYPE_VIDEO)&{&&&&&&&&&&&&&&&&enc_ctx-height&=&dec_ctx-&&&&&&&&&&&&&&&&enc_ctx-width&=&dec_ctx-&&&&&&&&&&&&&&&&enc_ctx-sample_aspect_ratio&=&dec_ctx-sample_aspect_&&&&&&&&&&&&&&&&&/*&take&first&format&from&list&of&supported&formats&*/&&&&&&&&&&&&&&&&enc_ctx-pix_fmt&=&encoder-pix_fmts[0];&&&&&&&&&&&&&&&&&/*&video&time_base&can&be&set&to&whatever&is&handy&andsupported&by&encoder&*/&&&&&&&&&&&&&&&&enc_ctx-time_base&=&dec_ctx-time_&&&&&&&&&&&&&}&else&{&&&&&&&&&&&&&&&&enc_ctx-sample_rate&=&dec_ctx-sample_&&&&&&&&&&&&&&&&enc_ctx-channel_layout&=&dec_ctx-channel_&&&&&&&&&&&&&&&&enc_ctx-channels&=&av_get_channel_layout_nb_channels(enc_ctx-channel_layout);&&&&&&&&&&&&&&&&&/*&take&first&format&from&list&of&supported&formats&*/&&&&&&&&&&&&&&&&enc_ctx-sample_fmt&=&encoder-sample_fmts[0];&&&&&&&&&&&&&&&&&AVRationaltime_base={1,&enc_ctx-sample_rate};&&&&&&&&&&&&&&&&enc_ctx-time_basetime_base&=&time_&&&&&&&&&&&&&}&&&&&&&&&&&&&/*&Third&parameter&can&be&used&to&pass&settings&to&encoder*/&&&&&&&&&&&&&ret&=avcodec_open2(enc_ctx,&encoder,&NULL);&&&&&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannot&openvideo&encoder&for&stream&#%u\n&,&i);&&&&&&&&&&&&&&&&&return&&&&&&&&&&&&&&}&&&&&&&&&}&else&if(dec_ctx-codec_type&==&AVMEDIA_TYPE_UNKNOWN)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_FATAL,&&Elementarystream&#%d&is&of&unknown&type,&cannot&proceed\n&,&i);&&&&&&&&&&&&&return&AVERROR_INVALIDDATA;&&&&&&&&&}&else&{&&&&&&&&&&&&&/*&if&this&stream&must&be&remuxed&*/&&&&&&&&&&&&&ret&=avcodec_copy_context(ofmt_ctx-streams[i]-codec,&&&&&&&&&&&&&&&&&&&&ifmt_ctx-streams[i]-codec);&&&&&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Copyingstream&context&failed\n&);&&&&&&&&&&&&&&&&&return&&&&&&&&&&&&&&}&&&&&&&&&}&&&&&&&&&if&(ofmt_ctx-oformat-flags&&AVFMT_GLOBALHEADER)&&&&&&&&&&&&enc_ctx-flags&|=&CODEC_FLAG_GLOBAL_HEADER;&&&&&}&&&&av_dump_format(ofmt_ctx,&0,&filename,&1);&&&&&if&(!(ofmt_ctx-oformat-flags&&AVFMT_NOFILE))&{&&&&&&&&&ret&=avio_open(&ofmt_ctx-pb,&filename,&AVIO_FLAG_WRITE);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Could&notopen&output&file&'%s'&,&filename);&&&&&&&&&&&&&return&&&&&&&&&&}&&&&&}&&&&&/*&init&muxer,&write&output&file&header&*/&&&&&ret&=avformat_write_header(ofmt_ctx,&NULL);&&&&&if&(ret&&)&{&&&&&&&&&av_log(NULL,AV_LOG_ERROR,&&Error&occurred&when&openingoutput&file\n&);&&&&&&&&&return&&&&&&}&&&&&return&0;&}&static&intinit_filter(FilteringContext*&fctx,&AVCodecContext&*dec_ctx,&&&&&&&&AVCodecContext&*enc_ctx,&const&char&*filter_spec)&{&&&&&char&args[512];&&&&&int&ret&=&0;&&&&&AVFilter*buffersrc&=&NULL;&&&&&AVFilter*buffersink&=&NULL;&&&&&AVFilterContext*buffersrc_ctx&=&NULL;&&&&&AVFilterContext*buffersink_ctx&=&NULL;&&&&&AVFilterInOut*outputs&=&avfilter_inout_alloc();&&&&&AVFilterInOut*inputs&&=&avfilter_inout_alloc();&&&&&AVFilterGraph*filter_graph&=&avfilter_graph_alloc();&&&&&if&(!outputs&||&!inputs&||&!filter_graph)&{&&&&&&&&&ret&=AVERROR(ENOMEM);&&&&&&&&&goto&&&&&&}&&&&&if&(dec_ctx-codec_type&==&AVMEDIA_TYPE_VIDEO)&{&&&&&&&&&buffersrc&=avfilter_get_by_name(&buffer&);&&&&&&&&&buffersink=&avfilter_get_by_name(&buffersink&);&&&&&&&&&if&(!buffersrc&||&!buffersink)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&filteringsource&or&sink&element&not&found\n&);&&&&&&&&&&&&&ret&=&AVERROR_UNKNOWN;&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&_snprintf(args,&sizeof(args),&&&&&&&&&&&&&&&&&&video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d&,&&&&&&&&&&&&&&&&dec_ctx-width,&dec_ctx-height,&dec_ctx-pix_fmt,&&&&&&&&&&&&&&&&&dec_ctx-time_base.num,dec_ctx-time_base.den,&&&&&&&&&&&&&&&&dec_ctx-sample_aspect_ratio.num,&&&&&&&&&&&&&&&&dec_ctx-sample_aspect_ratio.den);&&&&&&&&&ret&=avfilter_graph_create_filter(&buffersrc_ctx,&buffersrc,&&in&,&&&&&&&&&&&&&&&&args,&NULL,&filter_graph);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannotcreate&buffer&source\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&&ret&=avfilter_graph_create_filter(&buffersink_ctx,&buffersink,&&out&,&&&&&&&&&&&&&&&&NULL,&NULL,&filter_graph);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannotcreate&buffer&sink\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&&ret&=av_opt_set_bin(buffersink_ctx,&&pix_fmts&,&&&&&&&&&&&&&&&&(uint8_t*)&enc_ctx-pix_fmt,&sizeof(enc_ctx-pix_fmt),&&&&&&&&&&&&&&&&AV_OPT_SEARCH_CHILDREN);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannot&setoutput&pixel&format\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&}&else&if(dec_ctx-codec_type&==&AVMEDIA_TYPE_AUDIO)&{&&&&&&&&&buffersrc&=&avfilter_get_by_name(&abuffer&);&&&&&&&&&buffersink=&avfilter_get_by_name(&abuffersink&);&&&&&&&&&if&(!buffersrc&||&!buffersink)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&filteringsource&or&sink&element&not&found\n&);&&&&&&&&&&&&&ret&=AVERROR_UNKNOWN;&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&&if&(!dec_ctx-channel_layout)&&&&&&&&&&&&dec_ctx-channel_layout&=&&&&&&&&&&&&&&&&av_get_default_channel_layout(dec_ctx-channels);&&&&&&&&_snprintf(args,&sizeof(args),&&&&&&&&&&&&&&&&&&time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%I64x&,&&&&&&&&&&&&&&&&dec_ctx-time_base.num,&dec_ctx-time_base.den,dec_ctx-sample_rate,&&&&&&&&&&&&&&&&av_get_sample_fmt_name(dec_ctx-sample_fmt),&&&&&&&&&&&&&&&&dec_ctx-channel_layout);&&&&&&&&&ret&=avfilter_graph_create_filter(&buffersrc_ctx,&buffersrc,&&in&,&&&&&&&&&&&&&&&&args,&NULL,&filter_graph);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannotcreate&audio&buffer&source\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&&ret&=avfilter_graph_create_filter(&buffersink_ctx,&buffersink,&&out&,&&&&&&&&&&&&&&&&NULL,&NULL,&filter_graph);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannotcreate&audio&buffer&sink\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&&ret&=&av_opt_set_bin(buffersink_ctx,&&sample_fmts&,&&&&&&&&&&&&&&&&(uint8_t*)&enc_ctx-sample_fmt,&sizeof(enc_ctx-sample_fmt),&&&&&&&&&&&&&&&&AV_OPT_SEARCH_CHILDREN);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannot&setoutput&sample&format\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&&ret&=av_opt_set_bin(buffersink_ctx,&&channel_layouts&,&&&&&&&&&&&&&&&&(uint8_t*)&enc_ctx-channel_layout,&&&&&&&&&&&&&&&&&sizeof(enc_ctx-channel_layout),AV_OPT_SEARCH_CHILDREN);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannot&setoutput&channel&layout\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&&ret&=av_opt_set_bin(buffersink_ctx,&&sample_rates&,&&&&&&&&&&&&&&&&(uint8_t*)&enc_ctx-sample_rate,&sizeof(enc_ctx-sample_rate),&&&&&&&&&&&&&&&&AV_OPT_SEARCH_CHILDREN);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Cannot&setoutput&sample&rate\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&}&else&{&&&&&&&&&ret&=AVERROR_UNKNOWN;&&&&&&&&&goto&&&&&&}&&&&&/*&Endpoints&for&the&filter&graph.&*/&&&&outputs-name&&&&&&&=av_strdup(&in&);&&&&outputs-filter_ctx&=&buffersrc_ctx;&&&&outputs-pad_idx&&&&=&0;&&&&outputs-next&&&&&&&=&NULL;&&&&inputs-name&&&&&&&=&av_strdup(&out&);&&&&inputs-filter_ctx&=&buffersink_ctx;&&&&inputs-pad_idx&&&&=&0;&&&&inputs-next&&&&&&&=&NULL;&&&&&if&(!outputs-name&||&!inputs-name)&{&&&&&&&&&ret&=AVERROR(ENOMEM);&&&&&&&&&goto&&&&&&}&&&&&if&((ret&=&avfilter_graph_parse_ptr(filter_graph,filter_spec,&&&&&&&&&&&&&&&&&&&&&inputs,&&outputs,&NULL))&&)&&&&&&&&&goto&&&&&&if&((ret&=&avfilter_graph_config(filter_graph,&NULL))&)&&&&&&&&&goto&&&&&&/*&Fill&FilteringContext&*/&&&&fctx-buffersrc_ctxbuffersrc_ctx&=&buffersrc_&&&&fctx-buffersink_ctxbuffersink_ctx&=&buffersink_&&&&&fctx-filter_graphfilter_graph=&filter_&end:&&&&avfilter_inout_free(&inputs);&&&&avfilter_inout_free(&outputs);&&&&&return&&}&static&int&init_filters(void)&{&&&&&const&char*filter_&&&&&unsigned&int&i;&&&&&int&&&&&&filter_ctx&=(FilteringContext&*)av_malloc_array(ifmt_ctx-nb_streams,&sizeof(*filter_ctx));&&&&&if&(!filter_ctx)&&&&&&&&&return&AVERROR(ENOMEM);&&&&&for&(i&=&0;&i&&nb_&i++)&{&&&&&&&&filter_ctx[i].buffersrc_ctx&&=NULL;&&&&&&&&&filter_ctx[i].buffersink_ctx=&NULL;&&&&&&&&filter_ctx[i].filter_graph&&&=NULL;&&&&&&&&&if(!(ifmt_ctx-streams[i]-codec-codec_type&==&AVMEDIA_TYPE_AUDIO&&&&&&&&&&&&&&&&&||ifmt_ctx-streams[i]-codec-codec_type&==&AVMEDIA_TYPE_VIDEO))&&&&&&&&&&&&&&&&&&&&&&if&(ifmt_ctx-streams[i]-codec-codec_type==&AVMEDIA_TYPE_VIDEO)&&&&&&&&&&&&filter_spec&=&&null&;&/*&passthrough&(dummy)&filter&for&video&*/&&&&&&&&&else&&&&&&&&&&&&filter_spec&=&&anull&;&/*&passthrough&(dummy)&filter&for&audio&*/&&&&&&&&&ret&=init_filter(&filter_ctx[i],&ifmt_ctx-streams[i]-codec,&&&&&&&&&&&&&&&&ofmt_ctx-streams[i]-codec,&filter_spec);&&&&&&&&&if&(ret)&&&&&&&&&&&&&return&&&&&&}&&&&&return&0;&}&static&intencode_write_frame(AVFrame&*filt_frame,&unsignedint&stream_index,&int*got_frame)&{&&&&&int&&&&&&int&got_frame_&&&&&AVPacketenc_&&&&&int&(*enc_func)(AVCodecContext&*,&AVPacket&*,&const&AVFrame&*,&int*)&=&&&&&&&&(ifmt_ctx-streams[stream_index]-codec-codec_type&==&&&&&&&&&AVMEDIA_TYPE_VIDEO)&?&avcodec_encode_video2&:&avcodec_encode_audio2;&&&&&if&(!got_frame)&&&&&&&&&got_frame&=&got_frame_&&&&&av_log(NULL,AV_LOG_INFO,&&Encoding&frame\n&);&&&&&/*&encode&filtered&frame&*/&&&&&enc_pkt.data&=NULL;&&&&&enc_pkt.size&=0;&&&&&av_init_packet(&enc_pkt);&&&&&ret&=enc_func(ofmt_ctx-streams[stream_index]-codec,&&enc_pkt,&&&&&&&&&&&&filt_frame,&got_frame);&&&&av_frame_free(&filt_frame);&&&&&if&(ret&&)&&&&&&&&&return&&&&&&if&(!(*got_frame))&&&&&&&&&return&0;&&&&&/*&prepare&packet&for&muxing&*/&&&&enc_pkt.stream_index&=&stream_&&&&&enc_pkt.dts&=av_rescale_q_rnd(enc_pkt.dts,&&&&&&&&&&&&ofmt_ctx-streams[stream_index]-codec-time_base,&&&&&&&&&&&&ofmt_ctx-streams[stream_index]-time_base,&&&&&&&&&&&&(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));&&&&&enc_pkt.pts&=av_rescale_q_rnd(enc_pkt.pts,&&&&&&&&&&&&ofmt_ctx-streams[stream_index]-codec-time_base,&&&&&&&&&&&&ofmt_ctx-streams[stream_index]-time_base,&&&&&&&&&&&&(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));&&&&enc_pkt.duration&=&av_rescale_q(enc_pkt.duration,&&&&&&&&&&&&ofmt_ctx-streams[stream_index]-codec-time_base,&&&&&&&&&&&&ofmt_ctx-streams[stream_index]-time_base);&&&&&av_log(NULL,AV_LOG_DEBUG,&&Muxing&frame\n&);&&&&&/*&mux&encoded&frame&*/&&&&&ret&=av_interleaved_write_frame(ofmt_ctx,&&enc_pkt);&&&&&return&&}&static&intfilter_encode_write_frame(AVFrame&*frame,&unsignedint&stream_index)&{&&&&&int&&&&&&AVFrame*filt_&&&&&av_log(NULL,AV_LOG_INFO,&&Pushing&decoded&frame&tofilters\n&);&&&&&/*&push&the&decoded&frame&into&the&filtergraph&*/&&&&&ret&=av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,&&&&&&&&&&&&&frame,0);&&&&&if&(ret&&)&{&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Error&whilefeeding&the&filtergraph\n&);&&&&&&&&&return&&&&&&}&&&&&/*&pull&filtered&frames&from&the&filtergraph&*/&&&&&while&(1)&{&&&&&&&&&filt_frame=&av_frame_alloc();&&&&&&&&&if&(!filt_frame)&{&&&&&&&&&&&&&ret&=AVERROR(ENOMEM);&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&av_log(NULL,&AV_LOG_INFO,&&Pullingfiltered&frame&from&filters\n&);&&&&&&&&&ret&=av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,&&&&&&&&&&&&&&&&filt_frame);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&&/*&if&nomore&frames&for&output&-&returns&AVERROR(EAGAIN)&&&&&&&&&&&&&*&if&flushed&and&no&more&frames&for&output&-&returns&AVERROR_EOF&&&&&&&&&&&&&*&rewrite&retcode&to&0&to&show&it&as&normal&procedure&completion&&&&&&&&&&&&&*/&&&&&&&&&&&&&if&(ret&==&AVERROR(EAGAIN)&||&ret&==&AVERROR_EOF)&&&&&&&&&&&&&&&&&ret=&0;&&&&&&&&&&&&av_frame_free(&filt_frame);&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&filt_frame-pict_type&=&AV_PICTURE_TYPE_NONE;&&&&&&&&&ret&=encode_write_frame(filt_frame,&stream_index,&NULL);&&&&&&&&&if&(ret&&)&&&&&&&&&&&&&&&&&&}&&&&&return&&}&static&int&flush_encoder(unsigned&intstream_index)&{&&&&&int&&&&&&int&got_&&&&&if(!(ofmt_ctx-streams[stream_index]-codec-codec-capabilities&&&&&&&&&&&&&&&&&CODEC_CAP_DELAY))&&&&&&&&&return&0;&&&&&while&(1)&{&&&&&&&&av_log(NULL,&AV_LOG_INFO,&&Flushingstream&#%u&encoder\n&,&stream_index);&&&&&&&&&ret&=encode_write_frame(NULL,&stream_index,&&got_frame);&&&&&&&&&if&(ret&&)&&&&&&&&&&&&&&&&&&&&&&if&(!got_frame)&&&&&&&&&&&&&return&0;&&&&&}&&&&&return&&}&&&int_tmain(int&argc,&_TCHAR*&argv[])&{&&&&&int&&&&&&AVP&&&&&AVFrame&*frame=&NULL;&&&&&enum&AVMediaType&&&&&&unsigned&intstream_&&&&&unsigned&int&i;&&&&&int&got_&&&&&int&(*dec_func)(AVCodecContext&*,&AVFrame&*,&int&*,&const&AVPacket*);&&&&&if&(argc&!=&3)&{&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Usage:&%s&file&&file\n&,&argv[0]);&&&&&&&&&return&1;&&&&&}&&&&av_register_all();&&&&avfilter_register_all();&&&&&if&((ret&=&open_input_file(argv[1]))&&)&&&&&&&&&goto&&&&&&if&((ret&=&open_output_file(argv[2]))&&)&&&&&&&&&goto&&&&&&if&((ret&=&init_filters())&&)&&&&&&&&&goto&&&&&&/*&read&all&packets&*/&&&&&while&(1)&{&&&&&&&&&if&((ret=&av_read_frame(ifmt_ctx,&&packet))&&)&&&&&&&&&&&&&&&&&&&&&stream_index&=&packet.stream_&&&&&&&&&type&=ifmt_ctx-streams[packet.stream_index]-codec-codec_&&&&&&&&av_log(NULL,&AV_LOG_DEBUG,&&Demuxergave&frame&of&stream_index&%u\n&,&&&&&&&&&&&&&&&&stream_index);&&&&&&&&&if&(filter_ctx[stream_index].filter_graph)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_DEBUG,&&Going&toreencode&filter&the&frame\n&);&&&&&&&&&&&&&frame&=av_frame_alloc();&&&&&&&&&&&&&if&(!frame)&{&&&&&&&&&&&&&&&&&ret&=&AVERROR(ENOMEM);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&packet.dts&=&av_rescale_q_rnd(packet.dts,&&&&&&&&&&&&&&&&&&&&ifmt_ctx-streams[stream_index]-time_base,&&&&&&&&&&&&&&&&&&&&ifmt_ctx-streams[stream_index]-codec-time_base,&&&&&&&&&&&&&&&&&&&&&(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));&&&&&&&&&&&&packet.pts&=&av_rescale_q_rnd(packet.pts,&&&&&&&&&&&&&&&&&&&&ifmt_ctx-streams[stream_index]-time_base,&&&&&&&&&&&&&&&&&&&&ifmt_ctx-streams[stream_index]-codec-time_base,&&&&&&&&&&&&&&&&&&&&(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));&&&&&&&&&&&&dec_func&=&(type&==&AVMEDIA_TYPE_VIDEO)&?&avcodec_decode_video2&:&&&&&&&&&&&&&&&&avcodec_decode_audio4;&&&&&&&&&&&&&ret&=dec_func(ifmt_ctx-streams[stream_index]-codec,&frame,&&&&&&&&&&&&&&&&&&&&&got_frame,&&packet);&&&&&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&&&&&av_frame_free(&frame);&&&&&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Decodingfailed\n&);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&if&(got_frame)&{&&&&&&&&&&&&&&&&frame-pts&=&av_frame_get_best_effort_timestamp(frame);&&&&&&&&&&&&&&&&&ret=&filter_encode_write_frame(frame,&stream_index);&&&&&&&&&&&&&&&&av_frame_free(&frame);&&&&&&&&&&&&&&&&&if&(ret&)&&&&&&&&&&&&&&&&&&&&goto&&&&&&&&&&&&&&}&else&{&&&&&&&&&&&&&&&&av_frame_free(&frame);&&&&&&&&&&&&&}&&&&&&&&&}&else&{&&&&&&&&&&&&&/*&remux&this&frame&without&reencoding&*/&&&&&&&&&&&&packet.dts&=&av_rescale_q_rnd(packet.dts,&&&&&&&&&&&&&&&&&&&&ifmt_ctx-streams[stream_index]-time_base,&&&&&&&&&&&&&&&&&&&&ofmt_ctx-streams[stream_index]-time_base,&&&&&&&&&&&&&&&&&&&&&(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));&&&&&&&&&&&&packet.pts&=&av_rescale_q_rnd(packet.pts,&&&&&&&&&&&&&&&&&&&&ifmt_ctx-streams[stream_index]-time_base,&&&&&&&&&&&&&&&&&&&&ofmt_ctx-streams[stream_index]-time_base,&&&&&&&&&&&&&&&&&&&&&(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));&&&&&&&&&&&&&ret&=av_interleaved_write_frame(ofmt_ctx,&&packet);&&&&&&&&&&&&&if&(ret&&)&&&&&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&av_free_packet(&packet);&&&&&}&&&&&/*&flush&filters&and&encoders&*/&&&&&for&(i&=&0;&i&&nb_&i++)&{&&&&&&&&&/*&flush&filter&*/&&&&&&&&&if&(!filter_ctx[i].filter_graph)&&&&&&&&&&&&&&&&&&&&&&ret&=filter_encode_write_frame(NULL,&i);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Flushingfilter&failed\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&&&&&/*&flush&encoder&*/&&&&&&&&&ret&=&flush_encoder(i);&&&&&&&&&if&(ret&&)&{&&&&&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Flushingencoder&failed\n&);&&&&&&&&&&&&&goto&&&&&&&&&&}&&&&&}&&&&av_write_trailer(ofmt_ctx);&end:&&&&av_free_packet(&packet);&&&&av_frame_free(&frame);&&&&&for&(i&=&0;&i&&nb_&i++)&{&&&&&&&&avcodec_close(ifmt_ctx-streams[i]-codec);&&&&&&&&&if&(ofmt_ctx&&&&ofmt_ctx-nb_streams&i&&&&ofmt_ctx-streams[i]&&&ofmt_ctx-streams[i]-codec)&&&&&&&&&&&&avcodec_close(ofmt_ctx-streams[i]-codec);&&&&&&&&&if(filter_ctx&&&&filter_ctx[i].filter_graph)&&&&&&&&&&&&avfilter_graph_free(&filter_ctx[i].filter_graph);&&&&&}&&&&av_free(filter_ctx);&&&&avformat_close_input(&ifmt_ctx);&&&&&if&(ofmt_ctx&&&!(ofmt_ctx-oformat-flags&&&AVFMT_NOFILE))&&&&&&&&&avio_close(ofmt_ctx-pb);&&&&avformat_free_context(ofmt_ctx);&&&&&if&(ret&&)&&&&&&&&av_log(NULL,&AV_LOG_ERROR,&&Erroroccurred\n&);&&&&&return&(ret?&1:0);&}&
交流QQ群:

我要回帖

更多关于 ffmpeg 压缩mp3 的文章

 

随机推荐