Skip to content

Commit 13b55df

Browse files
committed
bump version, remove unused param, few cleanups
1 parent be1e8ff commit 13b55df

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

include/audio_mixer.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ class FFMPEG_API_DLL AudioMixer {
2424
void mixVideoAudio(std::filesystem::path videoFile, std::filesystem::path audioFile, std::filesystem::path outputMp4File);
2525

2626
/**
27+
* @deprecated sampleRate parameter is no longer used. Use the other overload of this function instead.
28+
*
2729
* @brief Mixes a video file and raw audio data into a single MP4 output.
2830
*
29-
* This function takes an input video file and raw audio data (in the form of a vector of floating-point samples),
31+
* This function takes an input video file and raw audio data (in the form of a vector of floating-point samples),
3032
* and merges them into a single MP4 output file.
3133
*
3234
* @param videoFile The path to the input video file.
@@ -37,7 +39,22 @@ class FFMPEG_API_DLL AudioMixer {
3739
* @warning The raw audio data is expected to be stereo (dual-channel). Using mono or multi-channel audio might lead to issues.
3840
* @warning The video file is expected to contain a single video stream. Only the first video stream will be copied.
3941
*/
40-
void mixVideoRaw(std::filesystem::path videoFile, const std::vector<float>& raw, std::filesystem::path outputMp4File, uint32_t sampleRate);
42+
[[deprecated]] void mixVideoRaw(std::filesystem::path videoFile, const std::vector<float>& raw, std::filesystem::path outputMp4File, uint32_t sampleRate);
43+
44+
/**
45+
* @brief Mixes a video file and raw audio data into a single MP4 output.
46+
*
47+
* This function takes an input video file and raw audio data (in the form of a vector of floating-point samples),
48+
* and merges them into a single MP4 output file.
49+
*
50+
* @param videoFile The path to the input video file.
51+
* @param raw A vector containing the raw audio data (floating-point samples).
52+
* @param outputMp4File The path where the output MP4 file will be saved.
53+
*
54+
* @warning The raw audio data is expected to be stereo (dual-channel). Using mono or multi-channel audio might lead to issues.
55+
* @warning The video file is expected to contain a single video stream. Only the first video stream will be copied.
56+
*/
57+
void mixVideoRaw(const std::filesystem::path& videoFile, const std::vector<float>& raw, const std::filesystem::path &outputMp4File);
4158
};
4259

4360
}

mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"id": "eclipse.ffmpeg-api",
1515
"name": "FFmpeg API",
16-
"version": "v1.1.1",
16+
"version": "v1.1.2",
1717
"developers": ["Eclipse Team", "maxnu"],
1818
"description": "Interaction with FFmpeg made easy",
1919
"tags": ["utility", "offline", "developer"],

src/audio_mixer.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ std::vector<float> resampleAudio(const std::vector<float>& inputAudio, int input
147147
return {};
148148
}
149149

150-
const int chunkSize = 4096;
151-
const int numChannels = 2;
150+
constexpr int chunkSize = 4096;
151+
constexpr int numChannels = 2;
152152

153153
int maxOutputSamples = av_rescale_rnd(chunkSize, targetSampleRate, inputSampleRate, AV_ROUND_UP);
154154
std::vector<float> outputAudio;
@@ -193,13 +193,18 @@ namespace ffmpeg {
193193

194194
std::vector<float> raw = readAudioFile(audioFile.string().c_str(), 44100, AV_SAMPLE_FMT_FLTP, &inputAudioParams);
195195

196-
mixVideoRaw(std::move(videoFile), raw, std::move(outputMp4File), inputAudioParams.sample_rate);
196+
mixVideoRaw(videoFile, raw, outputMp4File);
197197

198198
avformat_close_input(&wavFormatContext);
199199
}
200200

