From 62b6a553cc90fd4dd5092898d3f5b0b8ad53d658 Mon Sep 17 00:00:00 2001 From: Sch <3516520171@qq.com> Date: Fri, 25 Aug 2023 05:29:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BA=E5=AE=9A=E6=97=B6=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/Cut5/Utils/FFMPEGUtils.cpp | 8 ++--- Source/Cut5/Utils/Utils.cpp | 2 +- Source/Cut5/Utils/Utils.h | 4 +-- .../DragDropOperator/DragDropOperator.cpp | 2 +- Source/Cut5/Widgets/STimelineClip.cpp | 30 +++++++++++++++---- Source/Cut5/Widgets/STimelineTick.cpp | 2 +- 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Source/Cut5/Utils/FFMPEGUtils.cpp b/Source/Cut5/Utils/FFMPEGUtils.cpp index cf016ff..23899a3 100644 --- a/Source/Cut5/Utils/FFMPEGUtils.cpp +++ b/Source/Cut5/Utils/FFMPEGUtils.cpp @@ -39,7 +39,7 @@ FString FFFMPEGUtils::LoadMedia(const FString& Path, FTimelinePropertyData* Prop avcodec_parameters_to_context(VideoCodecContext, FormatContext->streams[VideoStream]->codecpar); AVCodec* VideoCodec = avcodec_find_decoder(VideoCodecContext->codec_id); - VideoCodecContext->time_base = AVRational({1, 30}); + VideoCodecContext->time_base = AVRational({1, static_cast(FGlobalData::GlobalFPS)}); VideoCodecContext->gop_size = 1; if (avcodec_open2(VideoCodecContext, VideoCodec, nullptr) < 0) @@ -198,7 +198,7 @@ FString FFFMPEGUtils::LoadContextPure(const FString& Path, FTimelinePropertyData avcodec_parameters_to_context(VideoCodecContext, FormatContext->streams[VideoStream]->codecpar); AVCodec* VideoCodec = avcodec_find_decoder(VideoCodecContext->codec_id); - VideoCodecContext->time_base = AVRational({1, 30}); + VideoCodecContext->time_base = AVRational({1, static_cast(FGlobalData::GlobalFPS)}); VideoCodecContext->gop_size = 1; if (avcodec_open2(VideoCodecContext, VideoCodec, nullptr) < 0) @@ -539,9 +539,9 @@ TArray FFFMPEGUtils::GetAudioBrush(FClipData* ClipData) FBufferArchive Buffer; FImageUtils::ExportRenderTarget2DAsPNG(TextureRenderTarget2D, Buffer); - FFileHelper::SaveArrayToFile(Buffer, *FPaths::Combine(FUtils::GetTempPath(), ClipData->ClipGuid.ToString(), FString::FromInt(Index) + ".png")); + FFileHelper::SaveArrayToFile(Buffer, *FPaths::Combine(FUtils::GetProjectTempPath(), ClipData->ClipGuid.ToString(), FString::FromInt(Index) + ".png")); TextureRenderTarget2D->MarkAsGarbage(); - FSlateDynamicImageBrush SlateBrush = FSlateDynamicImageBrush(*FPaths::Combine(FUtils::GetTempPath(), ClipData->ClipGuid.ToString(), FString::FromInt(Index) + ".png"), FVector2D(PicLength, FGlobalData::DefaultTrackHeight)); + FSlateDynamicImageBrush SlateBrush = FSlateDynamicImageBrush(*FPaths::Combine(FUtils::GetProjectTempPath(), ClipData->ClipGuid.ToString(), FString::FromInt(Index) + ".png"), FVector2D(PicLength, FGlobalData::DefaultTrackHeight)); ClipData->AudioBrushLength.Add(PicLength); ClipData->AudioBrushes.Add(SlateBrush); Index++; diff --git a/Source/Cut5/Utils/Utils.cpp b/Source/Cut5/Utils/Utils.cpp index 13e3696..5bdba2d 100644 --- a/Source/Cut5/Utils/Utils.cpp +++ b/Source/Cut5/Utils/Utils.cpp @@ -80,7 +80,7 @@ FString FUtils::GetResourcesPath(FString ResourcesName, bool bFullPath) return FPaths::Combine(FPaths::ProjectDir() + "/Resources/", ResourcesName); } -FString FUtils::GetTempPath() +FString FUtils::GetProjectTempPath() { return FPaths::ConvertRelativePathToFull(FPaths::Combine(FGlobalData::BasePath / FGlobalData::CurrentProjectName / "/Temp/")); } diff --git a/Source/Cut5/Utils/Utils.h b/Source/Cut5/Utils/Utils.h index aa5bbda..d58386e 100644 --- a/Source/Cut5/Utils/Utils.h +++ b/Source/Cut5/Utils/Utils.h @@ -12,7 +12,7 @@ public: static uint8* ConvertTwoChannelSound2PortAudioSound(uint8* Channel1, uint8* Channel2, int32 Size); static FString GetResourcesPath(FString ResourcesName, bool bFullPath = false); - static FString GetTempPath(); + static FString GetProjectTempPath(); static FSlateDynamicImageBrush* GetBrushFromImage(const FString& ImageName, const FVector2D Size); static TArray BrushPtr; @@ -71,7 +71,7 @@ public: inline static TArray sequence; inline static TArray frames; inline static int32 currentFrameIndex = 0; - inline static int32 FPS = 30; + inline static int32 FPS = FGlobalData::GlobalFPS; inline static FTimerDelegate videoDele; inline static FTimerHandle videoHandle; inline static FTimerDelegate updateFrameFinishedDele; diff --git a/Source/Cut5/Widgets/DragDropOperator/DragDropOperator.cpp b/Source/Cut5/Widgets/DragDropOperator/DragDropOperator.cpp index 72c0989..5c752a8 100644 --- a/Source/Cut5/Widgets/DragDropOperator/DragDropOperator.cpp +++ b/Source/Cut5/Widgets/DragDropOperator/DragDropOperator.cpp @@ -737,7 +737,7 @@ void DragDropOperator::OnDrop(const FGeometry& MyGeometry, const FDragDropEvent& NewClipData.PresetType = EPresetType::Custom; NewClipData.PresetsCustomData = ClipDragOperation.TimelinePropertyData->PresetsCustomData; NewClipData.ClipStartFrame = TrackDropFrame; - NewClipData.ClipEndFrame = NewClipData.ClipStartFrame + static_cast(NewClipData.PresetsCustomData.Time * 30.0); + NewClipData.ClipEndFrame = NewClipData.ClipStartFrame + static_cast(NewClipData.PresetsCustomData.Time * FGlobalData::GlobalFPS); NewClipData.ClipType = TrackType; } else diff --git a/Source/Cut5/Widgets/STimelineClip.cpp b/Source/Cut5/Widgets/STimelineClip.cpp index 6b61177..3aae8d2 100644 --- a/Source/Cut5/Widgets/STimelineClip.cpp +++ b/Source/Cut5/Widgets/STimelineClip.cpp @@ -252,10 +252,28 @@ void STimelineClip::Seek(int32 Frame) } // FDateTime A = FDateTime::Now(); + + + + + const int32 Offset = Frame - (ClipData->ClipStartFrame); - const int32 SeekMovieFrame = ClipData->VideoStartFrame + Offset; - int64 Timestamp = av_rescale_q(SeekMovieFrame / 30.0 * AV_TIME_BASE, AVRational{1, AV_TIME_BASE}, ClipData->ResourcePropertyDataPtr->Context->streams[ClipData->ResourcePropertyDataPtr->VideoStream]->time_base); - if (SeekMovieFrame - LastSeekFrame > 1 || SeekMovieFrame - LastSeekFrame < -1 || LastSeekFrame == 0) + int32 SeekMovieFrame = ClipData->VideoStartFrame + Offset; + + int32 VideoFPS = ClipData->ResourcePropertyDataPtr->VideoCodecContext->framerate.num; + if (VideoFPS < FGlobalData::GlobalFPS) + { + const double Interval = FGlobalData::GlobalFPS / VideoFPS; + SeekMovieFrame = float(SeekMovieFrame) / Interval; + } + else + { + VideoFPS = FGlobalData::GlobalFPS; + } + + + int64 Timestamp = av_rescale_q(float(SeekMovieFrame) / float(VideoFPS) * AV_TIME_BASE, AVRational{1, AV_TIME_BASE}, ClipData->ResourcePropertyDataPtr->Context->streams[ClipData->ResourcePropertyDataPtr->VideoStream]->time_base); + if (VideoFPS < FGlobalData::GlobalFPS || SeekMovieFrame - LastSeekFrame > 1 || SeekMovieFrame - LastSeekFrame < 0 || LastSeekFrame == 0) { AVRational frame_rate = ClipData->ResourcePropertyDataPtr->Context->streams[ClipData->ResourcePropertyDataPtr->VideoStream]->avg_frame_rate; if (av_seek_frame(ClipData->ResourcePropertyDataPtr->Context, ClipData->ResourcePropertyDataPtr->VideoStream, Timestamp, AVSEEK_FLAG_BACKWARD) < 0) @@ -896,7 +914,7 @@ void STimelineClip::Tick(const FGeometry& AllottedGeometry, const double InCurre } const int32 CurrentFrame = ClipData->MovieBrushNum - (ClipData->MovieBrushNum - ClipData->MovieBrushes.Num()); - const int64 Timestamp = av_rescale_q(CurrentFrame / 30.0 * AV_TIME_BASE, AVRational{1, AV_TIME_BASE}, ThumbnailCodecContext.Context->streams[ThumbnailCodecContext.VideoStream]->time_base); + const int64 Timestamp = av_rescale_q(CurrentFrame / FGlobalData::GlobalFPS * AV_TIME_BASE, AVRational{1, AV_TIME_BASE}, ThumbnailCodecContext.Context->streams[ThumbnailCodecContext.VideoStream]->time_base); av_seek_frame(ThumbnailCodecContext.Context, ThumbnailCodecContext.VideoStream, Timestamp, AVSEEK_FLAG_BACKWARD); AVPacket Packet; av_init_packet(&Packet); @@ -947,8 +965,8 @@ void STimelineClip::Tick(const FGeometry& AllottedGeometry, const double InCurre Texture->UpdateResource(); FGuid Guid = FGuid::NewGuid(); - FFFMPEGUtils::ExportImage(Texture, *FPaths::Combine(FUtils::GetTempPath() / ClipData->ClipGuid.ToString() / Guid.ToString() + ".png")); - ClipData->MovieBrushesPath.Add(FPaths::Combine(FUtils::GetTempPath() / ClipData->ClipGuid.ToString() / Guid.ToString() + ".png")); + FFFMPEGUtils::ExportImage(Texture, *FPaths::Combine(FUtils::GetProjectTempPath() / ClipData->ClipGuid.ToString() / Guid.ToString() + ".png")); + ClipData->MovieBrushesPath.Add(FPaths::Combine(FUtils::GetProjectTempPath() / ClipData->ClipGuid.ToString() / Guid.ToString() + ".png")); FSlateDynamicImageBrush Brush = FSlateDynamicImageBrush(*ClipData->MovieBrushesPath[ClipData->MovieBrushesPath.Num() - 1], FVector2f(0, 0)); ClipData->MovieBrushes.Add(Brush); diff --git a/Source/Cut5/Widgets/STimelineTick.cpp b/Source/Cut5/Widgets/STimelineTick.cpp index ef2069f..d930183 100644 --- a/Source/Cut5/Widgets/STimelineTick.cpp +++ b/Source/Cut5/Widgets/STimelineTick.cpp @@ -71,7 +71,7 @@ int32 STimelineTick::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGe const float Space = FGlobalData::GlobalFPS * FGlobalData::DefaultTimeTickSpace; const int32 Multiplier = FMath::GetMappedRangeValueClamped(FVector2D(300, 2), FVector2D(1, 5), Space); - if (j % (Multiplier * 30) == 0) + if (j % (Multiplier * int32(FGlobalData::GlobalFPS)) == 0) { const FSlateBrush Brush; FSlateDrawElement::MakeBox(