我的设备上有MPEG-TS文件。我想把设备上文件的开始时间缩短到一个相当精确的时间。
使用FFmpegWrapper作为基础,我希望能实现这一点。
然而,我对ffmpeg的C API有点迷惑。我该从哪里开始呢?
我尝试在开始PTS之前丢弃所有数据包,但这中断了视频流。
packet->pts = av_rescale_q(packet->pts, inputStream.stream->time_base, outputStream.stream->time_base);
packet->dts = av_rescale_q(packet->dts, inputStream.stream->time_base, outputStream.stream->time_base);
if(startPts == 0){
startPts = packet->pts;
}
if(packet->pts < cutTimeStartPts + startPts){
av_free_packet(packet);
continue;
}
如何在不破坏视频流的情况下切断输入文件的部分开头?当背靠背播放时,我希望2个剪辑片段无缝地运行在一起。
ffmpeg -i time.ts -c:v libx264 -c:a copy -ss $CUT_POINT -map 0 -y after.ts
ffmpeg -i time.ts -c:v libx264 -c:a copy -to $CUT_POINT -map 0 -y before.ts
这似乎就是我需要的。我认为重新编码是必要的,这样视频就可以从任意点开始,而不是现有的关键帧。如果有更有效的解决方案,那就太好了。如果没有,这就足够了。
编辑:这是我的尝试。我拼凑了各种我不能完全理解的从here复制过来的东西。我暂时不写“裁剪”这篇文章,而是试着在没有分层复杂性的情况下编写音频+视频编码。我在avcodec_encode_video2(...)
上获取EXC_BAD_ACCESS
- (void)convertInputPath:(NSString *)inputPath outputPath:(NSString *)outputPath
options:(NSDictionary *)options progressBlock:(FFmpegWrapperProgressBlock)progressBlock
completionBlock:(FFmpegWrapperCompletionBlock)completionBlock {
dispatch_async(conversionQueue, ^{
FFInputFile *inputFile = nil;
FFOutputFile *outputFile = nil;
NSError *error = nil;
inputFile = [[FFInputFile alloc] initWithPath:inputPath options:options];
outputFile = [[FFOutputFile alloc] initWithPath:outputPath options:options];
[self setupDirectStreamCopyFromInputFile:inputFile outputFile:outputFile];
if (![outputFile openFileForWritingWithError:&error]) {
[self finishWithSuccess:NO error:error completionBlock:completionBlock];
return;
}
if (![outputFile writeHeaderWithError:&error]) {
[self finishWithSuccess:NO error:error completionBlock:completionBlock];
return;
}
AVRational default_timebase;
default_timebase.num = 1;
default_timebase.den = AV_TIME_BASE;
FFStream *outputVideoStream = outputFile.streams[0];
FFStream *inputVideoStream = inputFile.streams[0];
AVFrame *frame;
AVPacket inPacket, outPacket;
frame = avcodec_alloc_frame();
av_init_packet(&inPacket);
while (av_read_frame(inputFile.formatContext, &inPacket) >= 0) {
if (inPacket.stream_index == 0) {
int frameFinished;
avcodec_decode_video2(inputVideoStream.stream->codec, frame, &frameFinished, &inPacket);
// if (frameFinished && frame->pkt_pts >= starttime_int64 && frame->pkt_pts <= endtime_int64) {
if (frameFinished){
av_init_packet(&outPacket);
int output;
avcodec_encode_video2(outputVideoStream.stream->codec, &outPacket, frame, &output);
if (output) {
if (av_write_frame(outputFile.formatContext, &outPacket) != 0) {
fprintf(stderr, "convert(): error while writing video frame\n");
[self finishWithSuccess:NO error:nil completionBlock:completionBlock];
}
}
av_free_packet(&outPacket);
}
if (frame->pkt_pts > endtime_int64) {
break;
}
}
}
av_free_packet(&inPacket);
if (![outputFile writeTrailerWithError:&error]) {
[self finishWithSuccess:NO error:error completionBlock:completionBlock];
return;
}
[self finishWithSuccess:YES error:nil completionBlock:completionBlock];
});
}
发布于 2016-01-25 10:03:40
FFmpeg (在本例中为libavformat/codec) API非常紧密地映射ffmpeg.exe命令行参数。要打开文件,请使用avformat_open_input_file()。最后两个参数可以为空。这将为您填充AVFormatContext。现在开始在循环中使用av_read_frame()读取帧。pkt.stream_index会告诉你每个包属于哪个流,avformatcontext->streamspkt.stream_index是随附的流信息,它告诉你它使用的是什么编解码器,是视频还是音频等。使用avformat_close()关闭。
对于多路复用,您使用相反的方法,有关详细信息,请参阅muxing。基本上,对于输入文件中的每个现有流,它都是allocate、avio_open2、add streams (基本上是context->streams[]),循环中是avformat_write_header()、av_interleaved_write_frame(),关闭的是av_write_trailer() (最后是free分配的上下文)。
使用libavcodec完成视频流的编码/解码。对于从多路复用器获得的每个AVPacket,使用avcodec_decode_video2()。使用avcodec_encode_video2()对输出AVFrame进行编码。请注意,这两者都会引入延迟,因此对每个函数的前几次调用都不会返回任何数据,并且您需要通过使用空输入数据调用每个函数来清除缓存的数据,以从中取出尾部数据包/帧。av_interleave_write_frame将正确交错数据包,因此视频/音频流不会去同步(如:在ts文件中,相同时间戳的视频数据包在音频数据包之后出现MB)。
如果你需要更多关于avcodec_decode_video2、avcodec_encode_video2、av_read_frame或av_interleaved_write_frame的详细示例,只需谷歌"$function example“,你就会看到完整的示例展示如何正确地使用它们。对于x264编码,在调用avcodec_open2进行编码质量设置时在AVCodecContext中执行set some default parameters。在C应用编程接口中,您可以使用AVDictionary来完成此操作,例如:
AVDictionary opts = *NULL;
av_dict_set(&opts, "preset", "veryslow", 0);
// use either crf or b, not both! See the link above on H264 encoding options
av_dict_set_int(&opts, "b", 1000, 0);
av_dict_set_int(&opts, "crf", 10, 0);
编辑哦,我忘了一部分,时间戳。每个AVPacket和AVFrame在其结构中都有一个pts变量,您可以使用该变量来决定是否在输出流中包含包/帧。因此,对于音频,您将使用多路复用步骤中的AVPacket.pts作为分隔符,对于视频,您将使用解码步骤中的AVFrame.pts作为分隔符。它们各自的文档会告诉您它们所在的单元。
edit2我看到你在没有实际代码的情况下仍然有一些问题,所以这里有一个真正的(工作的)代码转换器,它可以重新编码视频和重新复用音频。它可能有大量的but,泄漏和缺乏适当的错误报告,它也不处理时间戳(我把它留给你作为练习),但它做了你要求的基本事情:
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
static AVFormatContext *inctx, *outctx;
#define MAX_STREAMS 16
static AVCodecContext *inavctx[MAX_STREAMS];
static AVCodecContext *outavctx[MAX_STREAMS];
static int openInputFile(const char *file) {
int res;
inctx = NULL;
res = avformat_open_input(& inctx, file, NULL, NULL);
if (res != 0)
return res;
res = avformat_find_stream_info(inctx, NULL);
if (res < 0)
return res;
return 0;
}
static void closeInputFile(void) {
int n;
for (n = 0; n < inctx->nb_streams; n++)
if (inavctx[n]) {
avcodec_close(inavctx[n]);
avcodec_free_context(&inavctx[n]);
}
avformat_close_input(&inctx);
}
static int openOutputFile(const char *file) {
int res, n;
outctx = avformat_alloc_context();
outctx->oformat = av_guess_format(NULL, file, NULL);
if ((res = avio_open2(&outctx->pb, file, AVIO_FLAG_WRITE, NULL, NULL)) < 0)
return res;
for (n = 0; n < inctx->nb_streams; n++) {
AVStream *inst = inctx->streams[n];
AVCodecContext *inc = inst->codec;
if (inc->codec_type == AVMEDIA_TYPE_VIDEO) {
// video decoder
inavctx[n] = avcodec_alloc_context3(inc->codec);
avcodec_copy_context(inavctx[n], inc);
if ((res = avcodec_open2(inavctx[n], avcodec_find_decoder(inc->codec_id), NULL)) < 0)
return res;
// video encoder
AVCodec *encoder = avcodec_find_encoder_by_name("libx264");
AVStream *outst = avformat_new_stream(outctx, encoder);
outst->codec->width = inavctx[n]->width;
outst->codec->height = inavctx[n]->height;
outst->codec->pix_fmt = inavctx[n]->pix_fmt;
AVDictionary *dict = NULL;
av_dict_set(&dict, "preset", "veryslow", 0);
av_dict_set_int(&dict, "crf", 10, 0);
outavctx[n] = avcodec_alloc_context3(encoder);
avcodec_copy_context(outavctx[n], outst->codec);
if ((res = avcodec_open2(outavctx[n], encoder, &dict)) < 0)
return res;
} else if (inc->codec_type == AVMEDIA_TYPE_AUDIO) {
avformat_new_stream(outctx, inc->codec);
inavctx[n] = outavctx[n] = NULL;
} else {
fprintf(stderr, "Don’t know what to do with stream %d\n", n);
return -1;
}
}
if ((res = avformat_write_header(outctx, NULL)) < 0)
return res;
return 0;
}
static void closeOutputFile(void) {
int n;
av_write_trailer(outctx);
for (n = 0; n < outctx->nb_streams; n++)
if (outctx->streams[n]->codec)
avcodec_close(outctx->streams[n]->codec);
avformat_free_context(outctx);
}
static int encodeFrame(int stream_index, AVFrame *frame, int *gotOutput) {
AVPacket outPacket;
int res;
av_init_packet(&outPacket);
if ((res = avcodec_encode_video2(outavctx[stream_index], &outPacket, frame, gotOutput)) < 0) {
fprintf(stderr, "Failed to encode frame\n");
return res;
}
if (*gotOutput) {
outPacket.stream_index = stream_index;
if ((res = av_interleaved_write_frame(outctx, &outPacket)) < 0) {
fprintf(stderr, "Failed to write packet\n");
return res;
}
}
av_free_packet(&outPacket);
return 0;
}
static int decodePacket(int stream_index, AVPacket *pkt, AVFrame *frame, int *frameFinished) {
int res;
if ((res = avcodec_decode_video2(inavctx[stream_index], frame,
frameFinished, pkt)) < 0) {
fprintf(stderr, "Failed to decode frame\n");
return res;
}
if (*frameFinished){
int hasOutput;
frame->pts = frame->pkt_pts;
return encodeFrame(stream_index, frame, &hasOutput);
} else {
return 0;
}
}
int main(int argc, char *argv[]) {
char *input = argv[1];
char *output = argv[2];
int res, n;
printf("Converting %s to %s\n", input, output);
av_register_all();
if ((res = openInputFile(input)) < 0) {
fprintf(stderr, "Failed to open input file %s\n", input);
return res;
}
if ((res = openOutputFile(output)) < 0) {
fprintf(stderr, "Failed to open output file %s\n", input);
return res;
}
AVFrame *frame = av_frame_alloc();
AVPacket inPacket;
av_init_packet(&inPacket);
while (av_read_frame(inctx, &inPacket) >= 0) {
if (inavctx[inPacket.stream_index] != NULL) {
int frameFinished;
if ((res = decodePacket(inPacket.stream_index, &inPacket, frame, &frameFinished)) < 0) {
return res;
}
} else {
if ((res = av_interleaved_write_frame(outctx, &inPacket)) < 0) {
fprintf(stderr, "Failed to write packet\n");
return res;
}
}
}
for (n = 0; n < inctx->nb_streams; n++) {
if (inavctx[n]) {
// flush decoder
int frameFinished;
do {
inPacket.data = NULL;
inPacket.size = 0;
if ((res = decodePacket(n, &inPacket, frame, &frameFinished)) < 0)
return res;
} while (frameFinished);
// flush encoder
int gotOutput;
do {
if ((res = encodeFrame(n, NULL, &gotOutput)) < 0)
return res;
} while (gotOutput);
}
}
av_free_packet(&inPacket);
closeInputFile();
closeOutputFile();
return 0;
}
https://stackoverflow.com/questions/34823545
复制相似问题