全UIテキストをハードコードされた日本語文字列からTranslateクラス経由の多言語対応リソースへ完全移行しました。
YukkuriMovieMaker.Generatorをgitサブモジュールとして追加しソリューションファイルに組み込み、ソースジェネレータによるローカライズクラス自動生成の基盤を整えました。
ZeroDiskProxy/Localization/ディレクトリを新設し、[AutoGenLocalizer]属性を持つpartialクラスTranslateをTexts.csに定義しました。
.gitmodulesにYukkuriMovieMaker.GeneratorのGitHubリポジトリURLを登録し、サブモジュールとして管理します。
ZeroDiskProxy.slnxにYukkuriMovieMaker.Generatorプロジェクトを追加しました。
ProxyCacheManagerのプロキシ生成完了・キャンセル・失敗時のステータスメッセージをそれぞれTranslate.ProxyGenerationStatusCompleted・Translate.ProxyGenerationStatusCanceled・Translate.ProxyGenerationStatusFailedPrefixに置換しました。
ProxyGenerationItemの初期ステータス文字列をTranslate.ProxyGenerationStatusGeneratingに、格納先表示をTranslate.StorageMemory・Translate.StorageDiskに置換しました。
ZeroDiskProxySettings.NameプロパティをリテラルからTranslate.SettingsNameに変更しました。
SettingsViewModelの初期キャッシュ概要テキストをTranslate.CacheSummaryLoadingに変更しました。
キャッシュ削除ダイアログのメッセージをstring.Concatからstring.Format(Translate.CacheNoEntriesToDelete)およびstring.Format(Translate.CacheDeleteSummaryFormat, ...)に変更しました。
ダイアログタイトルをリテラルからTranslate.AppNameに変更しました。
プラグイン未初期化メッセージ・キャッシュ件数表示・メモリおよびディスク使用量表示・割当済メモリ表示をいずれもstring.FormatとTranslateキーに置き換えました。
CacheEntryViewModelのストレージ種別表示をTranslate.StorageMemory・Translate.StorageDiskに置換しました。
GenerationPopupView.xamlにxmlns:local名前空間宣言を追加し、ウィンドウタイトル・ヘッダ文言・空状態メッセージをすべて{x:Static local:Translate.XXX}バインディングに変更しました。
ZeroDiskProxySettingsView.xamlにxmlns:local名前空間宣言を追加し、設定カード説明文・全Expanderヘッダ・全ラベル・全ツールチップ・全ボタン文言・キャッシュ一覧ヘッダおよび空状態メッセージをすべて{x:Static local:Translate.XXX}バインディングに変更しました。
151 行
5.5 KiB
C#
151 行
5.5 KiB
C#
using System.IO;
|
|
using Windows.Media.MediaProperties;
|
|
using Windows.Media.Transcoding;
|
|
using Windows.Storage;
|
|
using ZeroDiskProxy.Memory;
|
|
using ZeroDiskProxy.Progress;
|
|
|
|
namespace ZeroDiskProxy.Core;
|
|
|
|
internal sealed class MfProxyEncoder : IProxyEncoder
|
|
{
|
|
private readonly MemoryBudget _memoryBudget;
|
|
private readonly string _fallbackDirectory;
|
|
private int _disposed;
|
|
|
|
internal MfProxyEncoder(MemoryBudget memoryBudget, string fallbackDirectory)
|
|
{
|
|
_memoryBudget = memoryBudget;
|
|
_fallbackDirectory = fallbackDirectory;
|
|
}
|
|
|
|
public async Task<ProxyCacheEntry> EncodeAsync(
|
|
string inputPath, float scale, float bitrateFactor, int gopSize,
|
|
ProxyGenerationItem? progressItem, CancellationToken cancellationToken)
|
|
{
|
|
ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
|
|
|
|
Directory.CreateDirectory(_fallbackDirectory);
|
|
var tempFileName = string.Concat("zdp_", Guid.NewGuid().ToString("N"), ".mp4");
|
|
var tempPath = Path.Combine(_fallbackDirectory, tempFileName);
|
|
ProxyCacheEntry? entry = null;
|
|
|
|
try
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
var inputFile = await StorageFile.GetFileFromPathAsync(inputPath);
|
|
var outputFolder = await StorageFolder.GetFolderFromPathAsync(_fallbackDirectory);
|
|
var outputFile = await outputFolder.CreateFileAsync(tempFileName, CreationCollisionOption.ReplaceExisting);
|
|
|
|
var sourceProfile = await MediaEncodingProfile.CreateFromFileAsync(inputFile);
|
|
uint origWidth = sourceProfile?.Video?.Width ?? 1920u;
|
|
uint origHeight = sourceProfile?.Video?.Height ?? 1080u;
|
|
if (origWidth == 0) origWidth = 1920;
|
|
if (origHeight == 0) origHeight = 1080;
|
|
|
|
double fps = 30.0;
|
|
if (sourceProfile?.Video?.FrameRate is { Numerator: > 0, Denominator: > 0 } fr)
|
|
fps = (double)fr.Numerator / fr.Denominator;
|
|
|
|
var proxyWidth = AlignTo16((uint)(origWidth * scale));
|
|
var proxyHeight = AlignTo16((uint)(origHeight * scale));
|
|
|
|
var profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
|
|
profile.Video = VideoEncodingProperties.CreateH264();
|
|
profile.Video.Width = proxyWidth;
|
|
profile.Video.Height = proxyHeight;
|
|
profile.Video.Bitrate = Math.Max((uint)(proxyWidth * proxyHeight * fps * 0.07 * bitrateFactor), 100_000u);
|
|
profile.Video.FrameRate.Numerator = (uint)Math.Max(1, Math.Round(fps));
|
|
profile.Video.FrameRate.Denominator = 1;
|
|
profile.Audio = null;
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
var transcoder = new MediaTranscoder
|
|
{
|
|
HardwareAccelerationEnabled = true,
|
|
VideoProcessingAlgorithm = MediaVideoProcessingAlgorithm.Default
|
|
};
|
|
|
|
var prepResult = await transcoder.PrepareFileTranscodeAsync(inputFile, outputFile, profile);
|
|
|
|
if (!prepResult.CanTranscode)
|
|
{
|
|
transcoder.HardwareAccelerationEnabled = false;
|
|
prepResult = await transcoder.PrepareFileTranscodeAsync(inputFile, outputFile, profile);
|
|
}
|
|
|
|
if (!prepResult.CanTranscode)
|
|
throw new InvalidOperationException(string.Concat("Transcode failed: ", prepResult.FailureReason.ToString()));
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
var transcodeOp = prepResult.TranscodeAsync();
|
|
|
|
if (progressItem is not null)
|
|
{
|
|
transcodeOp.Progress = static (_, pct) => { };
|
|
transcodeOp.Progress = (_, pct) =>
|
|
{
|
|
var p = Math.Min(99.0, pct);
|
|
System.Windows.Application.Current?.Dispatcher.BeginInvoke(
|
|
System.Windows.Threading.DispatcherPriority.Background,
|
|
() => { progressItem.Progress = p; });
|
|
};
|
|
}
|
|
|
|
using var reg = cancellationToken.Register(static state => ((Windows.Foundation.IAsyncOperationWithProgress<TranscodeFailureReason, double>)state!).Cancel(), transcodeOp);
|
|
await transcodeOp;
|
|
|
|
float effectiveScale = (float)proxyWidth / origWidth;
|
|
entry = new ProxyCacheEntry(inputPath, effectiveScale)
|
|
{
|
|
ProxyWidth = proxyWidth,
|
|
ProxyHeight = proxyHeight
|
|
};
|
|
|
|
var fileSize = new FileInfo(tempPath).Length;
|
|
if (_memoryBudget.CanAllocateInMemory(fileSize))
|
|
{
|
|
var data = await File.ReadAllBytesAsync(tempPath, cancellationToken);
|
|
_memoryBudget.RecordAllocation(data.Length);
|
|
entry.SetMemoryData(data);
|
|
TryDeleteFile(tempPath);
|
|
}
|
|
else
|
|
{
|
|
entry.SetDiskPath(tempPath);
|
|
}
|
|
|
|
return entry;
|
|
}
|
|
catch
|
|
{
|
|
entry?.Dispose();
|
|
TryDeleteFile(tempPath);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private static uint AlignTo16(uint value)
|
|
{
|
|
var aligned = (value + 15u) & ~15u;
|
|
return aligned < 16u ? 16u : aligned;
|
|
}
|
|
|
|
private static void TryDeleteFile(string path)
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(path))
|
|
File.Delete(path);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Interlocked.Exchange(ref _disposed, 1);
|
|
}
|
|
} |