使用ffmpeg在视频文件中重复音频Backgorund

时间:2016-09-28 17:23:36

标签: c# audio video ffmpeg

我的程序允许用户插入一堆图像和一个音频文件,然后可以将它们生成为视频文件。我的程序还允许用户为每个图像提供持续时间。

我能够将音频背景添加到视频输出中,但是当视频长度比音频长度更长时,音频不会循环播放。 此外,当音频长度超过视频长度时,视频输出将显示为空白,直到音频结束。

制作视频

public bool CreateVideo(string name, List<string> imgs)
    {
        Bitmap bitmap = (Bitmap)null;
        try
        {
            if (!Directory.Exists(this.outputFolder + "\\temp"))
                Directory.CreateDirectory(this.outputFolder + "\\temp");
            int num = this.rand.Next(this.delayMin, this.delayMax + 1);
            List<string> stringList = new List<string>((IEnumerable<string>)imgs);
            if (this.randomizeImages)
                this.Shuffle((IList)stringList);
            if (!string.IsNullOrEmpty(this.waterImage) && File.Exists(this.waterImage))
                bitmap = (Bitmap)Image.FromFile(this.waterImage, true);
            if (!string.IsNullOrEmpty(this.introImage))
            {
                int index = stringList.IndexOf(this.introImage);
                if (index > 0)
                {
                    string str = stringList[index];
                    stringList[index] = stringList[0];
                    stringList[0] = str;
                }
            }
            for (int position = 0; position < stringList.Count; ++position)
                this.ApplyWatermarkImage(stringList[position], position, (Image)bitmap);
            if (bitmap != null)
                bitmap.Dispose();
            string str1 = "";
            if (!string.IsNullOrEmpty(this.audioFile))
            {
                str1 = " -i \"" + this.audioFile + "\" -ar 22050 -ab 64k ";
                if (this.GetAudioLength(this.audioFile) > num * stringList.Count)
                    str1 += "-shortest ";
            }
            string str2 = this.outputFolder + "\\" + name + ".avi";
            ProcessStartInfo processStartInfo = new ProcessStartInfo(Environment.CurrentDirectory + "\\sm\\ffmpeg.exe", str1 + "-fflags +genpts -r 1/" + (object)num + " -b 32K -f image2  -i \"" + this.outputFolder + "\\temp\\%3d.jpg\" -vcodec mjpeg -s " + this.width.ToString() + "x" + this.height.ToString() + "  -y -r 30 \"" + str2 + "\"");
            Process process = new Process();
            processStartInfo.WorkingDirectory = Directory.GetCurrentDirectory();
            processStartInfo.CreateNoWindow = true;
            processStartInfo.UseShellExecute = false;
            processStartInfo.RedirectStandardError = true;
            process.StartInfo = processStartInfo;
            process.Start();
            StreamReader standardError = process.StandardError;
            Trace.WriteLine(standardError.ReadToEnd());
            standardError.Close();
            process.WaitForExit();
            return process.ExitCode != 1 && process.ExitCode != -1;
        }
        catch (Exception ex)
        {
            Trace.WriteLine("CreateVideo: " + ex.Message);
            return false;
        }
        finally
        {
            if (bitmap != null)
                bitmap.Dispose();
            try
            {
                if (Directory.Exists(this.outputFolder + "\\temp"))
                    Directory.Delete(this.outputFolder + "\\temp", true);
            }
            catch
            {
            }
        }
    }

获取音频长度

public int GetAudioLength(string path)
    {
        string output = this.GetOutput(path);
        int num = 0;
        if (output == "" || output.Contains("Invalid data found when processing input") || output.Contains("could not find codec parameters"))
            return num;
        string str;
        try
        {
            str = Regex.Match(output, "(?:Duration\\:)(.*?)(?:,)", RegexOptions.Singleline).Groups[1].Value.Trim();
        }
        catch (Exception ex)
        {
            str = "";
        }
        if (str != "")
        {
            try
            {
                num = int.Parse(str.Split(':')[0]) * 3600;
                num += int.Parse(str.Split(':')[1]) * 60;
                num += (int)double.Parse(str.Split(':')[2]);
            }
            catch (Exception ex)
            {
            }
        }
        return num;
    }

获取输出

private string GetOutput(string video_file)
    {
        try
        {
            return Process.Start(new ProcessStartInfo()
            {
                FileName = Environment.CurrentDirectory + "\\sm\\ffmpeg.exe",
                Arguments = "-i \"" + video_file + "\"",
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden
            }).StandardError.ReadToEnd();
        }
        catch (Exception ex)
        {
            return "";
        }
    }

我环顾四周但找不到解决方案。任何帮助将不胜感激。

0 个答案:

没有答案