Skip to content

Commit

Permalink
基本实现了读取视频主要编码设置并应用到当前转码设置的功能:通过将文件拖放到视频编码设置上方
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 24, 2023
1 parent 024b932 commit 6de3014
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 7 deletions.
105 changes: 99 additions & 6 deletions SimpleFFmpegGUI.Core/Manager/MediaInfoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,24 @@ await Task.Run(() =>
return mediaInfo;
}

public static async Task<string> GetSnapshotAsync(string path, TimeSpan time,string scale)
public static async Task<string> GetSnapshotAsync(string path, TimeSpan time, string scale)
{
string tempPath = FileSystemUtility.GetTempFileName("snapshot") + ".bmp";
FFmpegProcess process = new FFmpegProcess($"-ss {time.TotalSeconds:0.000} -i \"{path}\" -vframes 1 -vf scale={scale} {tempPath}");
await process.StartAsync(null, null);
return tempPath;
}

public static async Task<TimeSpan> GetVideoDurationByFFprobeAsync(string path)
public static TimeSpan GetVideoDurationByFFprobe(string path)
{
return (await FFProbe.AnalyseAsync(path)).Duration;
return FFProbe.Analyse(path).Duration;
}

public static TimeSpan GetVideoDurationByFFprobe(string path)
public static async Task<TimeSpan> GetVideoDurationByFFprobeAsync(string path)
{
return FFProbe.Analyse(path).Duration;
return (await FFProbe.AnalyseAsync(path)).Duration;
}

private static JObject GetMediaInfoProcessOutput(string path)
{
string tmpFile = FileSystemUtility.GetTempFileName("mediainfo");
Expand All @@ -66,7 +67,6 @@ private static JObject GetMediaInfoProcessOutput(string path)
return JObject.Parse(output);
}


/// <summary>
/// 解析编码设置(由NewBing生成)
/// </summary>
Expand Down Expand Up @@ -138,5 +138,98 @@ private static MediaInfoGeneral ParseMediaInfoJSON(JObject json)
}
return info;
}

public static VideoCodeArguments ConvertToVideoArguments(MediaInfoGeneral mediaInfo)
{
VideoCodeArguments arguments = new VideoCodeArguments();
var tracks = JObject.Parse(mediaInfo.Raw)["media"]["track"] as JArray;
if (mediaInfo.Videos.Count == 0)
{
throw new Exception("源文件不含视频");
}
var video = mediaInfo.Videos[0];

if (video.EncodingSettings != null && video.EncodingSettings.Count > 0)
{
var settings = video.EncodingSettings.ToDictionary(p => p.Name, p => p.Value);
try
{
arguments = new VideoCodeArguments();
if (settings["rc"].Equals("crf"))
{
if (settings.ContainsKey("crf"))
{
arguments.Crf = Convert.ToInt32(settings["crf"]);
}
}
else if (settings["rc"].Equals("abr"))
{
arguments.AverageBitrate = Convert.ToDouble(settings["bitrate"]) / 1000;
if (Convert.ToDouble(settings["stats-read"]) > 0)
{
arguments.TwoPass = true;
}
}
if (settings.ContainsKey("vbv-maxrate"))
{
arguments.MaxBitrate = Convert.ToDouble(settings["vbv-maxrate"]) / 1000;
arguments.MaxBitrateBuffer = Convert.ToDouble(settings["vbv-bufsize"]) / 1000 / arguments.MaxBitrate;
}
}
catch (Exception ex)
{
throw;
}

int preset = 0;
if (settings.ContainsKey("no-signhide"))
{
preset = 8;
}
else if (settings.ContainsKey("no-sao"))
{
preset = 7;
}
else if (settings["subme"].Equals(1))
{
preset = 6;
}
else if (settings["ref"].Equals(2))
{
preset = 5;
}
else if (settings["max-merge"].Equals(2))
{
preset = 4;
}
else if (settings["ref"].Equals(3))
{
preset = 3;
}
else if (settings["ref"].Equals(4))
{
preset = 2;
}
else if (settings["max-merge"].Equals(4))
{
preset = 1;
}
arguments.Preset = preset;
}
else
{
throw new Exception("源视频未提供编码设置信息,无法转换为输出参数");
}

arguments.Code = video.Format switch
{
"AVC" => "H264",
"HEVC" => "H265",
_ => video.Format
};


return arguments;
}
}
}
21 changes: 21 additions & 0 deletions SimpleFFmpegGUI.WPF/Pages/MediaInfoPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
x:Key="KeyStyle"
TargetType="{x:Type TextBlock}">
<Setter Property="Width" Value="160" />
<Setter Property="Padding" Value="2,8,12,8" />
</Style>

