/// <summary> /// Take a snapshot of a video at the provided time. /// </summary> public static void takeSnapshotFromVideo(string inFile, DateTime snapTime, ImageSize size, ImageCrop crop, string outFile) { string startTimeArg = UtilsVideo.formatStartTimeArg(snapTime); string videoSizeArg = UtilsVideo.formatVideoSizeArg(inFile, size, crop, 2, 2); string cropArg = UtilsVideo.formatCropArg(inFile, size, crop); string ffmpegSnapshotProgArgs = ""; // Example format: // -y -an -ss 00:03:33.370 -i "G:\Temp\input.mkv" -s 358x202 -f image2 -vf crop=358:202:0:0 -vframes 1 "output.jpg" ffmpegSnapshotProgArgs = String.Format("-y -an {0} -i \"{1}\" -f image2 -vf \"{2}, {3}\" -vframes 1 \"{4}\"", // Time to take snapshot at startTimeArg, // {0} // Filename inFile, // {1} // Filters videoSizeArg, // {2} cropArg, // {3} // Output name outFile); // {4} UtilsCommon.startFFmpeg(ffmpegSnapshotProgArgs, false, true); }
/// <summary> /// Extract a video clip from a longer video clip without re-encoding. /// </summary> public static void cutVideo(string inFile, DateTime startTime, DateTime endTime, string outFile) { string startTimeArg = UtilsVideo.formatStartTimeArg(startTime); string durationArg = UtilsVideo.formatDurationArg(startTime, endTime); string timeArg = formatStartTimeAndDurationArg(startTime, endTime); string ffmpegCutArgs = ""; // Example format: // -y -i -ss 00:00:00.000 -t "input.avi" -t 00:00:01.900 -c copy "output.avi" // Note: The order of the arguments is strange, but unless -ss comes before -i, // the video will SOMETIMES fail to copy and the output will consist // of only the audio. Lots of experimentation involved. ffmpegCutArgs = String.Format("-y {0} -i \"{1}\" {2} -c copy \"{3}\"", // Start time startTimeArg, // {0} // Input video file name inFile, // {1} // Duration durationArg, // {2} // Output video file name outFile); // {3} UtilsCommon.startFFmpeg(ffmpegCutArgs, false, true); }
/// <summary> /// Convert audio file to another format (ex. mp3 -> wav). /// </summary> public static void convertAudioFormat(string mp3File, string outFile, int numChannels) { string ffmpegAudioProgArgs = ""; // Examples: // -y -i "input.mp3"" -ac 2 -threads 0 "output.wav" ffmpegAudioProgArgs = $"-y -i \"{mp3File}\" -ac {numChannels} -threads 0 \"{outFile}\""; // {2} UtilsCommon.startFFmpeg(ffmpegAudioProgArgs, false, true); }
/// <summary> /// Extract an audio clip from a longer audio clip without re-encoding. /// </summary> public static void cutAudio(string fileToCut, DateTime startTime, DateTime endTime, string outFile) { string timeArg = UtilsVideo.formatStartTimeAndDurationArg(startTime, endTime); string audioCodecArg = UtilsVideo.formatAudioCodecArg(UtilsVideo.AudioCodec.COPY); string ffmpegAudioProgArgs = ""; // Example format: //-y -i "input.mp3" -ss 00:00:00.000 -t 00:00:01.900 -codec:a copy "output.mp3" ffmpegAudioProgArgs = $"-y -i \"{fileToCut}\" {timeArg} {audioCodecArg} \"{outFile}\""; // {3} UtilsCommon.startFFmpeg(ffmpegAudioProgArgs, false, true); }
/// <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) the entire audio from a video file. /// </summary> public static void ripAudioFromVideo(string inFile, int bitrate, string outFile) { string audioBitrateArg = UtilsVideo.formatAudioBitrateArg(bitrate); string ffmpegAudioProgArgs = ""; // Example format: // -vn -y -i "G:\Temp\inputs.mkv" -ac 2 -b:a 128k "output.mp3" ffmpegAudioProgArgs = String.Format("-vn -y -i \"{1}\" -ac 2 {1} -threads 0 \"{2}\"", inFile, // {0} audioBitrateArg, // {1} outFile); // {2} UtilsCommon.startFFmpeg(ffmpegAudioProgArgs, true, true); }
/// <summary> /// Take a snapshot of a video at the provided time. /// </summary> public static void takeSnapshotFromVideo(string inFile, DateTime snapTime, ImageSize size, ImageCrop crop, string outFile) { string startTimeArg = UtilsVideo.formatStartTimeArg(snapTime); string videoSizeArg = UtilsVideo.formatVideoSizeArg(inFile, size, crop, 2, 2); string cropArg = UtilsVideo.formatCropArg(inFile, size, crop); string ffmpegSnapshotProgArgs = ""; // Example format: // -y -an -ss 00:03:33.370 -i "G:\Temp\input.mkv" -s 358x202 -f image2 -vf crop=358:202:0:0 -vframes 1 "output.jpg" ffmpegSnapshotProgArgs = $"-y -an {startTimeArg} -i \"{inFile}\" -f image2 -vf \"{videoSizeArg}, {cropArg}\" -vframes 1 \"{outFile}\""; // {4} UtilsCommon.startFFmpeg(ffmpegSnapshotProgArgs, false, true); }
/// <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> /// Extract an audio clip from a longer audio clip without re-encoding. /// </summary> public static void cutAudio(string fileToCut, DateTime startTime, DateTime endTime, string outFile) { string timeArg = UtilsVideo.formatStartTimeAndDurationArg(startTime, endTime); string audioCodecArg = UtilsVideo.formatAudioCodecArg(UtilsVideo.AudioCodec.COPY); string ffmpegAudioProgArgs = ""; // Example format: //-y -i "input.mp3" -ss 00:00:00.000 -t 00:00:01.900 -codec:a copy "output.mp3" ffmpegAudioProgArgs = String.Format("-y -i \"{0}\" {1} {2} \"{3}\"", // Input file fileToCut, // {0} // Time span timeArg, // {1} // Audio codec audioCodecArg, // {2} // Output file (including full path) outFile); // {3} UtilsCommon.startFFmpeg(ffmpegAudioProgArgs, false, true); }
/// <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); } }