<commit_before>﻿using Screna;
using Screna.Audio;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;

namespace Captura.Models
{
    /// <summary>
    /// Encode Video using FFMpeg.exe
    /// </summary>
    public class FFMpegMuxedWriter : IVideoFileWriter
    {
        static readonly string BaseDir = Path.Combine(Path.GetTempPath(), "Screna.FFMpeg");
        readonly AudioFileWriter _audioWriter;
        readonly FFMpegVideoWriter _videoWriter;
        readonly string _ffmpegArgs, tempVideoPath, tempAudioPath;

        /// <summary>
        /// Creates a new instance of <see cref="FFMpegMuxedWriter"/>.
        /// </summary>
        /// <param name="FilePath">Path for the output file.</param>
        /// <param name="FrameRate">Video Frame Rate.</param>
        public FFMpegMuxedWriter(string FilePath, int FrameRate, int VideoQuality, FFMpegVideoArgsProvider VideoArgsProvider, int AudioQuality, FFMpegAudioArgsProvider AudioArgsProvider, IAudioProvider AudioProvider)
        {
            if (AudioProvider == null)
                throw new ArgumentNullException(nameof(AudioProvider), $"{nameof(AudioProvider)} can't be null. Use {nameof(FFMpegVideoWriter)} instead.");

            if (!Directory.Exists(BaseDir))
                Directory.CreateDirectory(BaseDir);

            var fileName = Path.GetFileName(FilePath);
            tempVideoPath = Path.Combine(BaseDir, fileName);
            tempAudioPath = Path.Combine(BaseDir, Path.ChangeExtension(fileName, ".wav"));
            
            _audioWriter = new AudioFileWriter(tempAudioPath, AudioProvider.WaveFormat);

            _videoWriter = new FFMpegVideoWriter(tempVideoPath, FrameRate, VideoQuality, VideoArgsProvider);
            
            _ffmpegArgs = $"-i {tempVideoPath} -vcodec copy -i {tempAudioPath} {AudioArgsProvider(AudioQuality)} \"{FilePath}\"";
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            _audioWriter.Dispose();
            _videoWriter.Dispose();

            var ffmpegProcess = new Process
            {
                StartInfo =
                {
                    FileName = ServiceProvider.FFMpegExePath,
                    Arguments = _ffmpegArgs,
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };

            ffmpegProcess.Start();
            ffmpegProcess.WaitForExit();
            
            File.Delete(tempAudioPath);
            File.Delete(tempVideoPath);<commit_msg>Fixed Command-line args for FFMpegMuxedWriter<commit_after>﻿using Screna;
using Screna.Audio;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;

namespace Captura.Models
{
    /// <summary>
    /// Encode Video using FFMpeg.exe
    /// </summary>
    public class FFMpegMuxedWriter : IVideoFileWriter
    {
        static readonly string BaseDir = Path.Combine(Path.GetTempPath(), "Screna.FFMpeg");
        readonly AudioFileWriter _audioWriter;
        readonly FFMpegVideoWriter _videoWriter;
        readonly string _ffmpegArgs, tempVideoPath, tempAudioPath;

        /// <summary>
        /// Creates a new instance of <see cref="FFMpegMuxedWriter"/>.
        /// </summary>
        /// <param name="FilePath">Path for the output file.</param>
        /// <param name="FrameRate">Video Frame Rate.</param>
        public FFMpegMuxedWriter(string FilePath, int FrameRate, int VideoQuality, FFMpegVideoArgsProvider VideoArgsProvider, int AudioQuality, FFMpegAudioArgsProvider AudioArgsProvider, IAudioProvider AudioProvider)
        {
            if (AudioProvider == null)
                throw new ArgumentNullException(nameof(AudioProvider), $"{nameof(AudioProvider)} can't be null. Use {nameof(FFMpegVideoWriter)} instead.");

            if (!Directory.Exists(BaseDir))
                Directory.CreateDirectory(BaseDir);

            var fileName = Path.GetFileName(FilePath);
            tempVideoPath = Path.Combine(BaseDir, fileName);
            tempAudioPath = Path.Combine(BaseDir, Path.ChangeExtension(fileName, ".wav"));
            
            _audioWriter = new AudioFileWriter(tempAudioPath, AudioProvider.WaveFormat);

            _videoWriter = new FFMpegVideoWriter(tempVideoPath, FrameRate, VideoQuality, VideoArgsProvider);
            
            _ffmpegArgs = $"-i {tempVideoPath} -i {tempAudioPath} {AudioArgsProvider(AudioQuality)} -vcodec copy \"{FilePath}\"";
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            _audioWriter.Dispose();
            _videoWriter.Dispose();

            var ffmpegProcess = new Process
            {
                StartInfo =
                {
                    FileName = ServiceProvider.FFMpegExePath,
                    Arguments = _ffmpegArgs,
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };

            ffmpegProcess.Start();
            ffmpegProcess.WaitForExit();
            
            File.Delete(tempAudioPath);
            File.Delete(tempVideoPath);
{'fraction_non_alphanumeric': 0.09606094388703085, 'fraction_numerical': 0.0, 'mean_word_length': 2.2272182254196644, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5102052', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1368, 'n_words': 313}