forked from Raveler/ffmpeg-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleInterface.cpp
More file actions
229 lines (190 loc) · 5.66 KB
/
SimpleInterface.cpp
File metadata and controls
229 lines (190 loc) · 5.66 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// AniHub.LipSync.cpp : Defines the exported functions for the DLL application.
//
#include "SimpleInterface.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <io.h>
#include <filesystem>
#include <fstream>
#include <sstream>
#include "ffmpegcpp.h"
#include "ffmpeg.h"
using namespace std;
using namespace ffmpegcpp;
struct Context
{
Muxer* muxer = nullptr;
VideoCodec* videoCodec = nullptr;
AudioCodec* audioCodec = nullptr;
Demuxer* videoDemuxer = nullptr;
Demuxer* audioDemuxer = nullptr;
const char* videoFilterString = nullptr;
const char* audioFilterString = nullptr;
Filter* videoFilter = nullptr;
Filter* audioFilter = nullptr;
VideoFrameSink* videoEncoder = nullptr;
AudioFrameSink* audioEncoder = nullptr;
RawVideoDataSource* source = nullptr;
vector<Demuxer*> uniqueDemuxers;
bool errored = false;
string error;
};
void SetError(Context* ctx, string error)
{
ctx->errored = true;
ctx->error = string(error);
}
void CleanUp(Context* ctx)
{
if (ctx->muxer != nullptr) delete ctx->muxer;
if (ctx->videoEncoder != nullptr) delete ctx->videoEncoder;
if (ctx->audioEncoder != nullptr) delete ctx->audioEncoder;
if (ctx->videoFilter != nullptr) delete ctx->videoFilter;
if (ctx->audioFilter != nullptr) delete ctx->audioFilter;
if (ctx->videoCodec != nullptr) delete ctx->videoCodec;
if (ctx->audioCodec != nullptr) delete ctx->audioCodec;
for (int i = 0; i < ctx->uniqueDemuxers.size(); ++i)
{
delete ctx->uniqueDemuxers[i];
}
delete ctx;
}
Demuxer* GetExistingDemuxer(Context* ctx, const char* fileName)
{
if (ctx->videoDemuxer != nullptr && string(ctx->videoDemuxer->GetFileName()) == string(fileName)) return ctx->videoDemuxer;
if (ctx->audioDemuxer != nullptr && string(ctx->audioDemuxer->GetFileName()) == string(fileName)) return ctx->audioDemuxer;
return nullptr;
}
void* ffmpegCppCreate(const char* outputFileName)
{
Context* ctx = new Context();
try
{
// create the output muxer but don't add any streams yet
ctx->muxer = new Muxer(outputFileName);
return ctx;
}
catch (FFmpegException e)
{
SetError(ctx, string("Failed to create output file " + string(outputFileName) + ": " + string(e.what())));
return nullptr;
}
}
void ffmpegCppAddVideoStream(void* handle, const char* videoFileName)
{
Context* ctx = (Context*)handle;
try
{
// create the demuxer or re-use the previous one
ctx->videoDemuxer = GetExistingDemuxer(ctx, videoFileName);
if (ctx->videoDemuxer == nullptr)
{
ctx->videoDemuxer = new Demuxer(videoFileName);
ctx->uniqueDemuxers.push_back(ctx->videoDemuxer);
}
// create the encoder
ctx->videoCodec = new VideoCodec(ctx->muxer->GetDefaultVideoFormat()->id);
ctx->videoEncoder = new VideoEncoder(ctx->videoCodec, ctx->muxer);
}
catch (FFmpegException e)
{
SetError(ctx, string("Failed to add video stream " + string(videoFileName) + ": " + string(e.what())));
}
}
void ffmpegCppAddAudioStream(void* handle, const char* audioFileName)
{
Context* ctx = (Context*)handle;
try
{
// create the demuxer or re-use the previous one
ctx->audioDemuxer = GetExistingDemuxer(ctx, audioFileName);
if (ctx->audioDemuxer == nullptr)
{
ctx->audioDemuxer = new Demuxer(audioFileName);
ctx->uniqueDemuxers.push_back(ctx->audioDemuxer);
}
// create the encoder
ctx->audioCodec = new AudioCodec(ctx->muxer->GetDefaultAudioFormat()->id);
ctx->audioEncoder = new AudioEncoder(ctx->audioCodec, ctx->muxer);
}
catch (FFmpegException e)
{
SetError(ctx, string("Failed to add audio stream " + string(audioFileName) + ": " + string(e.what())));
}
}
void ffmpegCppAddVideoFilter(void* handle, const char* filterString)
{
Context* ctx = (Context*)handle;
ctx->videoFilterString = filterString;
}
void ffmpegCppAddAudioFilter(void* handle, const char* filterString)
{
Context* ctx = (Context*)handle;
ctx->audioFilterString = filterString;
}
void ffmpegCppGenerate(void* handle)
{
Context* ctx = (Context*)handle;
try
{
// create a filter if necessary
FrameSink* videoFrameSink = ctx->videoEncoder;
FrameSink* audioFrameSink = ctx->audioEncoder;
if (ctx->videoFilterString != nullptr)
{
ctx->videoFilter = new Filter(ctx->videoFilterString, ctx->videoEncoder);
videoFrameSink = ctx->videoFilter;
}
if (ctx->audioFilterString != nullptr)
{
ctx->audioFilter = new Filter(ctx->audioFilterString, ctx->audioEncoder);
audioFrameSink = ctx->audioFilter;
}
// connect the input and output streams
if (ctx->videoDemuxer != nullptr)
{
ctx->videoDemuxer->DecodeBestVideoStream(videoFrameSink);
}
if (ctx->audioDemuxer != nullptr)
{
ctx->audioDemuxer->DecodeBestAudioStream(audioFrameSink);
}
// now go over all the streams and process all frames
for (int i = 0; i < ctx->uniqueDemuxers.size(); ++i)
{
ctx->uniqueDemuxers[i]->PreparePipeline();
}
// finally, we can start writing data to our pipelines. Open the floodgates
// to start reading frames from the input, decoding them, optionally filtering them,
// encoding them and writing them to the final container.
// This can be interweaved if you want to.
for (int i = 0; i < ctx->uniqueDemuxers.size(); ++i)
{
Demuxer* demuxer = ctx->uniqueDemuxers[i];
while (!demuxer->IsDone()) demuxer->Step();
}
// close the muxer and save the file to disk
ctx->muxer->Close();
}
catch (FFmpegException e)
{
SetError(ctx, string("Failed to generate output file: " + string(e.what())));
}
}
void ffmpegCppAddFilter(void* handle, const char* filterString)
{
// TODO
}
bool ffmpegCppIsError(void* handle)
{
return ((Context*)handle)->errored;
}
const char* ffmpegCppGetError(void* handle)
{
return ((Context*)handle)->error.c_str();
}
void ffmpegCppClose(void* handle)
{
CleanUp((Context*)handle);
}