コード例 #1
0
ファイル: FlvToolMain.cs プロジェクト: fangxu/FlvTool
        private bool AnalysVideo()
        {
            if (!File.Exists(textFlv.Text))
            {
                MessageBox.Show("flv file does not exist!", "no such file.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            EnableUI(false);
            new Thread(() =>
            {
                FileStream stream = null;
                stream = new FileStream(textFlv.Text, FileMode.Open, FileAccess.Read);
                this.Invoke(new MethodInvoker(() =>
                {
                    progressBar.Maximum = (int)stream.Length;
                    progressBar.Value = 0;
                    Application.DoEvents();

                }));

                parser = new FlvParser(stream, (tag) =>
                {

                    this.BeginInvoke(new MethodInvoker(() =>
                    {
                        progressBar.Value = (int)tag.Offset;
                        ChangeTaskBar();
                        if (progressBar.Value == progressBar.Maximum)
                        {
                            progressBar.Value = 0;
                        }
                    }));

                    return true;
                });
                stream.Close();
                stream.Dispose();

                this.BeginInvoke(new MethodInvoker(() =>
                {
                    textStat.Text = "File:" + textFlv.Text + "\r\n"
                        + "Length:" + (parser.Length / 1024.0 / 1024.0).ToString("0.0") + " Mb" + "\r\n"
                        + "Duration:" + parser.Duration / 1000 + " s" + "\r\n"
                        + "Rate:" + parser.Rate + "Kbps";
                    EnableUI(true);
                }));
            }).Start();
            return true;
        }
コード例 #2
0
ファイル: FlvWrite.cs プロジェクト: fangxu/FlvTool
 public FlvWrite(FlvParser parser)
 {
     this.parser = parser;
 }
コード例 #3
0
ファイル: FormAdvanced.cs プロジェクト: fangxu/FlvTool
        private void buttonBlack_Click(object sender, EventArgs e)
        {
            EnableUI(false);
            progressBar.Minimum = 0;
            progressBar.Value = 0;
            progressBar.Maximum = listViewFlv.Items.Count * 2;
            int numbers = listViewFlv.Items.Count;
            string[] files = new string[numbers];

            for (int i = 0; i < numbers; i++)
            {
                files[i] = listViewFlv.Items[i].Text;
            }
            // string s = listViewFlv.Items.

            new Thread(() =>
            {
                for (int i = 0; i < numbers; i++)
                {
                    double rate = (double)upDownRate.Value;
                    //string file = null;
                    //file = listViewFlv.Items[i].Text;

                    this.BeginInvoke(new MethodInvoker(() =>
                    {

                         this.Text = title + "--正在处理第" + (i + 1) + "个视频...";
                    }));

                    FileStream stream = null;
                    stream = new FileStream(files[i], FileMode.Open, FileAccess.Read);
                    FlvParser parser = new FlvParser(stream, null);
                    this.BeginInvoke(new MethodInvoker(() =>
                    {
                        progressBar.Value++;
                        ChangeTaskBar();
                    }));
                    stream.Close();
                    stream.Dispose();

                    if (parser.Rate > rate)
                    {
                        long filesize = parser.Length + 16;
                        double duration;
                        duration = filesize / 125.0 / rate; // * 8 / 1000 / rate
                        string offset = ((duration * 1000 - parser.Duration) / 1000).ToString("0.000");
                        Stream src = new FileStream(files[i], FileMode.Open);
                        string path = files[i].Substring(0, files[i].LastIndexOf(".")) + ".black.flv";

                        Stream dest = new FileStream(path, FileMode.Create);
                        FlvWrite fw = new FlvWrite(parser);
                        fw.WriteHead(dest, filesize, duration, -1, -1, -1, 1.0, 0,
                            0, parser.Tags.Count - 1, false);
                        for (int n = 1; n < parser.Tags.Count; n++)
                        {
                            src.Seek(parser.Tags[n].Offset - 11, SeekOrigin.Begin);
                            FlvParser.FlvTag tag = parser.Tags[n];
                            byte[] bs = new byte[tag.DataSize + 11];
                            // 数据
                            src.Read(bs, 0, bs.Length);
                            dest.Write(bs, 0, bs.Length);
                            // prev tag size
                            src.Read(bs, 0, 4);
                            dest.Write(bs, 0, 4);
                        }
                        src.Close();
                        src.Dispose();
                        byte[] buffer = new byte[]{
                            0x09, 0, 0, 0x01, // 视频帧 1 字节
                            0, 0, 0, 0,       // 04h, timestamp & ex
                            0, 0, 0,          // stream id
                            0x17,             // InnerFrame, H.264
                            0, 0, 0, 0x0c     // 此帧长度 12 字节
                            };
                        uint dur = (uint)(duration * 1000);
                        fw.PutTime(buffer, 0x04, dur);
                        dest.Write(buffer, 0, buffer.Length);

                        dest.Flush();
                        dest.Close();
                        dest.Dispose();
                    }
                    this.BeginInvoke(new MethodInvoker(() =>
                     {
                         progressBar.Value++;
                         ChangeTaskBar();
                         if (progressBar.Value == progressBar.Maximum)
                         {
                             progressBar.Value = 0;
                             this.Text =title+ "--处理完成!";
                             EnableUI(true);
                         }
                     }));
                }

            }).Start();
        }