Skip to content

Commit 87f859e

Browse files
bakpaulhugtalbot
andcommitted
[GL] Modify the FFMPeg recorder to be able to use it in SofaGLFW (#5862)
* Add a quiet option for some filesystem methods that only perform tests while keeping old behavior s default for compatibility * Modify the FFMPeg recorder to be able to use it in SofaGLFW * Quietly check for file exists --------- Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
1 parent 794f856 commit 87f859e

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

Sofa/GL/src/sofa/gl/VideoRecorderFFMPEG.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ bool VideoRecorderFFMPEG::init(const std::string& ffmpeg_exec_filepath, const st
9696
extension = ".exe";
9797
#endif
9898
m_ffmpegExecPath = helper::Utils::getExecutablePath() + "/ffmpeg" + extension;
99-
if(!FileSystem::isFile(m_ffmpegExecPath))
99+
if(!FileSystem::isFile(m_ffmpegExecPath, true))
100100
{
101+
msg_warning("VideoRecorderFFMPEG")<< "ffmpeg hasn't been found automatically. Falling back to simply calling ffmpeg"<< extension <<" and hope that the OS finds it on its own. " ;
101102
// Fallback to a relative FFMPEG (may be in system or exposed in PATH)
102103
m_ffmpegExecPath = "ffmpeg" + extension;
103104
}
@@ -138,31 +139,38 @@ void VideoRecorderFFMPEG::addFrame()
138139
{
139140
GLint viewport[4];
140141
glGetIntegerv(GL_VIEWPORT, viewport);
141-
142+
142143
if ((viewport[2] != m_viewportWidth) || (viewport[3] != m_viewportHeight))
143144
{
144145
std::cout << "WARNING viewport changed during video capture from " << m_viewportWidth << "x" << m_viewportHeight << " to " << viewport[2] << "x" << viewport[3] << std::endl;
145146
}
146147

148+
glReadPixels(viewport[0], viewport[1], m_viewportWidth, m_viewportHeight, GL_RGBA, GL_UNSIGNED_BYTE, (void*)m_viewportBuffer);
149+
150+
addFrame(m_viewportBuffer, m_viewportWidth, m_viewportHeight);
151+
}
152+
147153

148-
glReadPixels(0, 0, m_viewportWidth, m_viewportHeight, GL_RGBA, GL_UNSIGNED_BYTE, (void*)m_viewportBuffer);
149154

150-
// set ffmpeg buffer: initialize to 0 (black)
155+
void VideoRecorderFFMPEG::addFrame(unsigned char* rgbData, int fbWidth, int fbHeight)
156+
{
157+
158+
// set ffmpeg buffer: initialize to 0 (black)
151159
memset(m_ffmpegBuffer, 0, m_ffmpegBufferSize);
152160

153-
if (m_viewportWidth == m_ffmpegWidth)
161+
if (fbWidth == m_ffmpegWidth)
154162
{
155-
memcpy(m_ffmpegBuffer, m_viewportBuffer, m_viewportBufferSize);
163+
memcpy(m_ffmpegBuffer, rgbData, fbHeight * fbWidth * m_pixelFormatSize);
156164
}
157165
else
158166
{
159-
const unsigned char* viewportBufferIter = m_viewportBuffer;
160-
const size_t viewportRowSizeInBytes = m_pixelFormatSize * m_viewportWidth;
167+
const unsigned char* viewportBufferIter = rgbData;
168+
const size_t viewportRowSizeInBytes = m_pixelFormatSize * fbWidth;
161169

162170
unsigned char* ffmpegBufferIter = m_ffmpegBuffer;
163171
const size_t ffmpegRowSizeInBytes = m_pixelFormatSize * m_ffmpegWidth;
164172

165-
int row = m_viewportHeight;
173+
int row = fbHeight;
166174
while ( row-- > 0 )
167175
{
168176
memcpy( ffmpegBufferIter, viewportBufferIter, viewportRowSizeInBytes);
@@ -174,8 +182,6 @@ void VideoRecorderFFMPEG::addFrame()
174182

175183

176184
fwrite(m_ffmpegBuffer, m_ffmpegBufferSize, 1, m_ffmpeg);
177-
178-
return;
179185
}
180186

181187
void VideoRecorderFFMPEG::finishVideo()

Sofa/GL/src/sofa/gl/VideoRecorderFFMPEG.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ class SOFA_GL_API VideoRecorderFFMPEG
6262
bool init(const std::string& ffmpeg_exec_filepath, const std::string& filename, int width, int height, unsigned int framerate, unsigned int bitrate, const std::string& codec="");
6363

6464
void addFrame();
65+
void addFrame(unsigned char* rgbData, int fbWidth, int fbHeight);
6566
void saveVideo();
6667
void finishVideo();
6768

69+
int getPixelFormatSize()
70+
{
71+
return m_pixelFormatSize;
72+
};
6873

6974
void setPrefix(const std::string v) { m_prefix = v; }
7075

0 commit comments

Comments
 (0)