public void Open() { FFmpegLoader.EnsureLoaded(); if (closed) { throw new InvalidOperationException("Cannot reopen closed video writer"); fixed(AVFormatContext **fctx = &ctx) avformat_alloc_output_context2(fctx, null, null, Filename); this.fmt = ctx->oformat; if (fmt->video_codec != AVCodecID.AV_CODEC_ID_NONE) { AddStream(ref videoStream, ref videoCodec, fmt->video_codec); hasVideo = true; } if (fmt->audio_codec != AVCodecID.AV_CODEC_ID_NONE) { AddStream(ref audioStream, ref audioCodec, fmt->audio_codec); hasAudio = true; } int ret; if (hasVideo) { OpenVideo(); } if (hasAudio) { OpenAudio(); } // av_dump_format(ctx, 0, Filename, 1); ret = avio_open(&ctx->pb, Filename, AVIO_FLAG_WRITE); if (ret < 0) { throw new FFmpegException("Could not open file for writing", ret); } ret = avformat_write_header(ctx, null); if (ret < 0) { throw new FFmpegException("Could not write file header", ret); } }
/// <summary> /// Creates an empty FFmpeg format container for encoding. /// </summary> /// <param name="extension">A output file extension. It is used only to guess the container format.</param> /// <returns>A new instance of the <see cref="OutputContainer"/>.</returns> /// <remarks>Before you write frames to the container, you must call the <see cref="CreateFile(string)"/> method to create an output file.</remarks> public static OutputContainer Create(string extension) { FFmpegLoader.LoadFFmpeg(); var format = ffmpeg.av_guess_format(null, "x." + extension, null); if (format == null) { throw new NotSupportedException($"Cannot find a container format for the \"{extension}\" file extension."); } var formatContext = ffmpeg.avformat_alloc_context(); formatContext->oformat = format; return(new OutputContainer(formatContext)); }
public static void Main(string[] args) { var success = FFmpegLoader.Load(FFmpegPath); if (!success) { Console.WriteLine("Could not load FFmpeg"); return; } Console.WriteLine($"Loaded FFmpeg v{FFmpegLoader.FFmpegVersion}"); var bitmap = (Bitmap)Image.FromFile(ImagePath); var audio = CodecFactory.Instance.GetCodec(AudioPath) .ToSampleSource(); using (var writer = new VideoWriter(OutputPath)) { writer.Open(); float[] audioData = new float[writer.AudioSamplesPerFrame]; while (true) { if (writer.WriteVideo) { // Write a video frame writer.WriteVideoFrame(bitmap); } else { // Write an audio frame int read = audio.Read(audioData, 0, audioData.Length); if (read <= 0) { break; } writer.WriteAudioFrame(audioData); } } } }
private static AVFormatContext *MakeContext(string url, MediaOptions options, AVFormatContextDelegate contextDelegate) { FFmpegLoader.LoadFFmpeg(); var context = ffmpeg.avformat_alloc_context(); options.DemuxerOptions.ApplyFlags(context); var dict = new FFDictionary(options.DemuxerOptions.PrivateOptions, false).Pointer; contextDelegate(context); ffmpeg.avformat_open_input(&context, url, null, &dict) .ThrowIfError("An error occurred while opening the file"); ffmpeg.avformat_find_stream_info(context, null) .ThrowIfError("Cannot find stream info"); return(context); }
private void OnExecute() { if (!File.Exists(AudioFile)) { Console.WriteLine("Audio file does not exist"); return; } if (!File.Exists(WallpaperFile)) { Console.WriteLine("Wallpaper file does not exist"); return; } if (OutputFile == null) { Console.WriteLine("Output file is required"); return; } bool generateLyrics = LyricsFile != null; if (generateLyrics && !File.Exists(LyricsFile)) { Console.WriteLine("Lyrics file does not exist"); return; } if (FPS <= 0 || FPS > 120) { Console.WriteLine("FPS outside of range 1...120 are not supported"); return; } Console.WriteLine("Loading FFmpeg..."); var ffmpegOk = FFmpegLoader.Load("Libraries"); if (!ffmpegOk) { Console.WriteLine("FFmpeg libraries not found."); return; } Console.WriteLine($"Loaded ffmpeg v{FFmpegLoader.FFmpegVersion}"); Console.WriteLine("Generating Nightcore video..."); var options = new GeneratorOptions(new FileInfo(AudioFile), new FileInfo(WallpaperFile), new FileInfo(OutputFile), LyricsFile != null ? new FileInfo(LyricsFile) : null, FontFamily, GenerateIntro, FPS, 1.0f + SpeedIncrease / 100.0f, RendererVisible); var generator = new NightcoreGenerator(options); generator.AddEffect(new BeatPulseEffect(0.1, 4)); generator.AddEffect(new DetailsEffect()); generator.AddEffect(new FadeEffect(3)); generator.AddEffect(new ParticleEffect(20)); generator.ProgressHandler = progress => { Console.WriteLine($"Progress: {progress}%"); }; if (generator.Generate()) { Console.WriteLine("Generated video successfully"); } else { Console.WriteLine("Failed to generate video. Check error log for more info"); } }