ffmpeg下载流媒体视频

参考资料

Download any Azure Media Service video or live stream with FFmpeg. - Anduin Xue (aiursoft.cn)

下载地址:

https://www.gyan.dev/ffmpeg/builds/packages/ffmpeg-5.1.2-full_build.7z

示例代码:(包含下载以及上传腾讯COS)

using COSXML.Transfer;
using System;
using System.Data;
using System.Data.OleDb;
using System.Net;
using System.Diagnostics;
using System.IO;
using COSXML.Auth;
using COSXML;
using System.Threading.Tasks;
using System.Web;
using System.Runtime.InteropServices;

namespace MigrateSprintVideo {
    class Program {
        // 关闭64位(文件系统)的操作转向
        //[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        //public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
         开启64位(文件系统)的操作转向
        //[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        //public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

        static readonly string ffmpegPath = @"D:\tools\ffmpeg\bin\ffmpeg.exe";
        static readonly string ffmpegArgs = " -protocol_whitelist file,http,https,tcp,tls,crypto -i {0}  -c copy {1}";
        static readonly string videoPath = @"E:\sv\math\{0}\";
        //static readonly string videoPath = @"E:\sv\english\{0}\";
        static readonly string cosUrl = "video/math/sprint_micro/{0}/{1}.mp4";
        static readonly string bucket = "***-***-*******"; //存储桶,格式:BucketName-APPID
        static readonly string region = "ap-beijing";
        static async Task Main(string[] args) {
            //IntPtr oldWOW64State = new IntPtr();
            //Wow64DisableWow64FsRedirection(ref oldWOW64State);   // 关闭64位(文件系统)的操作转向

            CosXmlConfig config = new CosXmlConfig.Builder()
                .IsHttps(true)
                .SetRegion(region)
                .SetDebugLog(true)
                .Build();

            string xlsFileName = @"E:\workspace\2023\MigrateSprintVideo\video.xlsx";
            string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + xlsFileName + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";//连接数据源所用的字符串
            OleDbConnection con = new OleDbConnection(conString);//创建连接
            con.Open();//打开连接
            string sql = "select * from [Sheet1$]";//查询语句

            OleDbDataAdapter adp = new OleDbDataAdapter(sql, con);//执行查询语句
            DataSet ds = new DataSet();//可以理解未内存中的一个数据库,里边会有多张表
            adp.Fill(ds);//将查询结果填充到DataSet中
            con.Close();//关闭连接
            con.Dispose();//释放资源
            DataTableCollection datatable_collection = ds.Tables;//得到一个数据表的集合
            DataTable dt = datatable_collection[0];//第一张表就是我们的数据
            try {
                CosXml cosXml = new CosXmlServer(config, GetCredentialProvider());

                Console.WriteLine($"数据共{dt.Rows.Count}条");

                foreach (DataRow dr in dt.Rows) {
                    string questionId = dr[2].ToString();
                    string id = dr[0].ToString();
                    string url = HttpUtility.UrlPathEncode(dr[1].ToString()) + "(format=mpd-time-csf)";
                    string path = string.Format(videoPath, questionId);
                    if (!Directory.Exists(path)) {
                        Directory.CreateDirectory(path);
                    }
                    path += id + ".mp4";
                    Download(url, path);
                    string cosPath = string.Format(cosUrl, questionId, id);
                    await Upload(path, cosPath, cosXml);
                }
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }
            //Wow64RevertWow64FsRedirection(oldWOW64State);        // 开启64位(文件系统)的操作转向
            //Console.ReadKey();
        }



        /**
         * 下载视频
         */
        static void Download(string url, string local) {
            string args = string.Format(ffmpegArgs, url, local);
            Console.WriteLine(ffmpegPath + args);
            Process pro = Process.Start(ffmpegPath, args);
            //让 Process 组件等候相关的进程进入闲置状态。 
            //pro.WaitForInputIdle();
            //设定要等待相关的进程结束的时间,并且阻止目前的线程执行,直到等候时间耗尽或者进程已经结束为止。 
            pro.WaitForExit();
        }

        /**
         * 上传视频
         */
        static async
        /**
         * 上传视频
         */
        Task Upload(string srcPath, string cosPath, CosXml cosXml) {
            // 初始化 TransferConfig
            TransferConfig transferConfig = new TransferConfig();
            // 初始化 TransferManager
            TransferManager transferManager = new TransferManager(cosXml, transferConfig);


            // 上传对象
            COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);
            uploadTask.SetSrcPath(srcPath);

            uploadTask.progressCallback = delegate (long completed, long total) {
                Console.WriteLine(string.Format("{0} progress = {1:##.##}%", cosPath, completed * 100.0 / total));
            };


            try {
                COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await transferManager.UploadAsync(uploadTask);
                Console.WriteLine(result.GetResultInfo());
                string eTag = result.eTag;
            } catch (Exception e) {
                Console.WriteLine("CosException: " + e);
            }

        }

        static QCloudCredentialProvider GetCredentialProvider() {
            string secretId = "**************"; //用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
            string secretKey = "**************"; //用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
            long durationSecond = 600;  //每次请求签名有效时长,单位为秒
            QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(
              secretId, secretKey, durationSecond);
            return cosCredentialProvider;
        }
    }
}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值