当前位置: 首页 > news >正文

什么网站可以做字体效果好登封搜索引擎优化

什么网站可以做字体效果好,登封搜索引擎优化,wordpress表单 慢,网站安全检测发生告警后文章目录 FFmpeg 实现音频流抽取1. 包含FFmpeg头文件与命名空间声明2. 主函数与参数处理3. 打开输入文件4. 获取文件信息5. 查找音频流6. 分配输出文件上下文7. 猜测输出文件格式8. 创建新的音频流9. 打开输出文件10. 写入文件头信息11. 读取并写入音频数据12. 写入文件尾部信息…

文章目录

    • FFmpeg 实现音频流抽取
      • 1. 包含FFmpeg头文件与命名空间声明
      • 2. 主函数与参数处理
      • 3. 打开输入文件
      • 4. 获取文件信息
      • 5. 查找音频流
      • 6. 分配输出文件上下文
      • 7. 猜测输出文件格式
      • 8. 创建新的音频流
      • 9. 打开输出文件
      • 10. 写入文件头信息
      • 11. 读取并写入音频数据
      • 12. 写入文件尾部信息并释放资源
    • 运行程序
    • 注意事项
    • 抽取音频完整代码

FFmpeg 实现音频流抽取

1. 包含FFmpeg头文件与命名空间声明

使用FFmpeg库前需要包含相应的头文件,并在C++中声明外部C函数的命名空间。

#ifdef __cplusplus
extern "C" {
#endif
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/log.h>
#ifdef __cplusplus
}
#endif

2. 主函数与参数处理

程序入口点,处理命令行参数。

int main(int argc, char *argv[]) {// 参数检查if (argc < 3) {av_log(nullptr, AV_LOG_INFO, "参数必须多于3个\n");exit(-1);}// 输入输出文件路径char *src = argv[1];char *dst = argv[2];// ...
}

3. 打开输入文件

使用avformat_open_input打开输入文件。

ret = avformat_open_input(&pFmtCtx, src, nullptr, nullptr);
if (ret < 0) {av_log(nullptr, AV_LOG_ERROR, "打开输入文件失败\n");exit(-1);
}

4. 获取文件信息

调用avformat_find_stream_info获取多媒体文件的流信息。

if ((ret = avformat_find_stream_info(pFmtCtx, nullptr)) < 0) {av_log(nullptr, AV_LOG_INFO, "获取文件信息失败\n");exit(-1);
}

5. 查找音频流

遍历所有流,找到音频流的索引。

