使用ffmpeg实现视频连接

最近项目需要实现一个视频连接功能,即将一些小的视频片段连接成一个视频,项目开发用的是C#语言。

最终决定使用ffmpeg。

FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件)。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多codec都是从头开发的。

更多关于ffmpeg介绍看http://baike.baidu.com/view/856526.htm?fr=aladdin

以前也没有接触过ffmpeg,首先去官网现在windows平台的ffmpeg。

下载地址:http://ffmpeg.zeranoe.com/builds/

其中有三个版本,适用于不同情况。下载完成后解压其目录结构如下:

运行ff-prompt批处理命令,在其中输入相应的指令即可完成操作。

连接视频的命令如下:

ffmpeg -i "1.mp4" -qscale 6 "inputfile_01.mpg"
ffmpeg -i "2.mp4" -qscale 6 "inputfile_02.mpg"
copy /b "inputfile_01.mpg"+"inputfile_02.mpg" "inputfile_all.mpg"
ffmpeg -i inputfile_all.mpg -qscale 6 outputfile.mp4
del "inputfile_01.mpg"
del "inputfile_02.mpg"
del "inputfile_all.mpg"

上述命令只能连接两个视频的连接,因为在程序中无法事先确定视频数量,因此采用list集合来存储需要连接视频绝对路径,然后使用循环方式连接视频,具体代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Collections;

namespace MP4
{
    class MergeVedio
    {
        /// <summary>
        /// 调用指定的命令
        /// </summary>
        /// <param name="batPath"></param>
        /// <param name="cmd"></param>
        public void RunBat(string batPath, string cmd)
        {
            Process pro = new Process();
            FileInfo file = new FileInfo(batPath);
            pro.StartInfo.WorkingDirectory = file.Directory.FullName;
            pro.StartInfo.FileName = batPath;
            pro.StartInfo.Arguments = cmd;
            pro.Start();
            pro.WaitForExit();
        }

        /// <summary>
        /// 调用指定的命令
        /// </summary>
        /// <param name="batPath"></param>
        /// <param name="cmd"></param>
        public void RunWindowsCMD( string cmd)
        {
            Process pro = new Process();
            FileInfo file = new FileInfo("cmd.exe");
            pro.StartInfo.WorkingDirectory = file.Directory.FullName;
            pro.StartInfo.FileName = "cmd.exe";
            pro.StartInfo.CreateNoWindow = false;
            pro.StartInfo.UseShellExecute = false;
            pro.StartInfo.RedirectStandardInput = true;
            pro.StartInfo.RedirectStandardOutput = true;
            pro.Start();
            pro.StandardInput.WriteLine(cmd);
            pro.WaitForExit();
        }
        public void saveVedio(List<string> list, string name, string binpath)
        {
            string first = list[0].Replace("mp4", "mpg");
            string conert = " -i " + list[0] + " -qscale 6 " + first;
            string del;
            RunBat(binpath, conert);
            for (int i = 1; i < list.Count; i++)
            {
                string temp = list[i].Replace("mp4", "mpg");
                string conert1 = " -i " + list[i] + " -qscale 6 " + temp;
                RunBat(binpath, conert1);
                string merge = "copy /b " + first + "+" + temp +" " + first+" \r\n exit";
                RunWindowsCMD(merge);
                del = "del " + temp+" \r\n exit";
                RunWindowsCMD(del);
            }
            string final = "-i " + first + " -qscale 6 " + name;
            RunBat(binpath, final);
            del = "del " + first + " \r\nexit";
            RunWindowsCMD(del);
        }
    }
}

通过调用该类的saveVedio方法即可将参数中传入的视频地址中的视频连接,并保存在指定为name的地址中。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要重新连接视频流,可以使用FFmpeg的`avformat_open_input`函数,并通过设置`AVFormatContext`的一些参数来实现。以下是一些常用的参数设置: ```cpp #include <libavformat/avformat.h> // 创建AVFormatContext对象 AVFormatContext* formatContext = avformat_alloc_context(); // 设置重连参数 formatContext->interrupt_callback.callback = [](void*){ return 0; }; // 设置中断回调函数,返回0表示继续执行 formatContext->interrupt_callback.opaque = nullptr; // 回调函数的私有数据,可以设置为nullptr // 打开视频流 int ret = avformat_open_input(&formatContext, m_url, nullptr, nullptr); if (ret < 0) { // 打开失败处理 avformat_free_context(formatContext); return; } // 启用自动重连 formatContext->flags |= AVFMT_FLAG_AUTO_BSF; // 启用自动比特流过滤器(Bitstream Filter) // ... // 使用formatContext进行后续操作,如读取视频帧等 ``` 在上述代码中,我们首先创建一个`AVFormatContext`对象,并使用`avformat_alloc_context`函数进行分配。 然后,我们设置了重连参数。通过设置`formatContext->interrupt_callback.callback`为一个返回0的回调函数,表示在需要中断时返回,从而实现重新连接的目的。同时,我们将`formatContext->interrupt_callback.opaque`设置为`nullptr`,即回调函数的私有数据为空。 接下来,我们使用`avformat_open_input`函数打开视频流。如果打开失败,可以根据需要进行相应的错误处理。 最后,我们启用了自动重连功能,通过设置`formatContext->flags |= AVFMT_FLAG_AUTO_BSF`来启用自动比特流过滤器(Bitstream Filter)。 请注意,以上代码只是一个示例,并不包含完整的错误处理和视频流读取逻辑。在实际应用中,你需要根据具体情况进行适当的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值