Untitled

 avatar
unknown
csharp
a year ago
7.3 kB
5
Indexable
using FFmpeg.AutoGen;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Video Files (*.mp4, *.avi, *.mkv)|*.mp4;*.avi;*.mkv|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtVideoPath.Text = openFileDialog.FileName;
            }
        }

        private void txtStreamKey_TextChanged(object sender, EventArgs e)
        {
            string streamKey = txtStreamKey.Text;
        }

        private void txtFPS_TextChanged(object sender, EventArgs e)
        {
            string fps = txtFPS.Text;
        }

        private void txtBitrate_TextChanged(object sender, EventArgs e)
        {
            string bitrate = txtBitrate.Text;
        }

        unsafe private void btnStartStreaming_Click(object sender, EventArgs e)
        {
            try
            {
                string videoPath = txtVideoPath.Text;
                string streamKey = txtStreamKey.Text;
                int fps, bitrate;

                if (!int.TryParse(txtFPS.Text, out fps) || !int.TryParse(txtBitrate.Text, out bitrate))
                {
                    MessageBox.Show("FPS dan Bitrate harus berupa angka.");
                    return;
                }

                // Konfigurasi ffmpeg
                // Konfigurasi ffmpeg
                ffmpeg.avformat_network_init();

                // Definisikan format input
                AVFormatContext* inputFormatContext = null;
                if (ffmpeg.avformat_open_input(&inputFormatContext, videoPath, null, null) != 0)
                {
                    MessageBox.Show("Gagal membuka file video.");
                    return;
                }

                if (ffmpeg.avformat_find_stream_info(inputFormatContext, null) < 0)
                {
                    MessageBox.Show("Gagal mencari informasi stream.");
                    return;
                }

                // Definisikan format output
                AVFormatContext* outputFormatContext = null;
                string outputUrl = $"rtmp://a.rtmp.youtube.com/live2/{streamKey}";
                if (ffmpeg.avformat_alloc_output_context2(&outputFormatContext, null, "flv", outputUrl) < 0)
                {
                    MessageBox.Show("Gagal membuat konteks output.");
                    return;
                }

                // Tambahkan stream video
                AVStream* videoStream = ffmpeg.avformat_new_stream(outputFormatContext, null);
                if (videoStream == null)
                {
                    MessageBox.Show("Gagal menambahkan stream video.");
                    return;
                }

                // Konfigurasi stream video
                videoStream->codecpar->codec_id = AVCodecID.AV_CODEC_ID_H264;
                videoStream->codecpar->codec_type = AVMediaType.AVMEDIA_TYPE_VIDEO;
                videoStream->codecpar->width = inputFormatContext->streams[0]->codecpar->width;
                videoStream->codecpar->height = inputFormatContext->streams[0]->codecpar->height;
                videoStream->codecpar->format = (int)AVPixelFormat.AV_PIX_FMT_YUV420P;
                videoStream->time_base = inputFormatContext->streams[0]->time_base;

                // Buka file output
                if (ffmpeg.avio_open(&outputFormatContext->pb, outputUrl, ffmpeg.AVIO_FLAG_WRITE) < 0)
                {
                    MessageBox.Show("Gagal membuka file output.");
                    return;
                }

                // Tulis header output
                if (ffmpeg.avformat_write_header(outputFormatContext, null) < 0)
                {
                    MessageBox.Show("Gagal menulis header output.");
                    return;
                }

                while (true)
                {
                    // Read frame from input
                    int frameSize = ffmpeg.av_read_frame(inputFormatContext, packet);
                    if (frameSize < 0)
                    {
                        // End of stream reached
                        break;
                    }

                    // Convert frame to RGB
                    int srcStride = ffmpeg.avpicture_get_size(videoStream->codecpar->format, videoStream->codecpar->width, videoStream->codecpar->height);
                    byte[] srcData = new byte[srcStride * videoStream->codecpar->height];
                    ffmpeg.avpicture_fill((AVPicture)frame, srcData, (AVPixelFormat)videoStream->codecpar->format, videoStream->codecpar->width, videoStream->codecpar->height);

                    // Convert frame to YUV
                    int dstStride = ffmpeg.sws_get_stride(videoStream->codecpar->width, videoStream->codecpar->height, (AVPixelFormat)videoStream->codecpar->format);
                    byte[] dstData = new byte[dstStride * videoStream->codecpar->height];
                    SwsContext* swsContext = ffmpeg.sws_getContext(videoStream->codecpar->width, videoStream->codecpar->height, (AVPixelFormat)videoStream->codecpar->format,
                                                                    outputFormatContext->streams[videoStream->index]->codecpar->width, outputFormatContext->streams[videoStream->index]->codecpar->height,
                                                                    (AVPixelFormat)outputFormatContext->streams[videoStream->index]->codecpar->format,
                                                                    ffmpeg.SWS_BICUBIC,
                                                                    null,
                                                                    null,
                                                                    null);
                    ffmpeg.sws_scale(swsContext, new IntPtr[] { srcData }, new int[] { srcStride }, 0, videoStream->codecpar->height, new byte[] { 0 }, dstData, dstStride);

                    // Write frame to output
                    ffmpeg.av_init_packet(packet);
                    packet->stream_index = videoStream->index;
                    packet->data = dstData;
                    packet->size = dstStride * videoStream->codecpar->height;
                    FFmpeg.av_write_frame(outputFormatContext, packet);

                    // Clean up
                    ffmpeg.av_freep(srcData);
                    ffmpeg.av_freep(dstData);
                    ffmpeg.sws_free(swsContext);
                }

                // Close input and output files
                ffmpeg.av_close_input(inputFormatContext);
                ffmpeg.av_close_output(outputFormatContext);

                // Free packet and frame
                ffmpeg.av_packet_free(&packet);
                ffmpeg.av_frame_free(&frame);
            }
        }
    }
}
Editor is loading...
Leave a Comment