for (int i = 0; i < pFmtCtx->nb_streams; ++i) {if (pFmtCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {idx = i;break;}
}

6. 分配输出文件上下文

使用avformat_alloc_context分配输出文件的格式上下文。

oFmtCtx = avformat_alloc_context();
if (!oFmtCtx) {av_log(nullptr, AV_LOG_ERROR, "分配输出文件上下文失败\n");goto _ERROR;
}

7. 猜测输出文件格式

使用av_guess_format猜测输出文件的格式。

outFmt = av_guess_format(nullptr, dst, nullptr);
oFmtCtx->oformat = outFmt;

8. 创建新的音频流

为输出文件创建一个新的音频流,并复制输入音频流的参数。

outStream = avformat_new_stream(oFmtCtx, nullptr);
avcodec_parameters_copy(outStream->codecpar, inStream->codecpar);
outStream->codecpar->codec_tag = 0;

9. 打开输出文件

使用avio_open2打开输出文件准备写入。

ret = avio_open2(&oFmtCtx->pb, dst, AVIO_FLAG_WRITE, nullptr, nullptr);
if (ret < 0) {av_log(nullptr, AV_LOG_ERROR, "打开输出文件失败\n");goto _ERROR;
}

10. 写入文件头信息

调用avformat_write_header写入文件头信息。

ret = avformat_write_header(oFmtCtx, nullptr);
if (ret < 0) {av_log(nullptr, AV_LOG_ERROR, "写入文件头失败\n");goto _ERROR;
}

11. 读取并写入音频数据

读取输入文件的音频数据,转换时间戳,并写入输出文件。

while (av_read_frame(pFmtCtx, &pkt) >= 0) {if (pkt.stream_index == idx) {// 转换时间戳等pkt.pts = av_rescale_q_rnd(pkt.pts, inStream->time_base, outStream->time_base, AV_ROUND_NEAR_INF);pkt.dts = pkt.pts;// 写入输出文件av_interleaved_write_frame(oFmtCtx, &pkt);}av_packet_unref(&pkt);
}

12. 写入文件尾部信息并释放资源

写入文件尾部信息,关闭文件,并释放所有分配的资源。

av_write_trailer(oFmtCtx);
avio_close(oFmtCtx->pb);
avformat_free_context(oFmtCtx);_ERROR:// 清理资源if (pFmtCtx) {avformat_free_context(pFmtCtx);#  avformat_close_input(&pFmtCtx);}if (oFmtCtx) {avformat_free_context(oFmtCtx);# avformat_close_input(&oFmtCtx); // 注意:应使用 avformat_free_context 代替}
}

请注意,错误处理部分应使用avformat_free_context代替avformat_close_input来正确释放oFmtCtx资源。另外,程序中存在一些潜在的内存泄漏和错误处理问题,应进一步优化。

运行程序

程序需要传入至少两个参数:输入文件路径和输出文件路径。例如:

./my_ffmpeg_tool input.mp3 output.aac

注意事项

- 确保FFmpeg开发库已正确安装且可链接。
- 检查程序输出的错误信息以进行调试。
- 程序可能需要适当的读取和写入权限。

抽取音频完整代码

cmake_minimum_required(VERSION 3.27)
project(FFmpeg_exercise)
set(CMAKE_CXX_STANDARD 14)# 定义FFmpeg的安装路径变量
set(FFMPEG_INSTALL_DIR "/usr/local/ffmpeg")# 将FFmpeg的头文件目录添加到包含路径
include_directories(${FFMPEG_INSTALL_DIR}/include)# 定义FFmpeg库的基础名称(根据你的需要调整)
set(FFMPEG_LIBS "avcodec;avformat;avutil") # 用分号分隔库名# 寻找并链接FFmpeg库
foreach(FFMPEG_LIB ${FFMPEG_LIBS})find_library(${FFMPEG_LIB}_LIBRARY NAMES ${FFMPEG_LIB}PATHS ${FFMPEG_INSTALL_DIR}/lib NO_DEFAULT_PATH)list(APPEND FFMPEG_LIBRARIES ${${FFMPEG_LIB}_LIBRARY})
endforeach()add_executable(FFmpeg_exercise # main.cppextra_audic.cpp)
# 链接FFmpeg库
target_link_libraries(FFmpeg_exercise ${FFMPEG_LIBRARIES})
//
// Created by 陈伟峰 on 2024/6/22.
//
#ifdef __cplusplus
extern "C" {
#endif
// 包含FFmpeg的头文件
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/log.h>
#ifdef __cplusplus}
#endif
#include <iostream>int main(int argc,char *argv[]){int ret = -1;int idx = -1;//1.处理一些参数;char *src {nullptr};char *dst {nullptr};AVFormatContext *pFmtCtx {nullptr};AVFormatContext *oFmtCtx {nullptr};AVOutputFormat *outFmt {nullptr};AVStream *inStream {nullptr};AVStream *outStream {nullptr};AVPacket pkt {nullptr};//    设置日志级别av_log_set_level(AV_LOG_DEBUG);if(argc<3){av_log(nullptr,AV_LOG_INFO,"arguments must be more than 3\n");exit(-1);}src = argv[1];dst = argv[2];//2.打开输入多媒体文件ret = avformat_open_input(&pFmtCtx,src,nullptr,nullptr);if (ret<0){av_log(nullptr,AV_LOG_ERROR,"avformat_open_input failed\n");exit(-1);}//3.获取多媒体文件信息if ((ret= avformat_find_stream_info(pFmtCtx,nullptr))<0){av_log(nullptr,AV_LOG_INFO,"avformat_find_stream_info failed\n");exit(-1);}//4.遍历所有流,找到音频流for (int i = 0; i < pFmtCtx->nb_streams; ++i) {if (pFmtCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_AUDIO){idx = i;av_log(nullptr,AV_LOG_INFO,"find_stream_info Successed!\n");break;}}if (idx<0){av_log(nullptr,AV_LOG_ERROR,"can not find audio stream\n");exit(-1);}// 打开目的文件上下文oFmtCtx = avformat_alloc_context();if(!oFmtCtx){av_log(nullptr,AV_LOG_ERROR,"avformat_alloc_context failed\n");goto _ERROR;}outFmt = av_guess_format(nullptr,dst,nullptr);oFmtCtx->oformat = outFmt;// 为目的文件,创建一个新的音频流outStream = avformat_new_stream(oFmtCtx,nullptr);// 设置输出音频参数inStream = pFmtCtx->streams[idx];avcodec_parameters_copy(outStream->codecpar,inStream->codecpar);outStream->codecpar->codec_tag = 0;// 绑定ret = avio_open2(&oFmtCtx->pb,dst,AVIO_FLAG_WRITE,nullptr,nullptr);if(ret<0){av_log(nullptr,AV_LOG_ERROR,"avio_open2 failed\n");goto _ERROR;}// 写多媒体文件到目的文件ret = avformat_write_header(oFmtCtx,nullptr);if(ret<0){av_log(nullptr,AV_LOG_ERROR, "error:%s",av_err2str(ret));goto _ERROR;}// 读取输入文件中的音频数据while (av_read_frame(pFmtCtx,&pkt)>=0) {if(pkt.stream_index==idx){// 写入输出文件pkt.pts = av_rescale_q_rnd(pkt.pts,inStream->time_base,outStream->time_base,(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));pkt.dts = pkt.pts;pkt.duration = av_rescale_q(pkt.duration,inStream->time_base,outStream->time_base);pkt.stream_index = 0;pkt.pos = -1;av_interleaved_write_frame(oFmtCtx,&pkt);}av_packet_unref(&pkt);}// 写入文件尾av_write_trailer(oFmtCtx);// 释放资源avio_close(oFmtCtx->pb);avformat_free_context(oFmtCtx);_ERROR:if(pFmtCtx){
//        avformat_close_input(&pFmtCtx);avformat_free_context(pFmtCtx);pFmtCtx = nullptr;}if(oFmtCtx){
//        avformat_close_input(&oFmtCtx);avformat_free_context(oFmtCtx);oFmtCtx = nullptr;}
};
  • 执行结果
 ./FFmpeg_exercise demo.mp4 test.aac

image-20240622111917818

http://www.hkea.cn/news/895598/

相关文章:

  • 怎么用一个主机做多个网站许昌网络推广公司
  • 网站域名所有权郑州网站运营专业乐云seo
  • 桂园精品网站建设费用网站seo查询站长之家
  • 安卓手机怎么做网站站长工具seo综合查询广告
  • 余姚网站建设的公司手机百度账号申请注册
  • 预付网站制作费怎么做凭证如何自制网站
  • 定制网站多少钱北京seo网站管理
  • 南昌做网站公司哪家好如何建立独立网站
  • 成都解放号网站建设什么是百度竞价
  • 网站优化的基本思想与原则百度号码
  • 沧州网站建设制作设计优化深圳seo优化推广
  • 建立一个网站需要什么技术网上培训机构
  • 网站设计与管理论文百度账号注册平台
  • 网站空间商推荐seo是什么职位缩写
  • 怎么建设boss网站文件外链
  • 百度推广网站建设费百度搜索引擎的网址是多少
  • php 手机网站 上传图片定制网站建设
  • 关于网站建设的问题百度关键词分析
  • 登录官方网站装修公司网络推广方案
  • 设计网站官网入口网站搜索优化方法
  • 网站优化qq群山东做网站
  • wordpress icomoon太原seo快速排名
  • 中华建设杂志网站记者数据指数
  • 网站开发测试情况南召seo快速排名价格
  • 上海仓储公司小红书seo优化
  • 南京建设公司网站网络营销整合推广
  • wordpress更改语言沈阳seo优化
  • wordpress免费网站世界大学排名
  • 做网站的属于什么专业?百度爱采购竞价推广
  • 网站建设一年多少恰东莞网站到首页排名