/// <summary> /// Convert the input video using the specified options. /// /// Note: /// h.264 and .mp4 have timing/cutting issues. h.264 only cuts on the last keyframe, /// which could be several seconds before the time that you actually want to cut. /// /// When cutting an .mp4 (even with MPEG4 video and MP3 audio), the cut will take place /// ~0.5 seconds before it should. /// /// (Is this still true?) /// </summary> public static void convertVideo(string inFile, string audioStream, DateTime startTime, DateTime endTime, ImageSize size, ImageCrop crop, int bitrateVideo, int bitrateAudio, VideoCodec videoCodec, AudioCodec audioCodec, Profilex264 profile, Presetx264 preset, string outFile, DialogProgress dialogProgress) { string videoMapArg = formatVideoMapArg(); string audioMapArg = formatAudioMapArg(audioStream); string videoCodecArg = formatVideoCodecArg(videoCodec); string presetArg = formatPresetFileArg(preset); string keyframeOptionsArg = formatKeyframeOptionsArg(videoCodec); string profileArg = formatProfileFileArg(profile); string videoSizeArg = formatVideoSizeArg(inFile, size, crop, 16, 2); string videoBitrateArg = $"-b:v {bitrateVideo}k"; string audioCodecArg = formatAudioCodecArg(audioCodec); string audioBitrateArg = formatAudioBitrateArg(bitrateAudio); string timeArg = formatStartTimeAndDurationArg(startTime, endTime); string cropArg = formatCropArg(inFile, size, crop); string threadsArg = "-threads 0"; string ffmpegConvertArgs = ""; // Good ffmpeg resource: http://howto-pages.org/ffmpeg/ // 0:0 is assumed to be the video stream // Audio stream: 0:n where n is the number of the audio stream (usually 1) // // Example format: // -y -i "G:\Temp\input.mkv" -ac 2 -map 0:v:0 -map 0:a:0 -codec:v libx264 -preset superfast -g 6 -keyint_min 6 // -fpre "E:\subs2srs\subs2srs\bin\Release\Utils\ffmpeg\presets\libx264-ipod640.ffpreset" // -b:v 800k -codec:a aac -b:a 128k -ss 00:03:32.420 -t 00:02:03.650 -vf "scale 352:202, crop=352:202:0:0" -threads 0 // "C:\Documents and Settings\cb4960\Local Settings\Temp\~subs2srs_temp.mp4" ffmpegConvertArgs = $"-y -i \"{inFile}\" -ac 2 {videoMapArg} {audioMapArg} {videoCodecArg} {presetArg} {keyframeOptionsArg} {profileArg} {videoBitrateArg} {audioCodecArg} {audioBitrateArg}" + $" {timeArg} -vf \"{videoSizeArg}, {cropArg}\" {threadsArg} \"{outFile}\" "; // {14} if (dialogProgress == null) { UtilsCommon.startFFmpeg(ffmpegConvertArgs, true, true); } else { UtilsCommon.startFFmpegProgress(ffmpegConvertArgs, dialogProgress); } }
/// <summary> /// Rip (and re-encode) a portion of the audio from a video file. /// </summary> public static void ripAudioFromVideo(string inFile, string stream, DateTime startTime, DateTime endTime, int bitrate, string outFile, DialogProgress dialogProgress) { string audioBitrateArg = UtilsVideo.formatAudioBitrateArg(bitrate); string audioMapArg = UtilsVideo.formatAudioMapArg(stream); string timeArg = UtilsVideo.formatStartTimeAndDurationArg(startTime, endTime); string ffmpegAudioProgArgs = ""; // Example format: // -vn -y -i "G:\Temp\inputs.mkv" -ac 2 -map 0:1 -ss 00:03:32.420 -t 00:02:03.650 -b:a 128k -threads 0 "output.mp3" ffmpegAudioProgArgs = $"-vn -y -i \"{inFile}\" -ac 2 {audioMapArg} {timeArg} {audioBitrateArg} -threads 0 \"{outFile}\""; // {4} if (dialogProgress == null) { UtilsCommon.startFFmpeg(ffmpegAudioProgArgs, false, true); } else { UtilsCommon.startFFmpegProgress(ffmpegAudioProgArgs, dialogProgress); } }
/// <summary> /// Rip (and re-encode) a portion of the audio from a video file. /// </summary> public static void ripAudioFromVideo(string inFile, string stream, DateTime startTime, DateTime endTime, int bitrate, string outFile, DialogProgress dialogProgress) { string audioBitrateArg = UtilsVideo.formatAudioBitrateArg(bitrate); string audioMapArg = UtilsVideo.formatAudioMapArg(stream); string timeArg = UtilsVideo.formatStartTimeAndDurationArg(startTime, endTime); string ffmpegAudioProgArgs = ""; // Example format: // -vn -y -i "G:\Temp\inputs.mkv" -ac 2 -map 0:1 -ss 00:03:32.420 -t 00:02:03.650 -b:a 128k -threads 0 "output.mp3" ffmpegAudioProgArgs = String.Format("-vn -y -i \"{0}\" -ac 2 {1} {2} {3} -threads 0 \"{4}\"", // Video file inFile, // {0} // Mapping audioMapArg, // {1} // Time span timeArg, // {2} // Bitrate audioBitrateArg, // {3} // Output file name outFile); // {4} if (dialogProgress == null) { UtilsCommon.startFFmpeg(ffmpegAudioProgArgs, false, true); } else { UtilsCommon.startFFmpegProgress(ffmpegAudioProgArgs, dialogProgress); } }