2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#で.aviファイルを.mp4に変換するツールを作ってみた

Last updated at Posted at 2023-06-18

概要

指定参照フォルダにある.aviファイルを指定格納フォルダに.mp4形式に変換するツール。

環境

VisualStudio2019

UI

(ダサすぎ、笑。言い訳:実用用主義なので)
キャプチャ.PNG

変換元:変換したいAVI格納したフォルダを選ぶ
総数:AVI数
保存先:変換したMP4を格納したいフォルダを選ぶ
変換:変換実行
完了:MP4できた数

MainPage.xaml
<Page
    x:Class="mp4Changer.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:mp4Changer"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Height="162" Width="290">

    <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,0,0,0" Width="283">
        <Button x:Name="bt_Start" Content="変換" Height="26" Margin="121,83,0,0" VerticalAlignment="Top" Width="78" Click="Start_Click" FontSize="11"/>
        <TextBlock HorizontalAlignment="Left" Height="16" Margin="249,94,0,0" Text="完了" TextWrapping="Wrap" VerticalAlignment="Top" Width="22" FontSize="11" RenderTransformOrigin="0.568,-0.459"/>
        <TextBox x:Name="tb_FolderFrom" HorizontalAlignment="Left" Height="32" Margin="10,41,0,0" Text="未選択" TextWrapping="Wrap" VerticalAlignment="Top" Width="189" FontSize="9"/>
        <TextBox x:Name="tb_FolderTo" HorizontalAlignment="Left" Height="34" Margin="10,114,0,0" Text="未選択" TextWrapping="Wrap" VerticalAlignment="Top" Width="189" FontSize="9"/>
        <Button x:Name="bt_FolderFrom" Content="変換元" Height="26" Margin="10,10,0,0" VerticalAlignment="Top" Width="78" Click="FolderFrom_Click" FontSize="11"/>
        <Button x:Name="bt_FolderTo" Content="保存先" Height="26" Margin="10,83,0,0" VerticalAlignment="Top" Width="78" Click="FolderTo_Click" FontSize="11"/>
        <TextBox x:Name="tb_CountTo" HorizontalAlignment="Left" Height="34" Margin="204,114,0,0" Text="0" TextWrapping="Wrap" VerticalAlignment="Top" Width="67" FontSize="10"/>
        <TextBox x:Name="tb_CountFrom" HorizontalAlignment="Left" Height="32" Margin="204,41,0,0" Text="0" TextWrapping="Wrap" VerticalAlignment="Top" Width="67" FontSize="10"/>
        <TextBlock HorizontalAlignment="Left" Height="16" Margin="249,23,0,0" Text="総数" TextWrapping="Wrap" VerticalAlignment="Top" Width="22" FontSize="11"/>

    </Grid>
</Page>

Source

※何もフェールセーフを考慮していない。

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using Windows.Media.MediaProperties;
using Windows.Media.Transcoding;

namespace mp4Changer
{

    public sealed partial class MainPage : Page
    {
        Windows.Storage.StorageFolder From_folder;
        Windows.Storage.StorageFolder To_folder;
        string m_Dir; 
        int fileCount;
        int createCount;
        string[] FromFiles;
        FileSystemInfo[] FsInfo;

        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void FolderFrom_Click(object sender, RoutedEventArgs e)
        {
            var folderPicker = new Windows.Storage.Pickers.FolderPicker();
            folderPicker.FileTypeFilter.Add("*");
            From_folder = await folderPicker.PickSingleFolderAsync();

            if (From_folder == null)
            {
                return;
            }

            m_Dir = From_folder.Path.ToString();
            tb_FolderFrom.Text = m_Dir;
            DirectoryInfo DInfo = new DirectoryInfo(m_Dir);
            FsInfo = DInfo.GetFileSystemInfos();
            fileCount = FsInfo.Length;
            tb_CountFrom.Text = fileCount.ToString();
        }

        private async void FolderTo_Click(object sender, RoutedEventArgs e)
        {
            var folderPicker = new Windows.Storage.Pickers.FolderPicker();
            folderPicker.FileTypeFilter.Add("*");
            To_folder = await folderPicker.PickSingleFolderAsync();

            if (To_folder == null)
            {
                return;
            }
            tb_FolderTo.Text = To_folder.Path.ToString();
        }

        private async void Start_Click(object sender, RoutedEventArgs e)
        {
            int int_i = 0; 
            IReadOnlyList<StorageFile> fileList = await From_folder.GetFilesAsync();
            foreach (StorageFile source in fileList)
            {
                Windows.Storage.StorageFile destination = await To_folder.CreateFileAsync(source.DisplayName + ".mp4",Windows.Storage.CreationCollisionOption.OpenIfExists);
                MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p);//HD720pは画像品質、ここはいくつの設定できるので、必要に応じて変更可能。
                MediaTranscoder transcoder = new MediaTranscoder();
                PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(source, destination, profile);
                if (prepareOp.CanTranscode)
                {
                    var transcodeOp = prepareOp.TranscodeAsync();
                    int_i = int_i+1;
                    tb_CountTo.Text = int_i.ToString();
                }
                else
                {
                    return;
                }
            }
        }
    }
}
App.xaml
<Application
    x:Class="mp4Changer.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:mp4Changer">

</Application>
App.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace mp4Changer
{
    sealed partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null)
            {
                rootFrame = new Frame();
                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {

                }

                Window.Current.Content = rootFrame;
            }

            if (e.PrelaunchActivated == false)
            {
                if (rootFrame.Content == null)
                {
                    rootFrame.Navigate(typeof(MainPage), e.Arguments);
                }

                Window.Current.Activate();
            }
        }

        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            deferral.Complete();
        }
    }
}

注意:ファイル作成の部分はフォルダのアクセル権限拒否による問題が発生して、デバックするのにかなり苦労した。解決策として指定フォルダにappによる編集権限を設定するのと指定フォルダをvsと同じフォルダにすること。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?