support mp4 output

This commit is contained in:
Jan Grewe 2024-03-08 10:14:45 +01:00
parent 47ea6fb27e
commit 7ad190513a
6 changed files with 44 additions and 31 deletions

View File

@ -39,7 +39,7 @@ size_t ImageBuffer::bufferLoad() {
}
void ImageBuffer::resize(size_t new_size) {
std::cerr << "Buffer Resize" << std::endl;
qDebug() << "Buffer Resize";
mutex.lock();
buffer_size = new_size;
@ -54,9 +54,8 @@ void ImageBuffer::resize(size_t new_size) {
void ImageBuffer::push(MyImage *img) {
mutex.lock();
if (buffer[current_write_index] != nullptr) {
std::cerr << "possible frame drop detected!!" << std::endl;
std::cerr << "buffer.push write: " << current_write_index << " read: " << current_read_index << " load: " << load << std::endl;
qWarning() << "possible frame drop detected!!";
qWarning() << "buffer.push write: " << current_write_index << " read: " << current_read_index << " load: " << load;
delete(buffer[current_write_index]);
}
buffer[current_write_index] = img;
@ -93,7 +92,7 @@ MyImage* ImageBuffer::pop() {
load -= 1;
}
mutex.unlock();
std::cerr << "buffer.pop write: " << current_write_index << " read: " << current_read_index << " load: " << load << std::endl;
// std::cerr << "buffer.pop write: " << current_write_index << " read: " << current_read_index << " load: " << load << std::endl;
return img;
}

View File

@ -688,7 +688,7 @@ VideoSpecs PylonRecorder::getVideoSpecs(const ImageSettings &settings) {
void PylonRecorder::startRecording() {
qDebug() << "start recording!";
std::string filename = createFilename();
std::string filename = createFilename(".mp4");
fileLabel->setText(QString::fromStdString(filename));
qDebug() << "storing to file " << filename.c_str();
@ -697,6 +697,7 @@ void PylonRecorder::startRecording() {
VideoSpecs specs = getVideoSpecs(settings);
specs.filename = filename;
specs.format = VideoFormat::mp4;
qDebug() << "got video specifications";
if (buffer != nullptr) {
@ -825,11 +826,10 @@ QColor PylonRecorder::progressColor(int value) {
}
std::string PylonRecorder::createFilename() {
std::string PylonRecorder::createFilename(const std::string &extension) {
QDateTime dt(QDateTime::currentDateTimeUtc());
QDate date = dt.date();
std::string base = (date.toString("yyyy.MM.dd") + "_").toStdString();
std::string extension = ".raw";
QString idx = QString::number(movieCount);
std::string fname = base + idx.toStdString() + extension;
while (QFile::exists(QString::fromStdString(fname))) {

View File

@ -78,7 +78,7 @@ private:
void setExperimenterName();
void setSubjectID();
QColor progressColor(int value);
std::string createFilename();
std::string createFilename(const std::string &extension=".raw");
void cameraConfiguration();
VideoSpecs getVideoSpecs(const ImageSettings &settings);

4
util.h
View File

@ -25,4 +25,8 @@ struct CameraLayout {
CameraMode mode;
};
enum class VideoFormat {
raw,
mp4
};
#endif /*UTIL_H*/

View File

@ -2,6 +2,7 @@
#include <chrono>
#include <fstream>
#include <pylon/VideoWriter.h>
using namespace std::chrono;
void Writer::setVideoSpecs(VideoSpecs specs) {
@ -28,21 +29,28 @@ void Writer::run() {
size_t count = 0;
size_t chunksize = 256;
// size_t framecount = 0;
std::ofstream myFile;
if (videoSpecs.format == VideoFormat::mp4 && !Pylon::CVideoWriter::IsSupported()) {
qWarning() << "VideoWriter is not supported at the moment. Please install the pylon Supplementary Package for MPEG-4 which is available on the Basler website.";
// Releases all pylon resources.
// PylonTerminate();
// Return with error code 1.
emit writingDone();
return;
}
Pylon::CVideoWriter videoWriter;
if (specs_valid) {
stop_request = false;
stopNow = false;
if (videoSpecs.format == VideoFormat::raw) {
myFile.open(videoSpecs.filename, std::ios::out | std::ios::binary);
myFile.write((char*)&videoSpecs.width, 4);
myFile.write((char*)&videoSpecs.height, 4);
} else {
videoWriter.SetParameter(videoSpecs.width, videoSpecs.height, videoSpecs.pixelType, videoSpecs.fps, videoSpecs.quality);
videoWriter.Open(videoSpecs.filename.c_str());
}
std::ofstream myFile;
myFile.open(videoSpecs.filename, std::ios::out | std::ios::binary);
myFile.write((char*)&videoSpecs.width, 4);
myFile.write((char*)&videoSpecs.height, 4);
// Pylon::CVideoWriter videoWriter;
// videoWriter.SetParameter(videoSpecs.width, videoSpecs.height, videoSpecs.pixelType,
// videoSpecs.fps, videoSpecs.quality);
// std::cout << !Pylon::CVideoWriter::IsSupported() << std::endl;
// videoWriter.SetParameter();
// videoWriter.Open(videoSpecs.filename.c_str());
// std::cerr << "Video writer is open!" << videoWriter.IsOpen() << std::endl;
nix::File nix_file =nix::File::open(videoSpecs.filename + ".nix", nix::FileMode::Overwrite, "hdf5", nix::Compression::DeflateNormal);
nix::Block b = nix_file.createBlock("Recording", "nix.recording");
nix::Section s = nix_file.createSection("Recording", "nix.recording");
@ -88,17 +96,17 @@ void Writer::run() {
if (img != nullptr) {
auto start = high_resolution_clock::now();
// framecount += 1;
myFile.write((char*)img->data(), img->size());
// Pylon::CPylonImage pyImage;
// try {
// pyImage.AttachUserBuffer(img->data(), videoSpecs.width * videoSpecs.height, videoSpecs.pixelType, videoSpecs.width, videoSpecs.height, 0, videoSpecs.orientation);
// // Test duration of writing...
// videoWriter.Add( pyImage );
// } catch (const Pylon::GenericException &e) {
// std::cerr << "Writer::run: An exception occurred." << std::endl << e.GetDescription() << std::endl;
// }
if (videoSpecs.format == VideoFormat::raw) {
myFile.write((char*)img->data(), img->size());
} else {
Pylon::CPylonImage pyImage;
try {
pyImage.AttachUserBuffer(img->data(), videoSpecs.width * videoSpecs.height, videoSpecs.pixelType, videoSpecs.width, videoSpecs.height, 0, videoSpecs.orientation);
videoWriter.Add(pyImage);
} catch (const Pylon::GenericException &e) {
std::cerr << "Writer::run: An exception occurred." << std::endl << e.GetDescription() << std::endl;
}
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
std::cerr << "wrote binary to file " << duration.count() << std::endl;

View File

@ -10,6 +10,7 @@
#include "imagebuffer.h"
#include "projectsettings.h"
#include <opencv2/opencv.hpp>
#include "util.h"
struct VideoSpecs {
std::string filename;
@ -20,6 +21,7 @@ struct VideoSpecs {
double detectorGain;
Pylon::EPixelType pixelType;
Pylon::EImageOrientation orientation;
VideoFormat format = VideoFormat::raw;
};
class Writer : public QThread