c++ ffmpeg mp4 转 ts
时间: 2024-10-24 14:18:22 浏览: 35
在C++中利用FFmpeg库来转换MP4为TS(Transport Stream)文件,可以编写一个简单的命令行调用程序。这里我们不直接提供完整的C++代码,因为FFmpeg通常作为命令行工具使用,但在C++中可以通过`system()`函数执行外部命令。
首先,确保你的项目包含了FFmpeg的编译链接。以下是如何通过`system()`函数调用FFmpeg命令的例子:
```cpp
#include <cstdlib>
int main() {
std::string command = "ffmpeg -i input.mp4 -codec copy -bsf:a aac_adtstoasc -f mpegts output.ts";
// 注意替换input.mp4和output.ts为你实际的输入和输出文件路径
std::system(command.c_str());
return 0;
}
```
这段代码会运行FFmpeg命令,将`input.mp4`转换为TS格式并保存为`output.ts`。`-codec copy`选项用于保留原始编码,`-bsf:a aac_adtstoasc`确保音频编码适配TS格式,`-f mpegts`指定输出格式为TS。
相关问题
ffmpeg dav转mp4 cpp示例
以下是使用 FFmpeg C++ API 将 DAV 文件转换为 MP4 文件的示例代码:
```cpp
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <fstream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
using namespace std;
int main(int argc, char* argv[]) {
if (argc < 3) {
cout << "Usage: " << argv[0] << " input.dav output.mp4" << endl;
return 1;
}
char* input_file = argv[1];
char* output_file = argv[2];
av_register_all();
avcodec_register_all();
AVFormatContext* input_format_ctx = NULL;
AVFormatContext* output_format_ctx = NULL;
if (avformat_open_input(&input_format_ctx, input_file, NULL, NULL) != 0) {
cout << "Error: could not open input file." << endl;
return 1;
}
if (avformat_find_stream_info(input_format_ctx, NULL) < 0) {
cout << "Error: could not find stream information." << endl;
return 1;
}
if (avformat_alloc_output_context2(&output_format_ctx, NULL, NULL, output_file) < 0) {
cout << "Error: could not allocate output context." << endl;
return 1;
}
AVCodec* codec = NULL;
AVCodecParameters* codec_params = NULL;
int video_stream_index = -1;
for (int i = 0; i < input_format_ctx->nb_streams; i++) {
AVStream* stream = input_format_ctx->streams[i];
codec_params = stream->codecpar;
codec = avcodec_find_decoder(codec_params->codec_id);
if (codec == NULL) {
continue;
}
if (codec_params->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_index = i;
break;
}
}
if (video_stream_index == -1) {
cout << "Error: could not find video stream." << endl;
return 1;
}
AVStream* input_stream = input_format_ctx->streams[video_stream_index];
AVStream* output_stream = avformat_new_stream(output_format_ctx, codec);
if (output_stream == NULL) {
cout << "Error: could not create output stream." << endl;
return 1;
}
if (avcodec_parameters_copy(output_stream->codecpar, codec_params) < 0) {
cout << "Error: could not copy codec parameters." << endl;
return 1;
}
AVCodecContext* codec_ctx = avcodec_alloc_context3(codec);
if (codec_ctx == NULL) {
cout << "Error: could not allocate codec context." << endl;
return 1;
}
if (avcodec_parameters_to_context(codec_ctx, codec_params) < 0) {
cout << "Error: could not convert codec parameters to codec context." << endl;
return 1;
}
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
cout << "Error: could not open codec." << endl;
return 1;
}
av_dump_format(input_format_ctx, 0, input_file, 0);
av_dump_format(output_format_ctx, 0, output_file, 1);
AVPacket packet;
int frame_cnt = 0;
if (avformat_write_header(output_format_ctx, NULL) < 0) {
cout << "Error: could not write output file header." << endl;
return 1;
}
while (av_read_frame(input_format_ctx, &packet) >= 0) {
if (packet.stream_index != video_stream_index) {
av_packet_unref(&packet);
continue;
}
AVFrame* frame = av_frame_alloc();
int ret = avcodec_send_packet(codec_ctx, &packet);
if (ret < 0) {
av_packet_unref(&packet);
av_frame_free(&frame);
continue;
}
while (ret >= 0) {
ret = avcodec_receive_frame(codec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
av_frame_free(&frame);
break;
}
sws_scale(sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_YUV420P, 0, NULL, NULL, NULL), frame->data, frame->linesize, 0, codec_ctx->height, frame->data, frame->linesize);
frame_cnt++;
frame->pts = frame_cnt * input_stream->time_base.den / av_rescale_q(1, AV_TIME_BASE_Q, input_stream->time_base);
frame->pkt_dts = frame->pts;
av_packet_unref(&packet);
av_init_packet(&packet);
if (avcodec_send_frame(codec_ctx, frame) < 0) {
av_frame_free(&frame);
continue;
}
while (1) {
ret = avcodec_receive_packet(codec_ctx, &packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
av_packet_rescale_ts(&packet, codec_ctx->time_base, output_stream->time_base);
packet.stream_index = output_stream->index;
if (av_write_frame(output_format_ctx, &packet) < 0) {
av_packet_unref(&packet);
continue;
}
av_packet_unref(&packet);
}
av_frame_free(&frame);
}
}
if (av_write_trailer(output_format_ctx) < 0) {
cout << "Error: could not write output file trailer." << endl;
return 1;
}
avformat_close_input(&input_format_ctx);
avformat_free_context(input_format_ctx);
if (!(output_format_ctx->flags & AVFMT_NOFILE)) {
avio_closep(&output_format_ctx->pb);
}
avformat_free_context(output_format_ctx);
return 0;
}
```
其中,使用 `avformat_open_input` 打开输入文件, `avformat_find_stream_info` 查找流信息, `avformat_alloc_output_context2` 分配输出上下文, `avcodec_find_decoder` 查找解码器, `avcodec_open2` 打开解码器, `av_read_frame` 读取帧, `avcodec_send_packet` 发送数据包, `avcodec_receive_frame` 接收帧, `sws_scale` 进行图像缩放, `avcodec_send_frame` 发送帧, `avcodec_receive_packet` 接收数据包, `av_write_frame` 写入数据帧, `av_write_trailer` 写入尾帧, `avformat_close_input` 关闭输入文件, `avformat_free_context` 释放上下文等函数完成转换任务。
使用时,需要将 DAV 文件和 MP4 文件的路径传入程序作为参数,例如:
```bash
./dav2mp4 input.dav output.mp4
```
C++ffmpeg视频编码
以下是使用C++和FFmpeg进行视频编码的基本步骤:
1.导入FFmpeg库并初始化:
```c++
#include <iostream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
int main(int argc, char* argv[]) {
av_register_all();
avcodec_register_all();
avformat_network_init();
return 0;
}
```
2.打开输入文件并获取输入格式:
```c++
AVFormatContext* inputFormatContext = NULL;
if (avformat_open_input(&inputFormatContext, inputFileName, NULL, NULL) != 0) {
std::cout << "Could not open input file " << inputFileName << std::endl;
return -1;
}
if (avformat_find_stream_info(inputFormatContext, NULL) < 0) {
std::cout << "Could not find stream information" << std::endl;
return -1;
}
```
3.查找视频流并获取编解码器:
```c++
AVCodec* codec = NULL;
int videoStreamIndex = -1;
for (int i = 0; i < inputFormatContext->nb_streams; i++) {
if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
codec = avcodec_find_decoder(inputFormatContext->streams[i]->codecpar->codec_id);
if (codec == NULL) {
std::cout << "Unsupported codec!" << std::endl;
return -1;
}
break;
}
}
if (videoStreamIndex == -1) {
std::cout << "Could not find video stream!" << std::endl;
return -1;
}
AVCodecContext* codecContext = avcodec_alloc_context3(codec);
if (codecContext == NULL) {
std::cout << "Could not allocate codec context!" << std::endl;
return -1;
}
if (avcodec_parameters_to_context(codecContext, inputFormatContext->streams[videoStreamIndex]->codecpar) < 0) {
std::cout << "Could not copy codec parameters to codec context!" << std::endl;
return -1;
}
if (avcodec_open2(codecContext, codec, NULL) < 0) {
std::cout << "Could not open codec!" << std::endl;
return -1;
}
```
4.创建输出文件并获取输出格式:
```c++
AVFormatContext* outputFormatContext = NULL;
if (avformat_alloc_output_context2(&outputFormatContext, NULL, NULL, outputFileName) < 0) {
std::cout << "Could not allocate output format context!" << std::endl;
return -1;
}
AVOutputFormat* outputFormat = outputFormatContext->oformat;
if (outputFormat->video_codec == AV_CODEC_ID_NONE) {
std::cout << "Could not find suitable video codec!" << std::endl;
return -1;
}
AVStream* outputStream = avformat_new_stream(outputFormatContext, NULL);
if (outputStream == NULL) {
std::cout << "Could not allocate output stream!" << std::endl;
return -1;
}
AVCodecContext* outputCodecContext = avcodec_alloc_context3(NULL);
if (outputCodecContext == NULL) {
std::cout << "Could not allocate output codec context!" << std::endl;
return -1;
}
outputCodecContext->codec_id = outputFormat->video_codec;
outputCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
outputCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
outputCodecContext->width = codecContext->width;
outputCodecContext->height = codecContext->height;
outputCodecContext->time_base = codecContext->time_base;
outputCodecContext->bit_rate = 400000;
outputCodecContext->gop_size = 10;
outputCodecContext->max_b_frames = 1;
if (outputFormatContext->oformat->flags & AVFMT_GLOBALHEADER) {
outputCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
if (avcodec_open2(outputCodecContext, NULL, NULL) < 0) {
std::cout << "Could not open output codec!" << std::endl;
return -1;
}
if (avcodec_parameters_from_context(outputStream->codecpar, outputCodecContext) < 0) {
std::cout << "Could not copy codec parameters to output stream!" << std::endl;
return -1;
}
```
5.创建SwsContext并分配缓冲区:
```c++
SwsContext* swsContext = sws_getContext(codecContext->width, codecContext->height, codecContext->pix_fmt, outputCodecContext->width, outputCodecContext->height, outputCodecContext->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
if (swsContext == NULL) {
std::cout << "Could not create SwsContext!" << std::endl;
return -1;
}
AVFrame* inputFrame = av_frame_alloc();
if (inputFrame == NULL) {
std::cout << "Could not allocate input frame!" << std::endl;
return -1;
}
AVFrame* outputFrame = av_frame_alloc();
if (outputFrame == NULL) {
std::cout << "Could not allocate output frame!" << std::endl;
return -1;
}
outputFrame->format = outputCodecContext->pix_fmt;
outputFrame->width = outputCodecContext->width;
outputFrame->height = outputCodecContext->height;
if (av_frame_get_buffer(outputFrame, 32) < 0) {
std::cout << "Could not allocate output frame buffer!" << std::endl;
return -1;
}
```
6.读取输入文件并编码输出文件:
```c++
AVPacket packet;
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
while (av_read_frame(inputFormatContext, &packet) >= 0) {
if (packet.stream_index == videoStreamIndex) {
if (avcodec_send_packet(codecContext, &packet) < 0) {
std::cout << "Error sending a packet for decoding!" << std::endl;
break;
}
while (avcodec_receive_frame(codecContext, inputFrame) == 0) {
sws_scale(swsContext, inputFrame->data, inputFrame->linesize, 0, codecContext->height, outputFrame->data, outputFrame->linesize);
outputFrame->pts = av_rescale_q(inputFrame->pts, codecContext->time_base, outputCodecContext->time_base);
if (avcodec_send_frame(outputCodecContext, outputFrame) < 0) {
std::cout << "Error sending a frame for encoding!" << std::endl;
break;
}
while (avcodec_receive_packet(outputCodecContext, &packet) == 0) {
av_packet_rescale_ts(&packet, outputCodecContext->time_base, outputStream->time_base);
packet.stream_index = outputStream->index;
if (av_interleaved_write_frame(outputFormatContext, &packet) < 0) {
std::cout << "Error while writing output packet!" << std::endl;
break;
}
av_packet_unref(&packet);
}
}
}
av_packet_unref(&packet);
}
```
7.释放资源:
```c++
avformat_close_input(&inputFormatContext);
avformat_free_context(inputFormatContext);
avcodec_free_context(&codecContext);
avformat_free_context(outputFormatContext);
avcodec_free_context(&outputCodecContext);
av_frame_free(&inputFrame);
av_frame_free(&outputFrame);
sws_freeContext(swsContext);
```
阅读全文