201-
void AudioMixer::mixVideoRaw(std::filesystem::path videoFile, const std::vector<float>& raw, std::filesystem::path outputMp4File, uint32_t sampleRate) {
202-
const int frameSize = 1024;
201+
void AudioMixer::mixVideoRaw(std::filesystem::path videoFile, const std::vector<float>& raw, std::filesystem::path outputMp4File, uint32_t) {
202+
mixVideoRaw(videoFile, raw, outputMp4File);
203+
}
204+
205+
void AudioMixer::mixVideoRaw(const std::filesystem::path& videoFile, const std::vector<float>& raw, const std::filesystem::path &outputMp4File) {
206+
constexpr int frameSize = 1024;
207+
constexpr uint32_t sampleRate = 44100;
203208

204209
AVFormatContext* videoFormatContext = nullptr;
205210
if (avformat_open_input(&videoFormatContext, videoFile.string().c_str(), nullptr, nullptr) < 0) {
@@ -349,18 +354,17 @@ namespace ffmpeg {
349354
continue;
350355
}
351356

352-
AVPacket audioPacket;
353-
av_init_packet(&audioPacket);
354-
audioPacket.data = nullptr;
355-
audioPacket.size = 0;
357+
AVPacket* audioPacket = av_packet_alloc();
358+
audioPacket->data = nullptr;
359+
audioPacket->size = 0;
356360

357361
while (true) {
358-
int ret = avcodec_receive_packet(audio_codec_context_encoder, &audioPacket);
362+
int ret = avcodec_receive_packet(audio_codec_context_encoder, audioPacket);
359363
if (ret == 0) {
360-
av_packet_rescale_ts(&audioPacket, audio_codec_context_encoder->time_base, outputAudioStream->time_base);
361-
audioPacket.stream_index = 1;
362-
av_interleaved_write_frame(outputFormatContext, &audioPacket);
363-
av_packet_unref(&audioPacket);
364+
av_packet_rescale_ts(audioPacket, audio_codec_context_encoder->time_base, outputAudioStream->time_base);
365+
audioPacket->stream_index = 1;
366+
av_interleaved_write_frame(outputFormatContext, audioPacket);
367+
av_packet_unref(audioPacket);
364368
} else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
365369
break;
366370
else {

src/recorder.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ bool Recorder::init(const RenderSettings& settings) {
8080
m_codecContext->pix_fmt = AV_PIX_FMT_NONE;
8181
m_videoStream->time_base = m_codecContext->time_base;
8282

83-
const AVPixelFormat *pix_fmt = m_codec->pix_fmts;
84-
if (pix_fmt) {
83+
if (const AVPixelFormat *pix_fmt = m_codec->pix_fmts) {
8584
while (*pix_fmt != AV_PIX_FMT_NONE) {
8685
if(*pix_fmt == static_cast<AVPixelFormat>(settings.m_pixelFormat))
8786
m_codecContext->pix_fmt = *pix_fmt;
@@ -142,8 +141,7 @@ bool Recorder::init(const RenderSettings& settings) {
142141

143142
m_packet = av_packet_alloc();
144143

145-
av_init_packet(m_packet);
146-
m_packet->data = NULL;
144+
m_packet->data = nullptr;
147145
m_packet->size = 0;
148146

149147
int inputPixelFormat = (int)settings.m_pixelFormat;
@@ -191,7 +189,7 @@ bool Recorder::init(const RenderSettings& settings) {
191189
}
192190

193191
m_swsCtx = sws_getContext(m_codecContext->width, m_codecContext->height, (AVPixelFormat)inputPixelFormat, m_codecContext->width,
194-
m_codecContext->height, m_codecContext->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
192+
m_codecContext->height, m_codecContext->pix_fmt, SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);
195193

196194
if (!m_swsCtx) {
197195
geode::log::error("Could not create sws context.");
@@ -253,12 +251,16 @@ bool Recorder::writeFrame(const std::vector<uint8_t>& frameData) {
253251

254252
void Recorder::filterFrame(AVFrame* inputFrame, AVFrame* outputFrame) {
255253
if (av_buffersrc_add_frame(m_buffersrcCtx, inputFrame) < 0) {
256-
std::cerr << "Error feeding frame to filter graph.\n";
254+
geode::log::error("Error feeding frame to filter graph.");
257255
avfilter_graph_free(&m_filterGraph);
258256
return;
259257
}
260258

261-
av_buffersink_get_frame(m_buffersinkCtx, outputFrame);
259+
if (av_buffersink_get_frame(m_buffersinkCtx, outputFrame) < 0) {
260+
geode::log::error("Error retrieving frame from filter graph.");
261+
av_frame_unref(outputFrame);
262+
return;
263+
}
262264
}
263265

264266
void Recorder::stop() {
@@ -290,6 +292,10 @@ void Recorder::stop() {
290292
av_frame_free(&m_filteredFrame);
291293
}
292294

295+
if (m_hwDevice) {
296+
av_buffer_unref(&m_hwDevice);
297+
}
298+
293299
av_packet_free(&m_packet);
294300
}
295301

0 commit comments

Comments
 (0)