Write dive data as video subtitles

This commit adds an entry to the dive media context
menu which offers to write a subtitle file. This
creates an .ass file for the selected videos.

In an attempt to to clutter the screen too much, don't
show irrelevant entries (zero temperature or
NDL and show TTS only for dives with stops).

VLC is able to show these subtitles directly, they
can be integrated into the video file with ffmpeg.

Signed-off-by: Robert C. Helling <helling@atdotde.de>
This commit is contained in:
Robert C. Helling 2019-04-14 16:19:23 +02:00 committed by bstoeger
parent 0573b19b65
commit 52105e5217
8 changed files with 125 additions and 2 deletions

View file

@ -11,6 +11,8 @@
#include <QUrl>
#include <QMessageBox>
#include <QFileInfo>
#include "core/save-profiledata.h"
#include "core/membuffer.h"
//TODO: Remove those in the future.
#include "../mainwindow.h"
@ -57,6 +59,7 @@ void TabDivePhotos::contextMenuEvent(QContextMenuEvent *event)
popup.addAction(tr("Delete all media files"), this, SLOT(removeAllPhotos()));
popup.addAction(tr("Open folder of selected media files"), this, SLOT(openFolderOfSelectedFiles()));
popup.addAction(tr("Recalculate selected thumbnails"), this, SLOT(recalculateSelectedThumbnails()));
popup.addAction(tr("Save dive data as subtitles"), this, SLOT(saveSubtitles()));
popup.exec(event->globalPos());
event->accept();
}
@ -106,6 +109,40 @@ void TabDivePhotos::recalculateSelectedThumbnails()
Thumbnailer::instance()->calculateThumbnails(getSelectedFilenames());
}
void TabDivePhotos::saveSubtitles()
{
QVector<QString> selectedPhotos;
if (!ui->photosView->selectionModel()->hasSelection())
return;
QModelIndexList indexes = ui->photosView->selectionModel()->selectedRows();
if (indexes.count() == 0)
indexes = ui->photosView->selectionModel()->selectedIndexes();
selectedPhotos.reserve(indexes.count());
for (const auto &photo: indexes) {
if (photo.isValid()) {
QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString();
if (!fileUrl.isEmpty()) {
QFileInfo fi = QFileInfo(fileUrl);
QFile subtitlefile;
subtitlefile.setFileName(QString(fi.path()) + "/" + fi.completeBaseName() + ".ass");
int offset = photo.data(Qt::UserRole + 1).toInt();
int duration = photo.data(Qt::UserRole + 2).toInt();
// Only videos have non-zero duration
if (!duration)
continue;
struct membuffer b = { 0 };
save_subtitles_buffer(&b, &displayed_dive, offset, duration);
char *data = detach_buffer(&b);
subtitlefile.open(QIODevice::WriteOnly);
subtitlefile.write(data, strlen(data));
subtitlefile.close();
free(data);
}
}
}
}
//TODO: This looks overly wrong. We shouldn't call MainWindow to retrieve the DiveList to add Images.
void TabDivePhotos::addPhotosFromFile()
{