コード例 #1
0
        //int oldtime = 0;
        //public void startCaption()
        //{
        //    update = new Update(updateUI);
        //    var API_KEY = "uz804M95AKkq5OffLCB9GD3q";
        //    var SECRET_KEY = "IZxCofo7vFRYiPKhXqdmh2Da51MrhDKA";
        //    client = new Baidu.Aip.Speech.Asr(API_KEY, SECRET_KEY);
        //    Time timer = new Time(getTime);
        //    Thread.Sleep(500);
        //    while (true)
        //    {
        //        //Console.WriteLine(duration);
        //        //int position =player.GetPosition();
        //        if (position / 1000 % 7 == 2)
        //        {
        //            int newtime = (position / 1000 / 7) + 1;
        //            //Console.WriteLine(newtime);
        //            if (newtime != oldtime)
        //            {
        //                //Console.WriteLine(oldtime);
        //                var data = FileUtil.getFileVideo(videoPath, newtime + ".pcm");
        //                client.Timeout = 5000; // 若语音较长,建议设置更大的超时时间. ms
        //                var result = client.Recognize(data, "pcm", 16000, dic);
        //                Console.WriteLine(result);
        //                if (result["result"] != null)
        //                {
        //                    String value = result["result"][0].ToString();
        //                    String response = PostRequest.sendGet(value);
        //                    Thread.Sleep(200);
        //                    JObject jo = (JObject)JsonConvert.DeserializeObject(response);
        //                    String tranvalue = jo["trans_result"][0]["dst"].ToString();
        //                    Console.WriteLine(tranvalue);
        //                    if (tranvalue.Length > 16)
        //                    {
        //                        String data1 = tranvalue.Substring(0, 16) + "\r\n" + tranvalue.Substring(16);
        //                        //player.BeginInvoke(update, new object[] { newtime + " \r\n" + "00:00:" + newtime * 7 + ",500" + " --> 00:00:" + (newtime + 1) * 7 + ",000" + " \r\n" + data1 });
        //                        //player.SetConfig(511, newtime + " \r\n" + "00:00:" + newtime * 7 + ",500" + " --> 00:00:" + (newtime + 1) * 7 + ",000" + " \r\n" + data1);
        //                        //player.SetConfig(508, "微软雅黑;20;16777215;1");   //设置字幕字体
        //                        //player.SetConfig(507, "1;50;99");    //设置字幕位置
        //                        //Thread.Sleep(200);
        //                        FileUtil.writeSrtFile(videoPath, "\r\n" + (newtime + 1) + "\r\n" + "00:00:" + newtime * 7 + ",500" + " --> 00:00:" + (newtime + 1) * 7 + ",000" + "\r\n" + data1);
        //                    }
        //                    else
        //                    {
        //                        //Thread.Sleep(100);
        //                        //player.BeginInvoke(update, new object[] { newtime + " \r\n" + "00:00:" + newtime * 7 + ",500" + " --> 00:00:" + (newtime + 1) * 7 + ",000" + " \r\n" + tranvalue });
        //                        //player.SetConfig(508, "微软雅黑;20;16777215;1");   //设置字幕字体
        //                        //player.SetConfig(507, "1;50;99");    //设置字幕位置
        //                        //Thread.Sleep(200);
        //                        FileUtil.writeSrtFile(videoPath, "\r\n" + (newtime + 1) + "\r\n" + "00:00:" + newtime * 7 + ",500" + " --> 00:00:" + (newtime + 1) * 7 + ",000" + "\r\n" + tranvalue);
        //                    }
        //                }
        //                oldtime = newtime;
        //            }

        //            Thread.Sleep(50);
        //        }
        //    }
        //}

        public void startVideo()
        {
            //int duration = player.GetDuration();
            for (int i = 1; i <= duration + 1; i++)
            {
                if (!FileUtil.setVideoFile(videoPath, i + ".pcm"))
                {
                    CommandUtil.StartCmdProcess(FileUtil.ffmepg + " -ss " + i * 7 + " -t " + 7 + " -i " + videoPath + " -f s16le -acodec pcm_s16le  -ar 16000 -ac 1  " + FileUtil.videoPath + "/" + Path.GetFileName(videoPath).GetHashCode() + "/" + i + ".pcm");
                }
            }
        }
コード例 #2
0
        public void work()
        {
            if (!FileUtil.setVideoFile(videoPath, 0 + ".pcm"))
            {
                CommandUtil.StartCmdProcess(FileUtil.ffmepg + " -ss " + 0 + " -t " + 7 + " -i " + videoPath + " -f s16le -acodec pcm_s16le  -ar 16000 -ac 1  " + FileUtil.videoPath + "/" + Path.GetFileName(videoPath).GetHashCode() + "/" + 0 + ".pcm");
                Thread.Sleep(1000);
            }
            var API_KEY    = "uz804M95AKkq5OffLCB9GD3q";
            var SECRET_KEY = "IZxCofo7vFRYiPKhXqdmh2Da51MrhDKA";

            client = new Baidu.Aip.Speech.Asr(API_KEY, SECRET_KEY);
            var data = FileUtil.getFileVideo(videoPath, 0 + ".pcm");

            client.Timeout = 120000; // 若语音较长,建议设置更大的超时时间. ms
            var result = client.Recognize(data, "pcm", 16000, dic);

            if (result["err_no"].ToString().Equals("3301"))
            {
                Close();
            }
            Console.WriteLine(result);
            if (result["result"] != null)
            {
                String value    = result["result"][0].ToString();
                String response = PostRequest.sendGet(value);
                Thread.Sleep(300);
                JObject jo = (JObject)JsonConvert.DeserializeObject(response);
                Console.WriteLine(response);

                String tranvalue = jo["trans_result"][0]["dst"].ToString();
                if (tranvalue.Length > 7)
                {
                    String data1 = tranvalue.Substring(0, 7) + "\r\n" + tranvalue.Substring(7);
                    label.BeginInvoke(callBack, new object[] { data1 });
                    //player.SetConfig(511, 0 + 0 + "\r\n "+"\r\n " + "00:00:" + 0 * 7 + ",500" + " --> 00:00:" + (0 + 1) * 7 + ",000" + "\r\n " + data1);
                    //player.SetConfig(508, "微软雅黑;20;16777215;1");   //设置字幕字体
                    //player.SetConfig(507, "1;50;90");    //设置字幕位置
                    FileUtil.writeSrtFile(videoPath, data1);
                    Close();
                }
                else
                {
                    label.BeginInvoke(callBack, new object[] { tranvalue });
                    //player.SetConfig(511, 0 + " \r\n " + "00:00:" + 0 * 7 + ",500" + " --> 00:00:" + (0 + 1) * 7 + ",000" + " \r\n " + tranvalue);
                    //player.SetConfig(508, "微软雅黑;20;16777215;1");   //设置字幕字体
                    //player.SetConfig(507, "1;50;50");    //设置字幕位置
                    Console.WriteLine(tranvalue);
                    FileUtil.writeSrtFile(videoPath, 1 + "\r\n" + "00:00:" + 0 * 7 + ",500" + " --> 00:00:" + (0 + 1) * 7 + ",000" + "\r\n " + tranvalue);
                    Close();
                }
            }
        }