【FFmpeg】多媒体文件处理

2023-11-13

在这里插入图片描述

1、ffmpeg的日志打印

include<libavutil/log.h>
av_log_set_level(AV_LOG_DEBUG)
av_log(NULL, AV_LOG_INFO,"...");


AV_LOG_ERROR
AV_LOG_WARNING
AV_LOG_INFO
AV_LOG_DEBUG

具体例子:
ff_log.c :

#include<stdio.h>
#include<libavutil/log.h>

int main(int argc, char *argv[]){
	av_log_set_level(AV_LOG_INFO);
	av_log(NULL, AV_LOG_DEBUG, "Hello world:%s%d\n","aaa",10);
	av_log(NULL, AV_LOG_INFO, "Hello world INFO!\n");
	av_log(NULL, AV_LOG_ERROR, "Hello world ERROR!\n");
	return 0;
}
vim ff_log.c 


clang -g -o ff_log ff_log.c -I/usr/local/ffmpeg/include -L/usr/local/ffmpeg/lib -lavutil
或者:
clang -g -o ff_log ff_log.c `pkg-config --cflags --libs libavutil`


./ff_log
Hello world INFO!
Hello world ERROR!

mac中,使用第二种方式编译链接时报错如下:

Package libavutil was not found in the pkg-config search path.
Perhaps you should add the directory containing `libavutil.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libavutil' found
ff_log.c:2:9: fatal error: 'libavutil/log.h' file not found
#include<libavutil/log.h>
        ^~~~~~~~~~~~~~~~~
1 error generated.

解决办法如下:

vim ~/.bash_profile 
source ~/.bash_profile

在文件中添加最后两行export:

# HomeBrew
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/sbin:$PATH"
export PATH="/usr/local/ffmpeg/bin:$PATH"


export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/ffmpeg/lib/pkgconfig
export LD_LIBRARY_PATH=$LIB_LIBRARY_PATH:/usr/local/ffmpeg/lib
# HomeBrew END

2、ffmpeg文件与目录操作

文件删除与重命名:

avpriv_io_delete()

avpriv_io_move()

ffmpeg_file.c:

#include<stdio.h>
#include<libavutil/log.h>
#include<libavformat/avformat.h>

int main(int argc, char* argv[]){
	
	int ret;

	// 重命名文件
	ret = avpriv_io_move("111.txt", "222.txt");
	if(ret < 0){
		av_log(NULL, AV_LOG_ERROR, "Failed to rename\n");
		return -1;
	}
	av_log(NULL, AV_LOG_INFO, "Success to rename\n");

	// 删除文件
	// url
	ret = avpriv_io_delete("./mytestfile.txt");
	if(ret < 0){
		av_log(NULL, AV_LOG_ERROR, "Fail to delete file mytestfile.txt\n");
		return -1;
	}
	av_log(NULL, AV_LOG_INFO, "Success to delete mytestfile.txt\n");
	return 0;
}

编译运行:

ffmpeg_file % clang -g -o ffmpeg_mov_del ffmpeg_file.c `pkg-config --cflags --libs libavutil --libs libavformat`
./ffmpeg_mov_del

3、ffmpeg操作目录

avio_open_dir()

avio_read_dir()

avio_close_dir()

AVIODirContext	操作目录的上下文

AVIODirEntry	目录项 存放文件名,文件大小等信息 
实现一个简单的ls命令:

ffmpeg_file_list.c:

#include<libavutil/log.h>
#include<libavformat/avformat.h>

int main(int argc, char* argv[]){
	int ret;
	AVIODirContext *ctx = NULL;
	AVIODirEntry *entry = NULL;	

	av_log_set_level(AV_LOG_INFO);
	ret = avio_open_dir(&ctx, "./", NULL);
	if(ret < 0){
		av_log(NULL, AV_LOG_ERROR, "Cannot open dir:%s\n", av_err2str(ret));
	}
	
	while(1){
		ret = avio_read_dir(ctx, &entry);
		if(ret < 0){
			av_log(NULL, AV_LOG_ERROR, "Cannot read dir:%s\n", av_err2str(ret));
			goto __fail;
		}
		if(!entry){
			break;
		}

		av_log(NULL, AV_LOG_INFO, "%12"PRId64" %s \n", entry->size, entry->name);
		avio_free_directory_entry(&entry);
	}

__fail:
	avio_close_dir(&ctx);
	return 0;
}

编译运行:

clang -g -o ffmpeg_list ffmpeg_file_list.c `pkg-config --cflags --libs libavutil --libs libavformat`

./ffmpeg_list
        6148 .DS_Store 
          96 ffmpeg_del.dSYM 
       49848 ffmpeg_del 
           0 222.txt 
         536 ffmpeg_file.c 
         689 ffmpeg_file_list.c 
       50408 ffmpeg_list 
          96 ffmpeg_list.dSYM 

4、ffmpeg处理流数据

4.1 概念

多媒体文件其实就是容器,在容器中有很多流(流Stream/轨Track);
从流中读出的数据称为包;
在一个包中包含着一个或多个帧。

4.2 重要结构体
AVFormatContext	格式上下文
AVStream	流
AVPacket	包
4.3 FFmpeg操作流数据的基本步骤
  • 解复用 打开容器
  • 获取流 拿到容器中的流
  • 读数据包 从流中取出一个个数据包
  • 释放资源 做完逻辑操作后释放资源

5、实战

5.1 打印音视频信息

新版本ffmpeg中弃用了这个函数:av_register_all() 所有ffmpeg代码开始前都得先调用此api

avformat_open_input()avformat_close_input()	成对出现
打开多媒体文件,输出结构体AVFormatContext,借助这个格式上下文获取参数信息,最后关闭多媒体文件

av_dump_format() 打印多媒体文件的meta信息
vim mediainfo.c

输入内容如下:
#include<libavutil/log.h>
#include<libavformat/avformat.h>

int main(int argc, char* argv[])
{
	int ret;
	AVFormatContext *fmt_ctx = NULL; // 定义上下文

	av_log_set_level(AV_LOG_INFO); // 设置日志级别
	
	//av_register_all(); // 注册多媒体格式协议,新版本ffmpeg中弃用了这个函数
	
	avformat_open_input(&fmt_ctx,"./test.mp4", NULL, NULL); // 打开多媒体文件 第3个参数NULL:指定输入格式,默认文件后缀名
	if(ret<0){ // 如果打开失败,就打印日志信息并退出
		av_log(NULL, AV_LOG_ERROR, "Can't open file: %s\n",av_err2str(ret));
		return -1;
	}

	// 打开成功,进入这里,执行下面的逻辑
	av_dump_format(fmt_ctx, 0, "./test.mp4", 0); // 打印多诶题文件的meta信息  no2填0记住即可;no4填0表示是输入文件,如果输出文件填1

	avformat_close_input(&fmt_ctx); // 关闭多媒体文件

	return 0;
}

编译:

clang -g -o mediainfo mediainfo.c `pkg-config --cflags --libs libavutil --libs libavformat`
有个警告:
mediainfo.c:11:2: warning: 'avcodec_register_all' is deprecated

执行:

 ./mediainfo
 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from './test.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    comment         : vid:v0300fa50000c08eg9qcf5vi1hpn9gfg
    encoder         : Lavf58.20.100
  Duration: 00:00:10.94, bitrate: N/A
    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 720x1280, 1174 kb/s, SAR 1:1 DAR 9:16, 29.82 fps, 30 tbr, 15360 tbn (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, 2 channels, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

5.2 抽取音频数据

av_init_packet() 初始化的数据包

av_find_best_stream() 找到想要拿到的最好的一路流

av_read_frame() / av_packet_unref()  read获取流中的数据包,做一些处理,本例是写入aac文件; 
每次用read读取数据包,会增加引用计数,使用unref可以释放引用计数,让ffmpeg自动释放包,避免内存泄漏

使用课程中的代码抽取音频:

#include<stdio.h>
#include<libavutil/log.h>
#include<libavformat/avformat.h>
#include <libavformat/avio.h>

#define ADTS_HEADER_LEN  7;

void adts_header(char *szAdtsHeader, int dataLen){

    int audio_object_type = 2; 
    int sampling_frequency_index = 7;
    int channel_config = 2;

    int adtsLen = dataLen + 7;

    szAdtsHeader[0] = 0xff;         //syncword:0xfff                          高8bits
    szAdtsHeader[1] = 0xf0;         //syncword:0xfff                          低4bits
    szAdtsHeader[1] |= (0 << 3);    //MPEG Version:0 for MPEG-4,1 for MPEG-2  1bit
    szAdtsHeader[1] |= (0 << 1);    //Layer:0                                 2bits 
    szAdtsHeader[1] |= 1;           //protection absent:1                     1bit

    szAdtsHeader[2] = (audio_object_type - 1)<<6;            //profile:audio_object_type - 1                      2bits
    szAdtsHeader[2] |= (sampling_frequency_index & 0x0f)<<2; //sampling frequency index:sampling_frequency_index  4bits 
    szAdtsHeader[2] |= (0 << 1);                             //private bit:0                                      1bit
    szAdtsHeader[2] |= (channel_config & 0x04)>>2;           //channel configuration:channel_config               高1bit

    szAdtsHeader[3] = (channel_config & 0x03)<<6;     //channel configuration:channel_config      低2bits
    szAdtsHeader[3] |= (0 << 5);                      //original:0                               1bit
    szAdtsHeader[3] |= (0 << 4);                      //home:0                                   1bit
    szAdtsHeader[3] |= (0 << 3);                      //copyright id bit:0                       1bit  
    szAdtsHeader[3] |= (0 << 2);                      //copyright id start:0                     1bit
    szAdtsHeader[3] |= ((adtsLen & 0x1800) >> 11);           //frame length:value   高2bits

    szAdtsHeader[4] = (uint8_t)((adtsLen & 0x7f8) >> 3);     //frame length:value    中间8bits
    szAdtsHeader[5] = (uint8_t)((adtsLen & 0x7) << 5);       //frame length:value    低3bits
    szAdtsHeader[5] |= 0x1f;                                 //buffer fullness:0x7ff 高5bits
    szAdtsHeader[6] = 0xfc;
}

int main(int argc, char* argv[])
{
	int ret;
	int len;
	int audio_index;
	char* src = NULL;
	char* dst = NULL;
	AVPacket pkt;
	AVFormatContext *fmt_ctx = NULL; // 定义上下文

	av_log_set_level(AV_LOG_INFO); // 设置日志级别

	// 1:read two params from console
	if(argc<3){
		av_log(NULL, AV_LOG_ERROR, "the count of params should be 3.\n");
		return -1;
	}	
	
	src = argv[1];
	dst = argv[2];
	if(!src || !dst){
		av_log(NULL, AV_LOG_ERROR, "src or dst is null!\n");
		return -1;
	}

	ret = avformat_open_input(&fmt_ctx, src, NULL, NULL); // 打开多媒文件
	if(ret<0){ // 如果打开失败,就打印日志信息并退出
		av_log(NULL, AV_LOG_ERROR, "Can't open file: %s\n",av_err2str(ret));
		return -1;
	}

	// 打开成功,进入这里,执行下面的逻辑
	FILE* dst_fd = fopen(dst, "wb");
	if(!dst_fd){
		av_log(NULL, AV_LOG_ERROR, "Can't open out file!\n");
		avformat_close_input(&fmt_ctx); // 此时输入流已打开了,需要关闭
		return -1;
	}
	av_dump_format(fmt_ctx, 0, src, 0); // 打印多诶题文件的meta信息  no2填0记住即可;no4填0表示是输入文件,如果输出文件填1

	// 2:get stream
	ret = av_find_best_stream(fmt_ctx/*上下文*/, 
				AVMEDIA_TYPE_AUDIO/*想要抽取的流的类型,音频流*/, 
				-1/*流的索引号,此处不知道*/,
				-1/*音频流对应的视频流的索引号,此处不关心*/,
				NULL/*流的编解码器,不关心*/,
				0/*flag,一些标准,不关心*/ );		

	if(ret<0){
		av_log(NULL, AV_LOG_ERROR, "Cannot find the best stream!\n");
		avformat_close_input(&fmt_ctx);
		fclose(dst_fd);
		return -1;
	}
	
	audio_index = ret;

	av_init_packet(&pkt);

	while(av_read_frame(fmt_ctx, &pkt) >= 0){
		if(pkt.stream_index == audio_index){

			// 生成adts头,并写入文件
			char adts_header_buf[7];
            adts_header(adts_header_buf, pkt.size);
            fwrite(adts_header_buf, 1, 7, dst_fd);

			// 3:write audio data to aac file
			len = fwrite(pkt.data/*待存内容*/, 1/*逐个字节写入*/, pkt.size/*写入多少个字节*/, dst_fd/*写入哪个文件*/);
		
			if(len != pkt.size){
				av_log(NULL, AV_LOG_WARNING, "warning, length of data is not equal size of pkt!");
			}
		}
		av_packet_unref(&pkt); // 不管是不是最好的那路流,到这里都要释放
	}
	
	avformat_close_input(&fmt_ctx);
	if(dst_fd){
		fclose(dst_fd);
	}

	avformat_close_input(&fmt_ctx); // 关闭多媒体文件

	return 0;
}

为什么我使用课程中的程序无法成功抽取AAC音频?
利用ffmpeg中的api实现音频抽取

#include <stdio.h>
#include <libavutil/log.h>
#include <libavformat/avio.h>
#include <libavformat/avformat.h>

#define ERROR_STR_SIZE 1024

int main(int argc, char *argv[])
{
    int err_code;
    char errors[1024];

    char *src_filename = NULL;
    char *dst_filename = NULL;

    FILE *dst_fd = NULL;

    int audio_stream_index = -1;
    int len;

    AVFormatContext *ofmt_ctx = NULL;
    AVOutputFormat *output_fmt = NULL;

    AVStream *in_stream = NULL;
    AVStream *out_stream = NULL;

    AVFormatContext *fmt_ctx = NULL;
    //AVFrame *frame = NULL;
    AVPacket pkt;

    av_log_set_level(AV_LOG_DEBUG);

    if(argc < 3){
        av_log(NULL, AV_LOG_DEBUG, "the count of parameters should be more than three!\n");
        return -1;
    }

    src_filename = argv[1];
    dst_filename = argv[2];

    if(src_filename == NULL || dst_filename == NULL){
        av_log(NULL, AV_LOG_DEBUG, "src or dts file is null, plz check them!\n");
        return -1;
    }

    /*register all formats and codec*/
    av_register_all();

    /*open input media file, and allocate format context*/
    if((err_code = avformat_open_input(&fmt_ctx, src_filename, NULL, NULL)) < 0){
        av_strerror(err_code, errors, 1024);
        av_log(NULL, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n",
               src_filename,
               err_code,
               errors);
        return -1;
    }

    /*retrieve audio stream*/
    if((err_code = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
        av_strerror(err_code, errors, 1024);
        av_log(NULL, AV_LOG_DEBUG, "failed to find stream information: %s, %d(%s)\n",
               src_filename,
               err_code,
               errors);
        return -1;
    }

    /*dump input information*/
    av_dump_format(fmt_ctx, 0, src_filename, 0);

    in_stream = fmt_ctx->streams[1];
    AVCodecParameters *in_codecpar = in_stream->codecpar;
    if(in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO){
        av_log(NULL, AV_LOG_ERROR, "The Codec type is invalid!\n");
        exit(1);
    }

    //out file
    ofmt_ctx = avformat_alloc_context();
    output_fmt = av_guess_format(NULL, dst_filename, NULL);
    if(!output_fmt){
        av_log(NULL, AV_LOG_DEBUG, "Cloud not guess file format \n");
        exit(1);
    }

    ofmt_ctx->oformat = output_fmt;

    out_stream = avformat_new_stream(ofmt_ctx, NULL);
    if(!out_stream){
        av_log(NULL, AV_LOG_DEBUG, "Failed to create out stream!\n");
        exit(1);
    }

    if(fmt_ctx->nb_streams<2){
        av_log(NULL, AV_LOG_ERROR, "the number of stream is too less!\n");
        exit(1);
    }


    if((err_code = avcodec_parameters_copy(out_stream->codecpar, in_codecpar)) < 0 ){
        av_strerror(err_code, errors, ERROR_STR_SIZE);
        av_log(NULL, AV_LOG_ERROR,
               "Failed to copy codec parameter, %d(%s)\n",
               err_code, errors);
    }

    out_stream->codecpar->codec_tag = 0;

    if((err_code = avio_open(&ofmt_ctx->pb, dst_filename, AVIO_FLAG_WRITE)) < 0) {
        av_strerror(err_code, errors, 1024);
        av_log(NULL, AV_LOG_DEBUG, "Could not open file %s, %d(%s)\n",
               dst_filename,
               err_code,
               errors);
        exit(1);
    }

    /*
    dst_fd = fopen(dst_filename, "wb");
    if (!dst_fd) {
        av_log(NULL, AV_LOG_DEBUG, "Could not open destination file %s\n", dst_filename);
        return -1;
    }
    */


    /*dump output information*/
    av_dump_format(ofmt_ctx, 0, dst_filename, 1);

    /*
    frame = av_frame_alloc();
    if(!frame){
        av_log(NULL, AV_LOG_DEBUG, "Could not allocate frame\n");
        return AVERROR(ENOMEM);
    }
    */

    /*initialize packet*/
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;

    /*find best audio stream*/
    audio_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    if(audio_stream_index < 0){
        av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n",
               av_get_media_type_string(AVMEDIA_TYPE_AUDIO),
               src_filename);
        return AVERROR(EINVAL);
    }

    if (avformat_write_header(ofmt_ctx, NULL) < 0) {
        av_log(NULL, AV_LOG_DEBUG, "Error occurred when opening output file");
        exit(1);
    }

    /*read frames from media file*/
    while(av_read_frame(fmt_ctx, &pkt) >=0 ){
        if(pkt.stream_index == audio_stream_index){
            pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
            pkt.dts = pkt.pts;
            pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
            pkt.pos = -1;
            pkt.stream_index = 0;
            av_interleaved_write_frame(ofmt_ctx, &pkt);
            av_packet_unref(&pkt);
        }
    }

    av_write_trailer(ofmt_ctx);

    /*close input media file*/
    avformat_close_input(&fmt_ctx);
    if(dst_fd) {
        fclose(dst_fd);
    }

    avio_close(ofmt_ctx->pb);

    return 0;
}

5.3 抽取视频数据

Start code 在每一帧前面的关键字
将一帧一帧数据区分开:可以在每一帧前面加该帧的长度;也可以在每一帧前面加关键字或特征码;

SPS / PPS 解码的视频帧的参数 每个关键帧前加

codec->extradata 获取SPS和PPS是从编码器的扩展数据中获取,而非从数据包中

目的:
在每一个帧的前面增加Start Code特征码;
在每个关键帧前面增加SPS和PPS,同时也增加了特征码。

5.4 将MP4转成FLV格式

avformat_alloc_output_context2() / avformat_free_context()

avformat_new_stream()

avcodec_parameters_copy()



avformat_write_header() 生成多媒体文件头

av_write_frame() / av_interleaved_write_frame()

av_write_trailer()

可以将test.mpt 转换成 test.flv

#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>

static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt, const char *tag)
{
    AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;

    printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
           tag,
           av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
           av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
           av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
           pkt->stream_index);
}

int main(int argc, char **argv)
{
    AVOutputFormat *ofmt = NULL;
    AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
    AVPacket pkt;
    const char *in_filename, *out_filename;
    int ret, i;
    int stream_index = 0;
    int *stream_mapping = NULL;
    int stream_mapping_size = 0;

    if (argc < 3) {
        printf("usage: %s input output\n"
               "API example program to remux a media file with libavformat and libavcodec.\n"
               "The output format is guessed according to the file extension.\n"
               "\n", argv[0]);
        return 1;
    }

    in_filename  = argv[1];
    out_filename = argv[2];


    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
        fprintf(stderr, "Could not open input file '%s'", in_filename);
        goto end;
    }

    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        fprintf(stderr, "Failed to retrieve input stream information");
        goto end;
    }

    av_dump_format(ifmt_ctx, 0, in_filename, 0);

    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
    if (!ofmt_ctx) {
        fprintf(stderr, "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }

    stream_mapping_size = ifmt_ctx->nb_streams;
    stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
    if (!stream_mapping) {
        ret = AVERROR(ENOMEM);
        goto end;
    }

    ofmt = ofmt_ctx->oformat;

    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        AVStream *out_stream;
        AVStream *in_stream = ifmt_ctx->streams[i];
        AVCodecParameters *in_codecpar = in_stream->codecpar;

        if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
            stream_mapping[i] = -1;
            continue;
        }

        stream_mapping[i] = stream_index++;

        out_stream = avformat_new_stream(ofmt_ctx, NULL);
        if (!out_stream) {
            fprintf(stderr, "Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            goto end;
        }

        ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
        if (ret < 0) {
            fprintf(stderr, "Failed to copy codec parameters\n");
            goto end;
        }
        out_stream->codecpar->codec_tag = 0;
    }
    av_dump_format(ofmt_ctx, 0, out_filename, 1);

    if (!(ofmt->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Could not open output file '%s'", out_filename);
            goto end;
        }
    }

    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error occurred when write_header\n");
        goto end;
    }

    while (1) {
        AVStream *in_stream, *out_stream;

        ret = av_read_frame(ifmt_ctx, &pkt);
        if (ret < 0)
            break;

        in_stream  = ifmt_ctx->streams[pkt.stream_index];
        if (pkt.stream_index >= stream_mapping_size ||
            stream_mapping[pkt.stream_index] < 0) {
            av_packet_unref(&pkt);
            continue;
        }

        pkt.stream_index = stream_mapping[pkt.stream_index];
        out_stream = ofmt_ctx->streams[pkt.stream_index];
        log_packet(ifmt_ctx, &pkt, "in");

        /* copy packet */
        pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
        pkt.pos = -1;
        log_packet(ofmt_ctx, &pkt, "out");

        ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
        if (ret < 0) {
            fprintf(stderr, "Error muxing packet\n");
            break;
        }
        av_packet_unref(&pkt);
    }

    av_write_trailer(ofmt_ctx);
end:

    avformat_close_input(&ifmt_ctx);

    /* close output */
    if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
        avio_closep(&ofmt_ctx->pb);
    avformat_free_context(ofmt_ctx);

    av_freep(&stream_mapping);

    if (ret < 0 && ret != AVERROR_EOF) {
        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
        return 1;
    }

    return 0;
}

5.5 从MP4截取一段视频

av_seek_frame()

截取时,./cut 5 15 srcVideo toVideo将视频srcVideo的第5秒到第15秒存入toVideo

#include <stdlib.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>

static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt, const char *tag)
{
    AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;

    printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
           tag,
           av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
           av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
           av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
           pkt->stream_index);
}

int cut_video(double from_seconds, double end_seconds, const char* in_filename, const char* out_filename) {
    AVOutputFormat *ofmt = NULL;
    AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
    AVPacket pkt;
    int ret, i;

    //av_register_all();

    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
        fprintf(stderr, "Could not open input file '%s'", in_filename);
        goto end;
    }

    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        fprintf(stderr, "Failed to retrieve input stream information");
        goto end;
    }

    av_dump_format(ifmt_ctx, 0, in_filename, 0);

    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
    if (!ofmt_ctx) {
        fprintf(stderr, "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }

    ofmt = ofmt_ctx->oformat;

    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        AVStream *in_stream = ifmt_ctx->streams[i];
        AVStream *out_stream = avformat_new_stream(ofmt_ctx, NULL);
        if (!out_stream) {
            fprintf(stderr, "Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            goto end;
        }

        ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
        if (ret < 0) {
            fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
            goto end;
        }
        out_stream->codecpar->codec_tag = 0;
    }
    av_dump_format(ofmt_ctx, 0, out_filename, 1);

    if (!(ofmt->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Could not open output file '%s'", out_filename);
            goto end;
        }
    }

    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error occurred when opening output file\n");
        goto end;
    }

    ret = av_seek_frame(ifmt_ctx, -1, from_seconds*AV_TIME_BASE, AVSEEK_FLAG_ANY);
    if (ret < 0) {
        fprintf(stderr, "Error seek\n");
        goto end;
    }

    int64_t *dts_start_from = malloc(sizeof(int64_t) * ifmt_ctx->nb_streams);
    memset(dts_start_from, 0, sizeof(int64_t) * ifmt_ctx->nb_streams);
    int64_t *pts_start_from = malloc(sizeof(int64_t) * ifmt_ctx->nb_streams);
    memset(pts_start_from, 0, sizeof(int64_t) * ifmt_ctx->nb_streams);

    while (1) {
        AVStream *in_stream, *out_stream;

        ret = av_read_frame(ifmt_ctx, &pkt);
        if (ret < 0)
            break;

        in_stream  = ifmt_ctx->streams[pkt.stream_index];
        out_stream = ofmt_ctx->streams[pkt.stream_index];

        log_packet(ifmt_ctx, &pkt, "in");

        if (av_q2d(in_stream->time_base) * pkt.pts > end_seconds) {
            av_packet_unref(&pkt);
            break;
        }

        if (dts_start_from[pkt.stream_index] == 0) {
            dts_start_from[pkt.stream_index] = pkt.dts;
            printf("dts_start_from: %s\n", av_ts2str(dts_start_from[pkt.stream_index]));
        }
        if (pts_start_from[pkt.stream_index] == 0) {
            pts_start_from[pkt.stream_index] = pkt.pts;
            printf("pts_start_from: %s\n", av_ts2str(pts_start_from[pkt.stream_index]));
        }

        /* copy packet */
        pkt.pts = av_rescale_q_rnd(pkt.pts - pts_start_from[pkt.stream_index], in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        pkt.dts = av_rescale_q_rnd(pkt.dts - dts_start_from[pkt.stream_index], in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        if (pkt.pts < 0) {
            pkt.pts = 0;
        }
        if (pkt.dts < 0) {
            pkt.dts = 0;
        }
        if(pkt.pts < pkt.dts) continue;
        pkt.duration = (int)av_rescale_q((int64_t)pkt.duration, in_stream->time_base, out_stream->time_base);
        pkt.pos = -1;
        log_packet(ofmt_ctx, &pkt, "out");
        printf("\n");

        ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
        if (ret < 0) {
            fprintf(stderr, "Error muxing packet\n");
            break;
        }
        av_packet_unref(&pkt);
    }
    free(dts_start_from);
    free(pts_start_from);

    av_write_trailer(ofmt_ctx);
end:

    avformat_close_input(&ifmt_ctx);

    /* close output */
    if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
        avio_closep(&ofmt_ctx->pb);
    avformat_free_context(ofmt_ctx);

    if (ret < 0 && ret != AVERROR_EOF) {
        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
        return 1;
    }

    return 0;
}

int main(int argc, char *argv[]){
    if(argc < 5){
        fprintf(stderr, "Usage: \
                command startime, endtime, srcfile, outfile");
        return -1;
    }

    double startime = atoi(argv[1]);
    double endtime = atoi(argv[2]);
    cut_video(startime, endtime, argv[3], argv[4]);

    return 0;
}

5.6 一个简单的小咖秀

  • 将两个媒体文件中分别抽取音频与视频

  • 将音频与视频轨合并成一个新文件

  • 对音频与视频轨进行裁剪

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【FFmpeg】多媒体文件处理 的相关文章

  • 如何在Android项目中使用libffmpeg.so?

    我正在尝试在 Android 中创建一个屏幕录制应用程序 为此 我使用 FFmpeg 我已经创建了 libffmpeg so 文件 现在我想在 Android 项目中使用相同的方法来调用它的本机函数 我怎样才能做到这一点 本教程提供了有关此
  • Python 用静态图像将 mp3 转换为 mp4

    我有x文件包含一个列表mp3我想转换的文件mp3文件至mp4文件带有static png photo 似乎这里唯一的方法是使用ffmpeg但我不知道如何实现它 我编写了脚本来接受输入mp3文件夹和一个 png photo 然后它将创建新文件
  • FFmpeg 不适用于 android 10,直接进入 onFailure(String message) 并显示空消息

    我在我的一个项目中使用 FFmpeg 进行视频压缩 在 Android 10 Google Pixel 3a 上 对于发送执行的任何命令 它会直接进入 onFailure String message 并显示空消息 所以我在我的应用程序 g
  • OpenCV VideoWriter 未写入 Output.avi

    我正在尝试编写一段简单的代码来获取视频 裁剪视频并写入输出文件 系统设置 OS Windows 10 Conda Environment Python Version 3 7 OpenCV Version 3 4 2 ffmpeg Vers
  • Windows 上的 ffmpeg-android ndk

    我正在尝试编译 bash 文件 带有 android ndk 的 ffmpeg 我收到如下错误 arm linux androideabi gcc 无法创建可执行文件 C 编译器测试失败 Makefile 2 config mak 没有这样
  • 如何在Mac上使用AVFoundation将图片编码为H264,而不是使用x264

    我正在尝试制作一个 Mac 广播客户端 使用 FFmpeg 但不使用 x264 库编码为 H264 所以基本上 我可以从 AVFoundation 中获取原始帧CMSampleBufferRef or AVPicture 那么有没有一种方法
  • 为什么处理时间随着修剪位置的增加而增加?

    我最近一直在使用ffmpeg修剪一些视频 我注意到随着修剪位置的增加 修剪视频所花费的时间也会增加 即使持续时间相同 5 seconds 下面给出了修剪视频的命令0 to 5秒 处理仅需1秒 ffmpeg y i input mp4 fil
  • 使用 FFMPEG 添加覆盖并最少重新编码

    FFMPEG 对于剪切视频的一部分非常有用 而无需重新编码视频 我知道也可以使用 FFMPEG 添加叠加图像到视频的某个部分 例如从 10 秒到 20 秒 我的问题是 如果我对图像进行叠加 整个视频是否会因此而重新编码 或者只对相关的持续时
  • Node.js - 将数据缓冲到 Ffmpeg

    我使用 Node js 和 Ffmpeg 来创建动画 因为我试图避免第三方 avi mp4 解析器 所以我决定将动画输出为原始 rgb24 数据文件 然后使用一些程序将其转换为 mp4 文件 我发现 Ffmpeg 是免费且开源的 它完全可以
  • 将 ffmpeg 编译为独立二进制文件

    我正在尝试编译ffmpeg作为独立的二进制文件 因为我想在 AWS lambda 中使用它 我可以让事情在我正在编译的服务器上正常工作 但是如果我复制二进制文件并从另一台服务器运行它 我会得到 ffmpeg error while load
  • Android 中的 FFMpeg jni?

    我已经构建了 Bambuser http bambuser com opensource 提供的 FFMPEG 可执行文件和库 所以我设法构建了 Android 可执行文件和库 如何在 Eclipse 项目中链接这些库并从 Java 调用
  • FFmpeg av_read_frame 无法正确读取帧?

    好吧 我已经下载了一些 yuv 格式的原始 UHD 序列 并在 mp4 容器中使用 ffmpeg 对其进行编码 h264 4 4 4 100 质量 25fps 当我使用 ffprobe 找出编码了多少帧时 我得到 600 所以这是 24 秒
  • FFMPEG:将 YUV 数据转储到 AVFrame 结构中

    我正在尝试转储YUV420数据进入AVFrameFFMPEG 的结构 从下面的链接 http ffmpeg org doxygen trunk structAVFrame html http ffmpeg org doxygen trunk
  • 如何将AVFrame转换为glTexImage2D使用的纹理?

    如您所知 AVFrame 有 2 个属性 pFrame gt data pFrame gt linesize 当我从视频 sdcard test mp4 android平台 读取帧后 并将其转换为RGB AVFrame副 img conve
  • 如何使用 ffmpeg av_seek_frame() 在具有帧号的情况下读取任何帧

    int64 t timeBase timeBase int64 t pavStrm gt time base num AV TIME BASE int64 t pavStrm gt time base den int64 t seekTar
  • 有没有更有效的方法通过ffmpeg批量添加水印和加入视频?

    我有这个批处理文件 使用 ffmpeg 在我的视频中添加徽标 然后添加简介 但需要 10 小时到一天的时间 具体取决于我需要添加水印的数量 是否有更有效的方法来实现此目的 视频有时具有不同的分辨率 因此我无法删除到 1280 720 尺寸的
  • ffmpeg 将 m4s 转换为 mp4

    我正在研究 DASH 试图为最终用户优化 QoE 我有一个视频 并使用 ffmpeg 将其编码为不同的比特率 一切都很好 并且可以使用 dash 播放该视频 我想要的是将用户收到的片段合并为一个 m4 并将该 m4 转换为 mp4 我在 f
  • 用PHP+FFMPEG生成随机缩略图

    我正在尝试使用 FFMPEG 和 FFMPEG PHP 扩展从电影中的随机点生成缩略图 我的脚本工作正常 但是需要 20 分钟才能生成 5 10 个缩略图 该脚本通过生成随机数来工作 这些随机数稍后用作帧号 生成的所有数字均在电影帧数之内
  • 如何使用 FFmpeg 连接 MTS 视频并应用过滤器而不重新编码?

    我有一个包含许多 MTS 视频文件的 txt 文件 我想使用将它们全部合并在一起FFmpeg并获取一个大的 MTS 文件 但我想申请fade in and fade out到最后的视频 我可以在不重新编码的情况下做到这一点吗 因为重新编码需
  • 用于 Windows Phone 开发的 FFmpeg

    我在 ASP Net 基于 Web 的应用程序中使用了 FFmpeg 现在我想用它来进行Windows Phone开发 可以使用吗 如果是 那么如何 Windows Phone 7 根本不支持 FFmpeg 而且据我在网上找到的信息 Win

随机推荐

  • gitee同一台电脑使用多个账号的问题

    官方文档 https gitee com help articles 4238 article header0 目录 一 通过 https ssh 协议推拉代码 二 通过 https 推拉代码但是存在多个账号的问题 三 通过 ssh 推拉代
  • python3 判断列表是一个空列表

    python3 判断空列表 python3 有个判断列表是否为空的需求 试了好多方式 比如 a if a is not None COMMAND a if a 0 is None COMMAND 各种乱七八糟的逻辑 老是无法满足 其实很简单
  • Unity3D笔记第三天——GUI

    GUI GUI是Graphical User Interface的缩写 Unity的图形界面系统能容易和快速创建出各种交互界面 与传统的方法 创建GUI对象 标出位置 再写对应的事件函数不同 只需要用很少的代码 就可以把这些工作搞定 原理是
  • 2023企业级数仓建设概要

    一 前言 1 1 背景 无忧搬家数据以前很多都是数仓同学从业务库负责接入数据至ods层 然后就由各个下游分析师取ods贴源层数据然后进行取数计算分析 数仓这边缺乏沉淀公共层数据 从而有以下问题 直接从ods贴源层取数据 业务研发侧一改造则下
  • FastMM内存管理器在使用多线程情况下需要注意的问题。

    FastMM内存管理器在使用多线程情况下需要注意的问题 问题1 注 如果你在Delphi中 只是用TThread类创建线程 不会用到API函数CreateThread创建线程 哪么下面这篇文章你可以完全忽视 当然 如果你耐心的看完了下面这篇
  • 如何编写一份完整的软件测试报告?(进阶版)

    目录 背景 一 什么是测试报告 二 测试报告是给谁看的 三 测试报告应该怎么写 1 测试报告的内容 1 1 工作内容 1 2 软件结果 1 3 展开说说 1 4 你的价值 2 测试报告的结构 2 1 首先呈现出你的结论 2 2 当前遗留问题
  • 伪需求 or 新机遇——2018年DApp分析

    DApp的全称为Decentralized Application 是指去中心化应用或者分布式应用 DApp的出现是区块链应用落地的有益尝试 DApp也被认为是开启了区块链3 0时代的产品 因此 DApp被众多区块链业内人士寄予厚望 尽管2
  • 51单片机使用定时器0定时1ms的配置操作

    51单片机使用定时器0定时1ms的配置操作 51单片机的定时器0结构 配置的定时器0的方式 配置的程序代码内容 51单片机的定时器0结构 51单片机是一种广泛应用于嵌入式系统中的芯片 具有强大的计时和计数功能 其中的定时器0可以用来实现精确
  • 嘴说手画Spark的Bykey操作-groupByKey、reduceByKey、aggregateByKey 和 sortByKey

    之前写过一篇文章分析Spark Shuffle的原理 知道了Shuffle是性能杀手的原因 但在实际业务中 Shuffle操作通常不可避免 毕竟Spark基础的用途就是对大数据进行统计分析 由于数据分布的分散性 导致相同Key的数据汇集到一
  • 只能看到部分局域网计算机,为什么局域网中只能看到部分电脑

    局域网中只能看到部分电脑的原因 那是因为其他电脑没有开启共享模式 也就是来宾账号关闭了 需要在用户组中打开才行 局域网共享设置步骤如下 1 更改不同的计算机名 设置相同的工作组 2 我的电脑右键 管理 计算机管理 本地用户和组 用户 更改管
  • 计算机基础 英语名称,计算机英语词汇:计算机基础知识

    computer n 电脑 电子计算机 arithmetic logic unit 算术逻辑部件 manipulate vt 操纵 操作 keyboard n 键盘 information n 消息 知识 printer n 打印机 han
  • Log.d 日志调试查看(所有平台)

    https www cnblogs com onechen p 6436748 html http docwiki embarcadero com Libraries Berlin en FMX Types Log d 转载于 https
  • 如何去除数据库中重复的数据

    去除数据库中重复的数据 准备工作 方法一 用distinct 联合去重 方法二 使用窗口函数限制row number 1 方法三 使用窗口函数删除row number gt 1 方法四 group by去重 准备工作 原始表users CR
  • vs调试时报错:变量已被优化掉,因而不可用

    前言 使用vs运行程序时 发现不是每次运行的结果都一致 抛开多线程的因素 比方说我用openGL加载骨骼动画数据 有时候能加载出骨骼纹理 有时候就不行 很头疼 在调试问题的时候就遇见vs调试器报错 变量已被优化掉 因而不可用 解决 在vs顶
  • USB Audio&hid 混合设备的描述符详解

    USB Standard Device Descriptor ALIGN BEGIN uint8 t USBD HS DeviceDesc USB LEN DEV DESC ALIGN END 0x12 bLength USB DESC T
  • OpenCV中的姿势估计及3D效果(3D坐标轴,3D立方体)绘制

    OpenCV中的姿势估计及3D效果 3D坐标轴 3D立方体 绘制 1 效果图 2 原理 3 源码 3 1 姿态估计后绘制3D坐标轴 3 2 姿态估计后绘制立方体 参考 这篇博客将延续上一篇博客 OpenCV中的相机失真 内外参 不失真图像
  • 【泛微E9】JS限制明细表文本框包含特殊文字

  • 使用IDEA工具不能自动导包的处理方法

    使用IDEA工具不能自动导包的处理方法 当我们在使用idea的时候 有时候会出现它不自动导包的情况 这是因为你没有选中自动导包的按钮 那怎么选择呢 首先点击File选项中Settings 如图 之后在搜索框中搜索Maven 再选中Impor
  • 未来刷脸支付设备圈地运动将会加剧

    未来刷脸支付设备的圈地运动将会加剧 而支付宝方面数据显示 自从去年支付宝宣布刷脸支付大规模商业化后 与刷脸支付相关的上下游产业链 催生的研发 生产 安装调试人员就已经达到50万人 刷脸支付项目合作推广更是成为市场热门 抓住移动支付发展的浪潮
  • 【FFmpeg】多媒体文件处理

    1 ffmpeg的日志打印 include