From 5666f14829900e78bb8d2200c05e07eaa59dd64e Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Mon, 27 Mar 2017 15:50:40 +0300
Subject: [PATCH] Allow audio and video files with duration < 1s.

---
 .../SourceFiles/media/media_clip_reader.cpp   | 38 ++++++++++---------
 .../SourceFiles/storage/localimageloader.cpp  |  4 +-
 .../SourceFiles/storage/localimageloader.h    |  4 +-
 3 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/Telegram/SourceFiles/media/media_clip_reader.cpp b/Telegram/SourceFiles/media/media_clip_reader.cpp
index b9e6477f2..300d7547d 100644
--- a/Telegram/SourceFiles/media/media_clip_reader.cpp
+++ b/Telegram/SourceFiles/media/media_clip_reader.cpp
@@ -849,26 +849,28 @@ FileLoadTask::Video PrepareForSending(const QString &fname, const QByteArray &da
 	auto reader = std::make_unique<internal::FFMpegReaderImplementation>(&localLocation, &localData, playId);
 	if (reader->start(internal::ReaderImplementation::Mode::Inspecting, seekPositionMs)) {
 		auto durationMs = reader->durationMs();
-		result.isGifv = reader->isGifv();
-		if (!result.isGifv) {
-			auto middleMs = durationMs / 2;
-			if (!reader->inspectAt(middleMs)) {
-				return result;
+		if (durationMs > 0) {
+			result.isGifv = reader->isGifv();
+			if (!result.isGifv) {
+				auto middleMs = durationMs / 2;
+				if (!reader->inspectAt(middleMs)) {
+					return result;
+				}
 			}
-		}
-		auto hasAlpha = false;
-		auto readResult = reader->readFramesTill(-1, getms());
-		auto readFrame = (readResult == internal::ReaderImplementation::ReadResult::Success);
-		if (readFrame && reader->renderFrame(result.thumbnail, hasAlpha, QSize())) {
-			if (hasAlpha) {
-				auto cacheForResize = QImage();
-				auto request = FrameRequest();
-				request.framew = request.outerw = result.thumbnail.width();
-				request.frameh = request.outerh = result.thumbnail.height();
-				request.factor = 1;
-				result.thumbnail = PrepareFrameImage(request, result.thumbnail, hasAlpha, cacheForResize);
+			auto hasAlpha = false;
+			auto readResult = reader->readFramesTill(-1, getms());
+			auto readFrame = (readResult == internal::ReaderImplementation::ReadResult::Success);
+			if (readFrame && reader->renderFrame(result.thumbnail, hasAlpha, QSize())) {
+				if (hasAlpha) {
+					auto cacheForResize = QImage();
+					auto request = FrameRequest();
+					request.framew = request.outerw = result.thumbnail.width();
+					request.frameh = request.outerh = result.thumbnail.height();
+					request.factor = 1;
+					result.thumbnail = PrepareFrameImage(request, result.thumbnail, hasAlpha, cacheForResize);
+				}
+				result.duration = static_cast<int>(durationMs / 1000);
 			}
-			result.duration = static_cast<int>(durationMs / 1000);
 		}
 	}
 	return result;
diff --git a/Telegram/SourceFiles/storage/localimageloader.cpp b/Telegram/SourceFiles/storage/localimageloader.cpp
index 3f2921af6..4491ad597 100644
--- a/Telegram/SourceFiles/storage/localimageloader.cpp
+++ b/Telegram/SourceFiles/storage/localimageloader.cpp
@@ -251,7 +251,7 @@ bool FileLoadTask::CheckForSong(const QString &filepath, const QByteArray &conte
 	}
 
 	auto media = Media::Player::PrepareForSending(filepath, content);
-	if (media.duration <= 0) {
+	if (media.duration < 0) {
 		return false;
 	}
 	if (!ValidateThumbDimensions(media.cover.width(), media.cover.height())) {
@@ -275,7 +275,7 @@ bool FileLoadTask::CheckForVideo(const QString &filepath, const QByteArray &cont
 	}
 
 	auto media = Media::Clip::PrepareForSending(filepath, content);
-	if (media.duration <= 0) {
+	if (media.duration < 0) {
 		return false;
 	}
 
diff --git a/Telegram/SourceFiles/storage/localimageloader.h b/Telegram/SourceFiles/storage/localimageloader.h
index a44ce4a69..3614c22a1 100644
--- a/Telegram/SourceFiles/storage/localimageloader.h
+++ b/Telegram/SourceFiles/storage/localimageloader.h
@@ -244,14 +244,14 @@ public:
 		bool animated = false;
 	};
 	struct Song {
-		int duration = 0;
+		int duration = -1;
 		QString title;
 		QString performer;
 		QImage cover;
 	};
 	struct Video {
 		bool isGifv = false;
-		int duration = 0;
+		int duration = -1;
 		QImage thumbnail;
 	};
 	struct MediaInformation {