forked from xufuji456/FFmpegAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_cutting.cpp
More file actions
127 lines (113 loc) · 4.45 KB
/
video_cutting.cpp
File metadata and controls
127 lines (113 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Created by xu fulong on 2022/5/13.
//
#include <video_cutting.h>
#ifdef __ANDROID__
#include <android/log.h>
#define LOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR, "CutVideo", FORMAT, ##__VA_ARGS__)
#else
#include <stdio.h>
#define LOGE(FORMAT, ...) printf(FORMAT, ##__VA_ARGS__)
#endif
int CutVideo::open_output_file(AVFormatContext *ifmt_ctx, const char *filename)
{
int ret;
avformat_alloc_output_context2(&ofmt_ctx, nullptr, nullptr, filename);
if (!ofmt_ctx) {
LOGE("Could not create output context\n");
return AVERROR_UNKNOWN;
}
dts_start_offset = new int64_t [ifmt_ctx->nb_streams];
memset(dts_start_offset, -1, sizeof(int64_t) * ifmt_ctx->nb_streams);
pts_start_offset = new int64_t [ifmt_ctx->nb_streams];
memset(pts_start_offset, -1, sizeof(int64_t) * ifmt_ctx->nb_streams);
for (int i = 0; i < ifmt_ctx->nb_streams; i++) {
AVStream* in_stream = ifmt_ctx->streams[i];
AVCodecParameters* codecpar = in_stream->codecpar;
const AVCodec* dec = avcodec_find_decoder(codecpar->codec_id);
AVStream* out_stream = avformat_new_stream(ofmt_ctx, dec);
if (!out_stream) {
LOGE("Failed allocating output stream\n");
ret = AVERROR_UNKNOWN;
return ret;
}
avcodec_parameters_copy(out_stream->codecpar, codecpar);
}
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 < 0) {
LOGE("Could not open output file %s\n", filename);
return ret;
}
}
/* init muxer, write output file header */
ret = avformat_write_header(ofmt_ctx, nullptr);
if (ret < 0) {
LOGE("Error occurred when opening output file\n");
return ret;
}
return 0;
}
void CutVideo:: setParam(int64_t start_time, int64_t duration) {
m_startTime = start_time;
m_duration = duration;
}
AVPacket* CutVideo::copy_packet(AVFormatContext *ifmt_ctx, AVPacket *packet) {
auto* pkt = (AVPacket*)av_malloc(sizeof(AVPacket));
av_new_packet(pkt, 0);
if (0 == av_packet_ref(pkt, packet)) {
AVStream* in_stream = ifmt_ctx->streams[pkt->stream_index];
AVStream* out_stream = ofmt_ctx->streams[pkt->stream_index];
if (pts_start_offset[pkt->stream_index] == -1) {
pts_start_offset[pkt->stream_index] = pkt->pts;
dts_start_offset[pkt->stream_index] = pkt->dts;
}
// convert pts and dts
pkt->pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base, out_stream->time_base,
static_cast<AVRounding>(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base, out_stream->time_base,
static_cast<AVRounding>(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;
return pkt;
}
return nullptr;
}
int CutVideo::write_internal(AVFormatContext *ifmt_ctx, AVPacket *packet)
{
int ret;
AVPacket *pkt = copy_packet(ifmt_ctx, packet);
if (pkt == nullptr) {
LOGE("packet is NULL\n");
return -1;
}
pkt->pts = pkt->pts - pts_start_offset[pkt->stream_index];
pkt->dts = pkt->dts - dts_start_offset[pkt->stream_index];
// LOGE("pts=%ld, dts=%ld, stream_index=%d", pkt->pts, pkt->dts, pkt->stream_index);
// write packet into file
//TODO:when pts < dts, it occurs error.
// Therefore, wo need to cache packet queue, and ascend sort by dts
if ((ret = av_interleaved_write_frame(ofmt_ctx, pkt)) < 0) {
LOGE("Error to mux packet, stream_index=%d, pts=%ld, dts=%ld\n", pkt->stream_index, pkt->pts, pkt->dts);
}
av_packet_unref(pkt);
return ret;
}
void CutVideo::write_output_file(AVFormatContext *ifmt_ctx, AVPacket *packet) {
int64_t timestamp = packet->pts * av_q2d(ifmt_ctx->streams[packet->stream_index]->time_base);
if (timestamp >= m_startTime && timestamp < m_startTime + m_duration) {
write_internal(ifmt_ctx, packet);
}
}
void CutVideo::close_output_file() {
if (!ofmt_ctx)
return;
av_write_trailer(ofmt_ctx);
if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
avio_close(ofmt_ctx->pb);
}
avformat_free_context(ofmt_ctx);
delete pts_start_offset;
delete dts_start_offset;
}