固定时码

This commit is contained in:
Sch 2023-08-25 05:29:14 +08:00
parent abcfe4997e
commit 62b6a553cc
6 changed files with 33 additions and 15 deletions

View File

@ -39,7 +39,7 @@ FString FFFMPEGUtils::LoadMedia(const FString& Path, FTimelinePropertyData* Prop
avcodec_parameters_to_context(VideoCodecContext, FormatContext->streams[VideoStream]->codecpar); avcodec_parameters_to_context(VideoCodecContext, FormatContext->streams[VideoStream]->codecpar);
AVCodec* VideoCodec = avcodec_find_decoder(VideoCodecContext->codec_id); AVCodec* VideoCodec = avcodec_find_decoder(VideoCodecContext->codec_id);
VideoCodecContext->time_base = AVRational({1, 30}); VideoCodecContext->time_base = AVRational({1, static_cast<int32>(FGlobalData::GlobalFPS)});
VideoCodecContext->gop_size = 1; VideoCodecContext->gop_size = 1;
if (avcodec_open2(VideoCodecContext, VideoCodec, nullptr) < 0) 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_parameters_to_context(VideoCodecContext, FormatContext->streams[VideoStream]->codecpar);
AVCodec* VideoCodec = avcodec_find_decoder(VideoCodecContext->codec_id); AVCodec* VideoCodec = avcodec_find_decoder(VideoCodecContext->codec_id);
VideoCodecContext->time_base = AVRational({1, 30}); VideoCodecContext->time_base = AVRational({1, static_cast<int32>(FGlobalData::GlobalFPS)});
VideoCodecContext->gop_size = 1; VideoCodecContext->gop_size = 1;
if (avcodec_open2(VideoCodecContext, VideoCodec, nullptr) < 0) if (avcodec_open2(VideoCodecContext, VideoCodec, nullptr) < 0)
@ -539,9 +539,9 @@ TArray<FSlateBrush> FFFMPEGUtils::GetAudioBrush(FClipData* ClipData)
FBufferArchive Buffer; FBufferArchive Buffer;
FImageUtils::ExportRenderTarget2DAsPNG(TextureRenderTarget2D, 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(); 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->AudioBrushLength.Add(PicLength);
ClipData->AudioBrushes.Add(SlateBrush); ClipData->AudioBrushes.Add(SlateBrush);
Index++; Index++;

View File

@ -80,7 +80,7 @@ FString FUtils::GetResourcesPath(FString ResourcesName, bool bFullPath)
return FPaths::Combine(FPaths::ProjectDir() + "/Resources/", ResourcesName); return FPaths::Combine(FPaths::ProjectDir() + "/Resources/", ResourcesName);
} }
FString FUtils::GetTempPath() FString FUtils::GetProjectTempPath()
{ {
return FPaths::ConvertRelativePathToFull(FPaths::Combine(FGlobalData::BasePath / FGlobalData::CurrentProjectName / "/Temp/")); return FPaths::ConvertRelativePathToFull(FPaths::Combine(FGlobalData::BasePath / FGlobalData::CurrentProjectName / "/Temp/"));
} }

View File

@ -12,7 +12,7 @@ public:
static uint8* ConvertTwoChannelSound2PortAudioSound(uint8* Channel1, uint8* Channel2, int32 Size); static uint8* ConvertTwoChannelSound2PortAudioSound(uint8* Channel1, uint8* Channel2, int32 Size);
static FString GetResourcesPath(FString ResourcesName, bool bFullPath = false); static FString GetResourcesPath(FString ResourcesName, bool bFullPath = false);
static FString GetTempPath(); static FString GetProjectTempPath();
static FSlateDynamicImageBrush* GetBrushFromImage(const FString& ImageName, const FVector2D Size); static FSlateDynamicImageBrush* GetBrushFromImage(const FString& ImageName, const FVector2D Size);
static TArray<FSlateDynamicImageBrush*> BrushPtr; static TArray<FSlateDynamicImageBrush*> BrushPtr;
@ -71,7 +71,7 @@ public:
inline static TArray<int32> sequence; inline static TArray<int32> sequence;
inline static TArray<UTexture2D*> frames; inline static TArray<UTexture2D*> frames;
inline static int32 currentFrameIndex = 0; inline static int32 currentFrameIndex = 0;
inline static int32 FPS = 30; inline static int32 FPS = FGlobalData::GlobalFPS;
inline static FTimerDelegate videoDele; inline static FTimerDelegate videoDele;
inline static FTimerHandle videoHandle; inline static FTimerHandle videoHandle;
inline static FTimerDelegate updateFrameFinishedDele; inline static FTimerDelegate updateFrameFinishedDele;

View File

@ -737,7 +737,7 @@ void DragDropOperator::OnDrop(const FGeometry& MyGeometry, const FDragDropEvent&
NewClipData.PresetType = EPresetType::Custom; NewClipData.PresetType = EPresetType::Custom;
NewClipData.PresetsCustomData = ClipDragOperation.TimelinePropertyData->PresetsCustomData; NewClipData.PresetsCustomData = ClipDragOperation.TimelinePropertyData->PresetsCustomData;
NewClipData.ClipStartFrame = TrackDropFrame; NewClipData.ClipStartFrame = TrackDropFrame;
NewClipData.ClipEndFrame = NewClipData.ClipStartFrame + static_cast<int32>(NewClipData.PresetsCustomData.Time * 30.0); NewClipData.ClipEndFrame = NewClipData.ClipStartFrame + static_cast<int32>(NewClipData.PresetsCustomData.Time * FGlobalData::GlobalFPS);
NewClipData.ClipType = TrackType; NewClipData.ClipType = TrackType;
} }
else else

View File

@ -252,10 +252,28 @@ void STimelineClip::Seek(int32 Frame)
} }
// FDateTime A = FDateTime::Now(); // FDateTime A = FDateTime::Now();
const int32 Offset = Frame - (ClipData->ClipStartFrame); const int32 Offset = Frame - (ClipData->ClipStartFrame);
const int32 SeekMovieFrame = ClipData->VideoStartFrame + Offset; 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 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; 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) 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 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); av_seek_frame(ThumbnailCodecContext.Context, ThumbnailCodecContext.VideoStream, Timestamp, AVSEEK_FLAG_BACKWARD);
AVPacket Packet; AVPacket Packet;
av_init_packet(&Packet); av_init_packet(&Packet);
@ -947,8 +965,8 @@ void STimelineClip::Tick(const FGeometry& AllottedGeometry, const double InCurre
Texture->UpdateResource(); Texture->UpdateResource();
FGuid Guid = FGuid::NewGuid(); FGuid Guid = FGuid::NewGuid();
FFFMPEGUtils::ExportImage(Texture, *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::GetTempPath() / 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)); FSlateDynamicImageBrush Brush = FSlateDynamicImageBrush(*ClipData->MovieBrushesPath[ClipData->MovieBrushesPath.Num() - 1], FVector2f(0, 0));
ClipData->MovieBrushes.Add(Brush); ClipData->MovieBrushes.Add(Brush);

View File

@ -71,7 +71,7 @@ int32 STimelineTick::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGe
const float Space = FGlobalData::GlobalFPS * FGlobalData::DefaultTimeTickSpace; const float Space = FGlobalData::GlobalFPS * FGlobalData::DefaultTimeTickSpace;
const int32 Multiplier = FMath::GetMappedRangeValueClamped(FVector2D(300, 2), FVector2D(1, 5), Space); 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; const FSlateBrush Brush;
FSlateDrawElement::MakeBox( FSlateDrawElement::MakeBox(