视频格式转换(avi、wmv、flv、mkv、rmvb、rm、3gp转MP4、MP3)边学边开发
最近想把视频中的音乐提取成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>MP4</ComboBoxItem><ComboBoxItem IsSelected="True">MP3</ComboBoxItem></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>
后台代码,c#编写
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
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>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();}}
}
源码下载:点击下载
视频格式转换(avi、wmv、flv、mkv、rmvb、rm、3gp转MP4、MP3)边学边开发相关推荐
- Java视频格式转换---avi转MP4(h264编码格式)
Java视频格式转换---avi转MP4(h264编码格式的MP4视频在网页播放兼容性更好--试过其它的编码格式,没找到能在网页上直接播放的) 资源借鉴处 需要添加到pom.xml的包 java代码 ...
- Ffmpeg,mencoder视频格式转换
一. Ffmpeg,mencoder视频格式转换 Mencoder 转 flv mencoder -ffourcc FLV1 -lavfopts i_certify_that_my_video_st ...
- mkv格式怎么解封?教你用格式转换的方法把mkv格式转换成avi
mkv格式作为主流格式之一,虽然没有以前那么流行了,但是在下载视频作为素材的时候难免还会遇到mkv格式.而mkv格式是一种封装格式,很多播放器或pr等一些编辑软件是不能直接添加mkv格式的,因此我们常 ...
- mkv视频格式转换mp4格式
如何将mkv视频格式转换mp4格式?由于mkv属于不常用的视频格式,平时使用和接触到的机会也比较少,所以大家对它并不是很了解.mkv最大特点就是能容纳多种不同类型编码的视频.音频及字幕流,甚至囊括了R ...
- MakeMKV for mac(MKV视频格式转换工具)
MakeMKV for mac是一款运行在macOS上专业的MKV视频格式转换工具.Makemkv mac版能够读取受保护的蓝光光盘,并保留视频.音轨.元信息等所有相关数据.并且支持多平台,简单易用. ...
- 如何将mkv视频格式转换mp4格式?
如何将mkv视频格式转换mp4格式?视频格式的种类繁多,其中使用最多.最受欢迎的当属mp4,mp4格式大家再熟悉不过了,我们看的电影和我们拍摄的视频大部分都是mp4格式的.今天小编还要介绍另外一种视频 ...
- mkv视频格式转换mp4格式?
mkv视频格式转换mp4格式?在多种多样的视频格式中有种格式叫mkv,mkv跟mp4都是比较常用的视频格式.mkv是一种多媒体包格式,可以为多种音视频提供外壳,缺点也较为明显,大多数无法直接播放这样的 ...
- 狸窝音频剪辑软件_「附下载链接」常用的5款视频格式转换软件,收藏起来吧...
日常生活中,大家可能会自己制作一些记录生活的小视频,或者作为爱好上传到视频平台,就会用到很多种的视频格式,比如MP4.MKV.WMV.FLV.MOV.DV等等.因为有的视频网站需要特殊格式的视频才能上 ...
- 手机怎样转换php格式,手机视频格式转换方法适合所有手机视频格式转换|如何转换手机视频格式...
怎么转换手机视频格式,是不是所有类型的手机视频格式都支持转换?是的!!!狸窝全能视频转换器支持所有手机视频格式转换,并且转换步骤也非常简单,可以说狸窝视频转换器是目前网上最简单易用功能强大的视频格式转 ...
- 魔影工厂 – 免费简单易用的全能视频格式转换工具
随着手机.PSP.iPad.iPhone等各种各样的数码设备性能和屏幕越来越强大,不少人都希望将一些视频或者电影转换成它们的格式以便随时随地观看.网上视频格式转换软件非常非常的多,之前我们也介绍过格式 ...
最新文章
- python简易爬虫课程设计_python实现简单爬虫功能的示例
- pacs定位线_PACS中MR序列定位线的绘制
- C语言高级编程:如何确定一个变量是有符号还是无符号数
- python方向键控制角色_python方向键控制上下左右代码
- sql注入 练手网站_靶场sql注入练手----sqlmap篇(纯手打)
- Swift 类与结构体
- 计算机网络性能(1)
- 理工科学生计算机要求,理工科学生
- 新计算机 安装win2000,虚拟机安装Windows 2000超详细教程
- 解决Deepin开机锁屏状态下能够使用触控板而解锁之后无法使用触控板的BUG
- android官网m魅族15,魅族15/Plus/Lite等机型现身Android官网:设计惊艳
- Flutter系列之在 macOS 上安装和配置 Flutter 开发环境
- Linux打印添加水印,Linux 下给图片批量加水印
- oracle千万级数据量的表关联更新
- hadoop103: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
- 计算机科学家帕斯卡,帕斯卡-世界上最伟大的科学家排行榜-天天排行网
- 针对X5新内核Blink以及google chrome主要字体主动放大问题
- Visual Studio Code底部状态栏的隐藏及复原
- 华为云服务器宕机,阿里云无人撼动!
- 解决: 利用 Xware 在 centos 上搭建远程下载器