2022-11-22 11:10:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
2023-01-26 18:50:41 +00:00
|
|
|
use OCA\Memories\Exif;
|
2022-11-22 11:10:25 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
|
|
class LivePhoto
|
|
|
|
{
|
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
|
|
|
public function __construct(IDBConnection $connection)
|
|
|
|
{
|
|
|
|
$this->connection = $connection;
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
/**
|
|
|
|
* Check if a given Exif data is the video part of a Live Photo.
|
|
|
|
*/
|
|
|
|
public function isVideoPart(array $exif): bool
|
2022-11-22 11:10:25 +00:00
|
|
|
{
|
2022-11-25 15:32:24 +00:00
|
|
|
return \array_key_exists('MIMEType', $exif)
|
|
|
|
&& 'video/quicktime' === $exif['MIMEType']
|
|
|
|
&& \array_key_exists('ContentIdentifier', $exif);
|
2022-11-22 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get liveid from photo part */
|
2023-04-14 02:43:13 +00:00
|
|
|
public function getLivePhotoId(File $file, array $exif)
|
2022-11-22 11:10:25 +00:00
|
|
|
{
|
2022-11-22 16:54:19 +00:00
|
|
|
// Apple JPEG (MOV has ContentIdentifier)
|
2022-11-22 11:10:25 +00:00
|
|
|
if (\array_key_exists('MediaGroupUUID', $exif)) {
|
|
|
|
return $exif['MediaGroupUUID'];
|
|
|
|
}
|
|
|
|
|
2023-03-30 00:02:22 +00:00
|
|
|
// Google MVIMG and Samsung JPEG
|
2023-03-29 23:58:34 +00:00
|
|
|
if (\array_key_exists('MicroVideoOffset', $exif) && ($videoLength = $exif['MicroVideoOffset']) > 0) {
|
|
|
|
// As explained in the following issue,
|
|
|
|
// https://github.com/pulsejet/memories/issues/468
|
|
|
|
//
|
|
|
|
// MicroVideoOffset is the length of the video in bytes
|
|
|
|
// and the video is located at the end of the file.
|
|
|
|
//
|
|
|
|
// Note that we could have just used "self__trailer" here,
|
|
|
|
// since exiftool can extract the video from the trailer,
|
|
|
|
// but explicitly specifying the offset is much faster because
|
|
|
|
// we don't need to spawn exiftool to read the video file.
|
2023-03-30 00:02:22 +00:00
|
|
|
//
|
|
|
|
// For Samsung JPEG, we can also check for EmbeddedVideoType
|
|
|
|
// and subsequently extract the video file using the
|
|
|
|
// EmbeddedVideoFile binary prop, but setting the offset
|
|
|
|
// is faster for the same reason mentioned above.
|
2023-03-29 23:58:34 +00:00
|
|
|
$videoOffset = $file->getSize() - $videoLength;
|
|
|
|
|
|
|
|
return "self__traileroffset={$videoOffset}";
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:51:52 +00:00
|
|
|
// Google JPEG and Samsung HEIC / JPEG (Apple?)
|
2022-11-22 16:54:19 +00:00
|
|
|
if (\array_key_exists('MotionPhoto', $exif)) {
|
|
|
|
if ('image/jpeg' === $exif['MIMEType']) {
|
2023-01-26 18:50:41 +00:00
|
|
|
// Google Motion Photo JPEG
|
|
|
|
|
|
|
|
// We need to read the DirectoryItemLength key to get the length of the video
|
|
|
|
// These keys are duplicate, one for the image and one for the video
|
|
|
|
// With exiftool -G4, we get the following:
|
|
|
|
//
|
|
|
|
// "Unknown:DirectoryItemSemantic": "Primary"
|
|
|
|
// "Unknown:DirectoryItemLength": 0
|
|
|
|
// "Copy1:DirectoryItemSemantic": "MotionPhoto"
|
|
|
|
// "Copy1:DirectoryItemLength": 3011435 // <-- this is the length of the video
|
|
|
|
//
|
|
|
|
// The video is then located at the end of the file, so we can get the offset.
|
|
|
|
// Match each DirectoryItemSemantic to find MotionPhoto, then get the length.
|
|
|
|
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
|
|
|
|
$extExif = Exif::getExifWithDuplicates($path);
|
|
|
|
|
|
|
|
foreach ($extExif as $key => $value) {
|
|
|
|
if (str_ends_with($key, ':DirectoryItemSemantic')) {
|
|
|
|
if ('MotionPhoto' === $value) {
|
|
|
|
$videoLength = $extExif[str_replace('Semantic', 'Length', $key)];
|
|
|
|
if (\is_int($videoLength) && $videoLength > 0) {
|
|
|
|
$videoOffset = $file->getSize() - $videoLength;
|
|
|
|
|
2023-03-29 23:58:34 +00:00
|
|
|
return "self__traileroffset={$videoOffset}";
|
2023-01-26 18:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback: video should hopefully be in trailer
|
2022-11-22 16:54:19 +00:00
|
|
|
return 'self__trailer';
|
|
|
|
}
|
2023-10-05 06:51:52 +00:00
|
|
|
|
2023-10-05 06:52:11 +00:00
|
|
|
if ('image/heic' === $exif['MIMEType']) {
|
2023-10-05 06:51:52 +00:00
|
|
|
// Samsung HEIC -- no way to get this out yet (DirectoryItemLength is senseless)
|
|
|
|
// The reason this is above the MotionPhotoVideo check is because extracting binary
|
|
|
|
// EXIF fields on the fly is extremely expensive compared to trailer extraction.
|
2022-11-22 16:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:51:52 +00:00
|
|
|
// Samsung HEIC (at least S21)
|
|
|
|
if (\array_key_exists('MotionPhotoVideo', $exif) && !empty($exif['MotionPhotoVideo'])) {
|
|
|
|
// It's a binary exif field, decode when the user requests it
|
|
|
|
return 'self__exifbin=MotionPhotoVideo';
|
|
|
|
}
|
|
|
|
|
2022-11-22 11:10:25 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
/**
|
|
|
|
* Process video part of Live Photo.
|
|
|
|
*/
|
2023-04-14 04:59:01 +00:00
|
|
|
public function processVideoPart(File $file, array $exif): bool
|
2022-11-22 11:10:25 +00:00
|
|
|
{
|
|
|
|
$fileId = $file->getId();
|
|
|
|
$mtime = $file->getMTime();
|
|
|
|
$liveid = $exif['ContentIdentifier'];
|
|
|
|
if (empty($liveid)) {
|
2023-04-14 04:59:01 +00:00
|
|
|
return false;
|
2022-11-22 11:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('fileid')
|
|
|
|
->from('memories_livephoto')
|
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
2023-04-14 04:59:01 +00:00
|
|
|
$prevRow = $query->executeQuery()->fetch();
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'liveid' => $query->createNamedParameter($liveid, IQueryBuilder::PARAM_STR),
|
|
|
|
'mtime' => $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT),
|
|
|
|
'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
|
|
|
|
'orphan' => $query->createNamedParameter(false, IQueryBuilder::PARAM_BOOL),
|
|
|
|
];
|
|
|
|
|
2023-04-15 18:32:02 +00:00
|
|
|
if ($prevRow) {
|
|
|
|
$query->update('memories_livephoto')
|
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
$query->set($key, $value);
|
2022-11-22 11:10:25 +00:00
|
|
|
}
|
2023-04-15 18:32:02 +00:00
|
|
|
} else {
|
|
|
|
$query->insert('memories_livephoto')->values($params);
|
2022-11-22 11:10:25 +00:00
|
|
|
}
|
2023-04-15 18:32:02 +00:00
|
|
|
|
|
|
|
return $query->executeStatement() > 0;
|
2022-11-22 11:10:25 +00:00
|
|
|
}
|
2023-04-14 02:43:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete entry from memories_livephoto table.
|
|
|
|
*/
|
|
|
|
public function deleteVideoPart(File $file): void
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete('memories_livephoto')
|
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($file->getId(), IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
|
|
|
$query->executeStatement();
|
|
|
|
}
|
2022-11-22 11:10:25 +00:00
|
|
|
}
|