Skip to content

Commit

Permalink
去掉NReco的引用,直接使用ffmeg.exe进行最后的视频合成
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjinming1062 committed Jun 30, 2020
1 parent 2ad84cf commit e6baa4e
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 32 deletions.
2 changes: 1 addition & 1 deletion App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RecordWin"
StartupUri="Code/MainWindow.xaml">
Startup="OnStartup">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
16 changes: 15 additions & 1 deletion App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Windows;
using System;
using System.IO;
using System.Windows;

namespace RecordWin
{
Expand All @@ -7,5 +9,17 @@ namespace RecordWin
/// </summary>
public partial class App : Application
{
private void OnStartup(object sender, StartupEventArgs e)
{
string zipPath = Path.Combine(AppContext.BaseDirectory, "RecordLid.zip");
if (File.Exists(zipPath))//依赖文件以压缩包存储,启动时检测压缩包,如果存在则解压
{
var SevenZip = new SevenZipNET.SevenZipExtractor(zipPath);
SevenZip.ExtractAll(AppContext.BaseDirectory);
File.Delete(zipPath);//解压完成移除压缩包
}
MainWindow win = new MainWindow();
win.Show();
}
}
}
34 changes: 34 additions & 0 deletions Code/Functions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecordWin
{
public static class Functions
{
/// <summary>
/// 调用cmd命令
/// </summary>
public static void CMD(string args)
{
Process cmd = new Process();
cmd.StartInfo.FileName = string.Format("cmd.exe");
cmd.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
cmd.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
cmd.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
cmd.StartInfo.RedirectStandardError = true;//重定向标准错误输出
cmd.StartInfo.CreateNoWindow = true;//不显示程序窗口
cmd.Start();//启动程序
cmd.StandardInput.WriteLine(args + "&exit");
cmd.StandardInput.AutoFlush = true;
cmd.StandardInput.Close();
cmd.BeginOutputReadLine();
cmd.BeginErrorReadLine();
cmd.WaitForExit();
cmd.Close();
}
}
}
54 changes: 33 additions & 21 deletions Code/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using NAudio.Wave;
using NReco.VideoConverter;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
Expand Down Expand Up @@ -334,29 +333,43 @@ internal void StopRecord(bool ShowErr = true, bool CloseCamera = true)
//Convert后的MP4体积更小但清晰度没什么影响,所以无论有无声音都进行一次转换处理
if (SettingHelp.Settings.桌面 || SettingHelp.Settings.摄像头)//没有视频则不转换
{
Thread thread = new Thread(() =>
{
for (int i = 0; i < 10; i++)
{
Dispatcher.Invoke(() => { waitBar.Value = i; });
Thread.Sleep(1000);
}
});//起一个线程让进度条动起来
thread.Start();
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
string tempVideo = curVideoName, tempAudio = curAudioName;//运行未完成转换再次开始录制,所以这里需要把当前转换中的文件名记录下来
var ffMpeg = new FFMpegConverter();
ffMpeg.ConvertProgress += FfMpeg_ConvertProgress;
FFMpegInput[] input = SettingHelp.Settings.声音 ? new FFMpegInput[] { new FFMpegInput(tempVideo), new FFMpegInput(tempAudio) } : new FFMpegInput[] { new FFMpegInput(tempVideo) };
var setting = new OutputSettings();
setting.CustomOutputArgs = "-crf 12";
ffMpeg.ConvertMedia(input, MakeFilePath(SettingHelp.Settings.编码类型), SettingHelp.Settings.编码类型, setting);
try
string tempVideo = curVideoName;
string tempAudio = curAudioName;
string outfile = MakeFilePath(SettingHelp.Settings.编码类型);
tempAudio = SettingHelp.Settings.声音 ? $"-i \"{tempAudio}\"" : "";
string ffmpeg = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ffmpeg.exe");
string cmd = $"ffmpeg -i {tempVideo} {tempAudio} -acodec copy {outfile} -crf 12";
Functions.CMD(cmd);
while (true)
{
if (File.Exists(tempVideo) && !SettingHelp.Settings.保留视频) File.Delete(tempVideo);
}
catch { }//防止文件被占用删除失败
try
{
if (File.Exists(tempAudio) && !SettingHelp.Settings.保留音频) File.Delete(tempAudio);//合成后移除音频文件
try//防止文件被占用删除失败
{
if (File.Exists(tempVideo) && !SettingHelp.Settings.保留视频) File.Delete(tempVideo);
if (File.Exists(tempAudio) && !SettingHelp.Settings.保留音频) File.Delete(tempAudio);//合成后移除音频文件
break;
}
catch
{
Thread.Sleep(1000);
}
}
catch { }
Dispatcher.Invoke(() =>
{
barGrid.Visibility = Visibility.Collapsed;
});
{
barGrid.Visibility = Visibility.Collapsed;
btBegin.Visibility = Visibility.Visible;
thread?.Abort();
});
});
}
}
Expand All @@ -365,7 +378,6 @@ internal void StopRecord(bool ShowErr = true, bool CloseCamera = true)
if (ShowErr) Message(ex.Message);
}
}
private void FfMpeg_ConvertProgress(object sender, ConvertProgressEventArgs e) => Dispatcher.Invoke(() => { waitBar.Value = (waitBar.Value + 1) % 10; });
private void btBegin_Click(object sender, RoutedEventArgs e)
{
if (!SettingHelp.Settings.桌面 && !SettingHelp.Settings.摄像头 && !SettingHelp.Settings.声音)
Expand Down
Binary file removed Dlls/NReco.VideoConverter.dll
Binary file not shown.
Binary file added NeededDlls/RecordLid.zip
Binary file not shown.
Binary file removed NeededDlls/avcodec-53.dll
Binary file not shown.
Binary file removed NeededDlls/avdevice-53.dll
Binary file not shown.
Binary file removed NeededDlls/avfilter-2.dll
Binary file not shown.
Binary file removed NeededDlls/avformat-53.dll
Binary file not shown.
Binary file removed NeededDlls/avutil-51.dll
Binary file not shown.
Binary file removed NeededDlls/swscale-2.dll
Binary file not shown.
24 changes: 15 additions & 9 deletions RecordWin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -64,6 +66,9 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="7z.NET, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\7z.NET.1.0.3\lib\net463\7z.NET.dll</HintPath>
</Reference>
<Reference Include="AForge">
<HintPath>Dlls\AForge.dll</HintPath>
</Reference>
Expand All @@ -79,9 +84,6 @@
<Reference Include="NAudio">
<HintPath>Dlls\NAudio.dll</HintPath>
</Reference>
<Reference Include="NReco.VideoConverter">
<HintPath>Dlls\NReco.VideoConverter.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
Expand All @@ -107,6 +109,7 @@
<Compile Include="Code\CameraShow.xaml.cs">
<DependentUpon>CameraShow.xaml</DependentUpon>
</Compile>
<Compile Include="Code\Functions.cs" />
<Compile Include="Code\SettingWindow.xaml.cs">
<DependentUpon>SettingWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -167,6 +170,8 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="NeededDlls\RecordLid.zip" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand All @@ -176,18 +181,19 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="NeededDlls\avcodec-53.dll" />
<Content Include="NeededDlls\avdevice-53.dll" />
<Content Include="NeededDlls\avfilter-2.dll" />
<Content Include="NeededDlls\avformat-53.dll" />
<Content Include="NeededDlls\avutil-51.dll" />
<Resource Include="Resources\RecordWin.ico" />
<None Include="Resources\激光.cur" />
<Content Include="NeededDlls\swscale-2.dll" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>xcopy /e /r /s /y "$(ProjectDir)NeededDlls" "$(ProjectDir)bin\Debug\"</PostBuildEvent>
</PropertyGroup>
<Import Project="packages\7z.NET.1.0.3\build\7z.NET.targets" Condition="Exists('packages\7z.NET.1.0.3\build\7z.NET.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\7z.NET.1.0.3\build\7z.NET.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\7z.NET.1.0.3\build\7z.NET.targets'))" />
</Target>
</Project>
4 changes: 4 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="7z.NET" version="1.0.3" targetFramework="net48" />
</packages>

0 comments on commit e6baa4e

Please sign in to comment.