</UserControl.Resources>
Expand Down Expand Up @@ -160,6 +161,26 @@
Text="旋转" />
<TextBlock Text="{Binding Rotation}" />
</StackPanel>
<Expander
Header="编码设置"
Visibility="{Binding EncodingSettings, Converter={StaticResource CountMoreThanZeroConverter}}">
<ItemsControl ItemsSource="{Binding EncodingSettings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock
VerticalAlignment="Center"
Style="{StaticResource KeyStyle}"
Text="{Binding Name}"
TextWrapping="Wrap" />
<TextBlock
VerticalAlignment="Center"
Text="{Binding Value}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
</ui:SimpleStackPanel>
</GroupBox>
</DataTemplate>
Expand Down
1 change: 1 addition & 0 deletions SimpleFFmpegGUI.WPF/Panels/CodeArgumentsPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:ui="http://schemas.modernwpf.com/2019"
d:DesignHeight="450"
d:DesignWidth="800"
AllowDrop="True"
mc:Ignorable="d">
<UserControl.Resources>
<local:Int2StringConverter x:Key="Int2StringConverter" />
Expand Down
49 changes: 49 additions & 0 deletions SimpleFFmpegGUI.WPF/Panels/CodeArgumentsPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -274,6 +275,54 @@ public OutputArguments GetOutputArguments()
return ViewModel.GetArguments();
}

protected override void OnDragOver(DragEventArgs e)
{
base.OnDragOver(e);
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
if (files.Length == 1 && File.Exists(files[0]))
{
e.Effects = DragDropEffects.Link;
}
}
}

protected override async void OnDrop(DragEventArgs e)
{
base.OnDrop(e);
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
if (files.Length == 1 && File.Exists(files[0]))
{
var file = files[0];
try
{
var info = await MediaInfoManager.GetMediaInfoAsync(file);
var videoArgs = MediaInfoManager.ConvertToVideoArguments(info);
ViewModel.Video = videoArgs.Adapt<VideoArgumentsWithSwitch>();
if(videoArgs.Crf.HasValue)
{
ViewModel.Video.EnableCrf = true;
}
if(videoArgs.AverageBitrate.HasValue)
{
ViewModel.Video.EnableAverageBitrate= true;
}
if(videoArgs.MaxBitrate.HasValue)
{
ViewModel.Video.EnableMaxBitrate= true;
}
}
catch (Exception ex)
{
this.CreateMessage().QueueError("解析视频编码参数失败", ex);
}
}
}
}

public CodeArgumentsPanelViewModel ViewModel { get; } = App.ServiceProvider.GetService<CodeArgumentsPanelViewModel>();
}

Expand Down
6 changes: 5 additions & 1 deletion 日志.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,4 +643,8 @@

【Core】【Web】网页端新增支持进程优先级设置

【WPF】修复了完整状态面板与底栏重合的BUG
【WPF】修复了完整状态面板与底栏重合的BUG

## 20230524

【Core】【WPF】基本实现了读取视频主要编码设置并应用到当前转码设置的功能:通过将文件拖放到视频编码设置上方。

0 comments on commit 6de3014

Please sign in to comment.