最近想把视频中的音乐提取成mp3,找了好多软件,都不顺手,所以自己动手写了这么一个小软件。主要使用的是:ffmpeg.exe,转换时候带进度条,可以转换一个文件,也可以批量转换文件。
软件共有三部分:
1、选择转换的文件,文件格式可以是avi、wmv、flv、mkv、rmvb、rm、3gp;
2、输出路径和输出格式选择,输出格式为MP3,MP4两种。
3.转换按钮和进度条显示,1和2不设置完成后点击开始转换即可进行视频转换。
演示以下操作流程。
视频转换工具
开发环境Windows10,Microsoft Visual Studio Ultimate 2012,WPF项目。
<Window x:Name="mainForm" x:Class="ConverVideo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="视频转换工具" Height="309.2" Width="832.21" Loaded="MainForm_Loaded" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Background="#FF535353" Foreground="#FF336666" WindowStyle="None" BorderBrush="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}" Icon="logo.ico">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="403*"/>
<ColumnDefinition Width="38*"/>
Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="271*"/>
<RowDefinition Height="50*"/>
Grid.RowDefinitions>
<GroupBox x:Name="SelectFileGB" Header="选择转换文件" HorizontalAlignment="Left" Margin="10,34,0,0" VerticalAlignment="Top" Height="107" Width="677" BorderBrush="#FFBCBCBC" OpacityMask="#FF336666" Foreground="#FFBCBCBC">
<Button x:Name="SelectFileBT" Content="请选择" HorizontalAlignment="Left" Height="70" VerticalAlignment="Top" Width="99" Margin="6,10,0,0" Click="SelectFileBT_Click" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" FontFamily="Arial Black" FontSize="20"/>
GroupBox>
<Label x:Name="SelectFileInofLB" Content="文
件
信
息" HorizontalAlignment="Left" Margin="123,58,0,0" VerticalAlignment="Top" Width="25" Height="70" RenderTransformOrigin="0,0" Foreground="#FFBCBCBC"/>
<ScrollViewer Margin="147,44,77,123.8" BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
<Label x:Name="ll" Content="" Foreground="#FFBCBCBC"/>
ScrollViewer>
<GroupBox x:Name="SelectOutPathGB" Header="输出情况" HorizontalAlignment="Left" Margin="10,156,0,0" VerticalAlignment="Top" Width="677" Height="100" Foreground="#FFBCBCBC">GroupBox>
<Button x:Name="SelectOutPathBT" Content="选择输出目录" HorizontalAlignment="Left" Margin="24,180,0,0" VerticalAlignment="Top" Width="96" Click="SelectOutPathBT_Click" Height="58" FontSize="14" FontFamily="Arial Black"/>
<Label x:Name="OuptFlePathLB" Content="输出路径:" HorizontalAlignment="Left" Margin="127,180,0,0" VerticalAlignment="Top" Width="548" Foreground="#FFBCBCBC"/>
<Label Content="输出类型:" HorizontalAlignment="Left" Margin="127,212,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.434,0.892" Foreground="#FFBCBCBC"/>
<ComboBox x:Name="FileExtentnameGB" HorizontalAlignment="Left" Margin="193,212,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem>MP4ComboBoxItem>
<ComboBoxItem IsSelected="True">MP3ComboBoxItem>
ComboBox>
<Grid Margin="10,9.2,75,12.8" Grid.Row="1">
<ProgressBar Minimum="0" Maximum="100" Value="0" Name="pbStatus" />
<TextBlock Text="{Binding ElementName=pbStatus, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#FF046908" />
Grid>
<Button x:Name="ConverBT" Content="开始转换" HorizontalAlignment="Left" Margin="695,234,0,0" VerticalAlignment="Top" Width="125" Height="65" Click="ConverBT_Click" Grid.RowSpan="2" Grid.ColumnSpan="2" Foreground="#FFE73716" FontSize="20"/>
<Label Content="视频转换" HorizontalAlignment="Left" VerticalAlignment="Top" MouseDown="Label_MouseDown_2" Grid.ColumnSpan="2" Height="31" Background="#FF393939" Foreground="White" Width="830" Padding="30,7,5,5" FontWeight="Bold" FontFamily="Nirmala UI"/>
<Button x:Name="CloseBT" Content="X" Grid.Column="1" HorizontalAlignment="Left" Margin="47,6,0,0" VerticalAlignment="Top" Width="20" Foreground="#FFFB0505" Background="#33DDDDDD" Height="18" Click="CloseBT_Click"/>
<Image HorizontalAlignment="Left" Height="23" Margin="4,2,0,0" VerticalAlignment="Top" Width="23" Source="logo.ico" RenderTransformOrigin="-1.322,0.596"/>
<Image Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="119" Margin="701,43,0,0" VerticalAlignment="Top" Width="119" Source="impic/zsm.png"/>
<Label Content="生活不易,打赏随意" HorizontalAlignment="Left" Margin="702,160,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Width="119" Foreground="#FFF4F3F2"/>
Grid>
Window>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
namespace ConverVideo
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
private OpenFileDialog openFileDialog;
private int OneFiletotaldata = 0,OneFilecurrentdata=0;
private delegate void MyDelegate(int value);
private Dictionary<string, string> FilesList_Current = new Dictionary<string, string>();
private Dictionary<string, string> FilesList_Total = new Dictionary<string, string>();
private int AllFrame_Current = 0, AllFrame_Total = 0;
public MainWindow()
{
InitializeComponent();
}
private void SelectFileBT_Click(object sender, RoutedEventArgs e)
{
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择文件";
openFileDialog.Filter = "视频文件(*.avi,*.wmv,*.flv,*.mkv,*.rmvb,*.rm,*.3gp)|*.avi;*.wmv;*.flv;*.mkv;*.rmvb;*.rm;*.3gp4";
openFileDialog.Multiselect = true;
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
ll.Content = "";
//SelectOldFileTXT.Text = openFileDialog1.FileName;
for (int i = 0; i < openFileDialog.FileNames.Length; i++)
{
ll.Content += openFileDialog.FileNames[i] + "\n";
}
}
}
private void SelectOutPathBT_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
OuptFlePathLB.Content = "输出路径:"+folderBrowserDialog.SelectedPath;
}
}
private void MainForm_Loaded(object sender, RoutedEventArgs e)
{
OuptFlePathLB.Content += AppDomain.CurrentDomain.BaseDirectory;
}
private void ConverBT_Click(object sender, RoutedEventArgs e)
{
if (openFileDialog==null)
{
System.Windows.MessageBox.Show("请选择要转换的文件");
return;
}
ConverBT.IsEnabled = false;
pbStatus.Value = 0;
FilesList_Current.Clear();
FilesList_Total.Clear();
for (int i = 0; i < openFileDialog.FileNames.Length; i++)
{
FilesList_Current.Add("my" + i.ToString(), "0");
FilesList_Total.Add("my" + i.ToString(), "0");
OneFiletotaldata = 0;
OneFilecurrentdata = 0;
string oldfile = openFileDialog.FileNames[i];
string outfilename = OuptFlePathLB.Content.ToString().Replace("输出路径:", "") + System.IO.Path.GetFileNameWithoutExtension(oldfile) + "." + FileExtentnameGB.SelectedValue.ToString().Split(':')[1].Trim();
//ExcuteProcess("ffmpeg.exe", "-progress pro.log -y -i " + oldfile + " " + outfilename + " ",i, Output);
ExcuteProcess("ffmpeg.exe", "-y -i \"" + oldfile + "\" \"" + outfilename + "\" ", i, Output);
}
}
static void ExcuteProcess(string exe, string arg,int index, DataReceivedEventHandler output)
{
using (var p = new Process())
{
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + exe;
p.StartInfo.Arguments = arg;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += output;
p.ErrorDataReceived += output;
p.StartInfo.Verb = "my"+index;
p.Start(); //启动线程
p.BeginOutputReadLine();
p.BeginErrorReadLine();
// p.WaitForExit(); //等待进程结束
}
}
private void Output(object ob, DataReceivedEventArgs dinfo)
{
if (dinfo.Data == null)
{
return;
}
var p = (System.Diagnostics.Process)ob;
// WriteLog(dinfo.Data);
if (dinfo.Data.IndexOf("Duration:") >= 0)
{
MatchCollection mt = Regex.Matches(dinfo.Data, "([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2})");
if (mt.Count > 0)
{
string tempdata = mt[0].ToString().Replace(":", "").Replace(".", "");
OneFiletotaldata = Convert.ToInt32(tempdata);
FilesList_Total[p.StartInfo.Verb] = Convert.ToInt32(tempdata).ToString();
}
}
if (dinfo.Data.IndexOf("size=") >= 0)
{
MatchCollection mt = Regex.Matches(dinfo.Data, "([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2})");
if (mt.Count > 0)
{
string tempdata = mt[0].ToString().Replace(":", "").Replace(".", "");
OneFilecurrentdata = Convert.ToInt32(tempdata);
FilesList_Current[p.StartInfo.Verb] = Convert.ToInt32(tempdata).ToString();
}
}
AllFrame_Total=0;
AllFrame_Current = 0;
foreach (KeyValuePair<string,string> kv in FilesList_Total)
{
AllFrame_Total += Convert.ToInt32(kv.Value);
}
foreach (KeyValuePair<string, string> kv in FilesList_Current)
{
AllFrame_Current += Convert.ToInt32(kv.Value);
}
if (AllFrame_Total > 0 && AllFrame_Current > 0)
{
decimal ttemp = Math.Round(((decimal)AllFrame_Current / AllFrame_Total), 2);
Dispatcher.BeginInvoke(new Action<int>((a) =>
{
this.pbStatus.Value = a;
if (a>=100)
{
this.ConverBT.IsEnabled = true;
}
})
, Convert.ToInt32(ttemp * 100));
}
}
static void WriteLog(string logstr)
{
string filename = "info3.txt";
string path = AppDomain.CurrentDomain.BaseDirectory + "log";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
TextWriter tw = new StreamWriter(Path.Combine(path, filename), true); //true在文件末尾添加数据
tw.WriteLine(DateTime.Now.ToString() + ":" + logstr);
tw.Close();
}
private void Label_MouseDown_2(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragMove();
}
}
private void CloseBT_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
源码下载:点击下载