视频格式转换(avi、wmv、flv、mkv、rmvb、rm、3gp转MP4、MP3)边学边开发

这篇具有很好参考价值的文章主要介绍了视频格式转换(avi、wmv、flv、mkv、rmvb、rm、3gp转MP4、MP3)边学边开发。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

最近想把视频中的音乐提取成mp3,找了好多软件,都不顺手,所以自己动手写了这么一个小软件。主要使用的是:ffmpeg.exe,转换时候带进度条,可以转换一个文件,也可以批量转换文件。

一、先看一下软件如何使用

视频格式转换,自己开发的小工具,wpf,microsoft,c#
软件共有三部分:
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="&#x000A;&#x000A;&#x000A;" 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();
        }
    }
}

源码下载:点击下载文章来源地址https://www.toymoban.com/news/detail-587198.html

到了这里,关于视频格式转换(avi、wmv、flv、mkv、rmvb、rm、3gp转MP4、MP3)边学边开发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 怎么把avi文件转换成mp4视频格式,4个高能方法

           怎么把avi文件转换成mp4视频格式? 当您下载到avi格式的视频文件时,您可能会选择将其转换为MP4格式的文件。 avi是一种由微软开发的多媒体容器格式,尽管现在已经被认为是老旧的技术,但由于其简单易懂的开发API和Windows的通用性,仍被广泛使用。虽然avi格式视频

    2024年02月06日
    浏览(44)
  • avi怎么转换成视频?

    nbsp; nbsp; avi怎么转换成视频?在我们日常使用的视频格式中,AVI是一种常见且经常被使用的音频视频交叉格式之一。它的优点之一是占用的存储空间相对较小,但也明显存在着画质损失的缺点。虽然AVI格式的视频在某种程度上也很常见,但与最常见的MP4格式视频相比,无论如

    2024年02月08日
    浏览(27)
  • 20230403在WIN10下通过ffmpeg调用NVIDIA的硬件加速wmv视频转码为MP4格式

    20230403在WIN10下通过ffmpeg调用NVIDIA的硬件加速wmv视频转码为MP4格式 2023/4/3 15:50 最近向学习日语,找到日语发音的视频中,大多数是MP4格式,少量是WMV格式,PR2023貌似不能识别WMV格式。 于是:万能的ffmpeg上场了!   手动指定编解码器 通过 ffmpeg -codecs | findstr \\\"vc1\\\" 查看 vc1 的编解

    2023年04月22日
    浏览(35)
  • 解决video标签无法播放avi格式的视频

    错误代码 正确代码 在video标签中不要加src属性,必须在video标签内加source标签,兼容不同浏览器解码支持。

    2024年02月13日
    浏览(37)
  • 视频转码实例:把MP4转为MKV视频,一键批量转换的操作方法

    在数字媒体时代,视频格式的多样性是不可避免的。经常把MP4格式的视频转换为MKV格式。MKV格式有较高的音频和视频质量,能在其他设备或软件上播放视频。以下是云炫AI智剪如何把MP4视频转为MKV格式的一键批量转换操作方法。 已转码的mkv视频效果缩略图展示。   Mp4视频批量

    2024年01月18日
    浏览(39)
  • 【Vue】播放flv格式视频(flv.js视频播放器)

     图片来源:HTTP-FLV直播初探   github地址:GitHub - bilibili/flv.js: HTML5 FLV Player npm install flv.js --save 封装一个组件flvVideo hasAudio设置为true就不展示。。。不知道为啥~ home.vue 参考:Vue 中使用flv.js视频播放器

    2024年02月14日
    浏览(57)
  • 如何使用ffmpeg将BDMV(m2ts)转换成MKV、MP4等其他格式的文件

    BDMV 是蓝光碟使用的格式。这种格式没有办法使用播放软件播放,必须要用硬盘播放器,也就是专门的设备。但是最经典的 ffmpeg 可以将其转换成其他格式,并且保持相同的码率和清晰度,这样就可以很方便的查看了。 本文使用 macOS 进行演示,但是会介绍如何一些其他平台的

    2024年02月10日
    浏览(56)
  • Java类jar 实现 转视频格式 fvl转mp4,avi示例

    依赖: 视频格式转换示例:

    2024年02月11日
    浏览(33)
  • flv怎么转换成mp4格式?准备3个方法给大家

    nbsp; nbsp; flv怎么转换成mp4格式?FLV是一种流行的视频文件格式,最初由Adobe公司开发,用于在Web上播放和传输视频内容。FLV格式以其较小的文件大小和较高的压缩比而闻名,并广泛应用于在线视频分享平台、流媒体服务和网络广告等领域。能够提供较高质量的视频和音频体验

    2024年02月07日
    浏览(46)
  • 视频转码:掌握mp4视频格式转FLV视频的技巧,视频批量剪辑方法

    在多媒体时代,视频格式的转换成为一种常见的需求。把MP4格式转换为FLV格式,FLV格式的视频文件通常具有较小的文件大小,同时保持了较好的视频质量。批量剪辑视频的方法能大大提高工作效率。下面来看云炫AI智剪如何进行MP4到FLV的转码,如何批量剪辑视频的方法。 转码

    2024年01月19日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包