gpt4 book ai didi

c# - 从图像生成视频并添加音频时,视频无法播放

转载 作者:行者123 更新时间:2023-11-30 16:43:15 25 4
gpt4 key购买 nike

所以我在一个文件夹中有一堆图像,具有以下结构:

image-0.png
image-1.png
image-2.png

有时该文件夹可能只有 1 张图像,将另存为:

image-0.png

我生成视频和添加音频的代码

// create the file
using (WaveFileReader wf = new WaveFileReader(Audio))
{
// the files
OldVideo = "old.avi";
NewVideo = "video.avi";
Audio = "sound.wav";

// Get the time of the audio and divide by images
time = wf.TotalTime.TotalSeconds;
mimtime = time / imagescount;

ffmpegPath = "ffmpeg.exe";
ffmpegParams = " -r 1/" + mimtime + " -i " + ImagesFolder + "image-%d.png -t " + time + " -y -vf scale=1280:-2 " + OldVideo;
ffmaudioParams = " -i " + OldVideo + " -i " + Audio + " -c copy -shortest " + NewVideo;
}


using (Process ffmpeg = new Process())
{
//Generate video
ffmpegstartInfo = new ProcessStartInfo();
ffmpegstartInfo.FileName = ffmpegPath;
ffmpegstartInfo.Arguments = ffmpegParams;
ffmpegstartInfo.RedirectStandardOutput = true;
ffmpegstartInfo.RedirectStandardError = true;
ffmpegstartInfo.UseShellExecute = false;
ffmpegstartInfo.CreateNoWindow = true;

ffmpeg.StartInfo.FileName = ffmpegPath;
ffmpeg.StartInfo.Arguments = ffmpegParams;
ffmpeg.StartInfo = ffmpegstartInfo;
ffmpeg.Start();
ffmpeg.WaitForExit(30000);


/*ffmpeg.StartInfo.FileName = "cmd.exe";
ffmpeg.StartInfo.Arguments = "/k " + ffmpegPath + " " + ffmpegParams;
ffmpeg.Start();
ffmpeg.WaitForExit(30000);*/
}

using (Process ffmaudio = new Process())
{
//Add audio to video
ffmaudiostartInfo = new ProcessStartInfo();
ffmaudiostartInfo.FileName = ffmpegPath;
ffmaudiostartInfo.Arguments = ffmaudioParams;
ffmaudiostartInfo.RedirectStandardOutput = true;
ffmaudiostartInfo.RedirectStandardError = true;
ffmaudiostartInfo.UseShellExecute = false;
ffmaudiostartInfo.CreateNoWindow = true;

ffmaudio.StartInfo.FileName = ffmpegPath;
ffmaudio.StartInfo.Arguments = ffmaudioParams;
ffmaudio.StartInfo = ffmaudiostartInfo;
ffmaudio.Start();
ffmaudio.WaitForExit(30000);

/*ffmaudio.StartInfo.FileName = "cmd.exe";
ffmaudio.StartInfo.Arguments = "/k " + ffmpegPath + " " + ffmaudioParams;
ffmaudio.Start();
ffmaudio.WaitForExit(30000);*/
}

那么,我的代码是做什么的,它使用 wavfilereader 获取我的音频文件的总长度,然后根据我拥有的图像数量分割这段时间,这样我就可以生成视频图像平均分割。然后我将视频生成一个名为“old.avi”的文件,然后将其与我的 wav 文件一起使用以生成一个名为 video.avi 的新文件(我试图将生成的视频和音频放在一起)

我的问题是当我生成视频时它变得可以播放了。它声称(例如)1 分 50 秒长,但一旦我点击它,它就无法播放。它变得无法播放,如下所示

http://i.imgur.com/tsck2NX.gifv

我将 C# 与进程和 Winforms 一起使用,这会打开运行 ffmpeg 的 cmd 行,使用上面调用的 2 个命令和下面的命名变量:

ffmpegParams;
ffmaudioParams;

里面有我正在使用的所有命令

-r 1/ (audio length /divided by total images) so they're equal
-t (to limit to the total time which is the audio's total seconds)
... I am using more as you can see in my code above

如何解决我所描述的问题?

生成视频时的CMD行:

ffmpeg version N-86848-g03a9e6f Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 7.1.0 (GCC)
configuration: --enable-gpl --enable-version3 --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-zlib
libavutil 55. 68.100 / 55. 68.100
libavcodec 57.102.100 / 57.102.100
libavformat 57. 76.100 / 57. 76.100
libavdevice 57. 7.100 / 57. 7.100
libavfilter 6. 95.100 / 6. 95.100
libswscale 4. 7.101 / 4. 7.101
libswresample 2. 8.100 / 2. 8.100
libpostproc 54. 6.100 / 54. 6.100
Input #0, image2, from 'C:\Users\Laptop\Documents\program/images/image-%d.png':
Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
Stream #0:0: Video: png, rgba(pc), 800x800, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
Stream #0:0 -> #0:0 (png (native) -> mpeg4 (native))
Press [q] to stop, [?] for help
[mpeg4 @ 00000000024f44a0] bitrate tolerance 4000000 too small for bitrate 200000, overriding
Output #0, avi, to 'C:\Users\Laptop\Documents\program/old.avi':
Metadata:
ISFT : Lavf57.76.100
Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 1280x1280, q=2-31, 200 kb/s, 0.01 fps, 0.01 tbn, 0.01 tbc
Metadata:
encoder : Lavc57.102.100 mpeg4
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
frame= 1 fps=0.0 q=6.8 Lsize= 46kB time=00:01:37.57 bitrate= 3.9kbits/s speed=1.43e+003x
video:41kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 13.641467%

将音频添加到视频时

ffmpeg version N-86848-g03a9e6f Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 7.1.0 (GCC)
configuration: --enable-gpl --enable-version3 --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-zlib
libavutil 55. 68.100 / 55. 68.100
libavcodec 57.102.100 / 57.102.100
libavformat 57. 76.100 / 57. 76.100
libavdevice 57. 7.100 / 57. 7.100
libavfilter 6. 95.100 / 6. 95.100
libswscale 4. 7.101 / 4. 7.101
libswresample 2. 8.100 / 2. 8.100
libpostproc 54. 6.100 / 54. 6.100
Input #0, avi, from 'C:\Users\Laptop\Documents\program/old.avi':
Metadata:
encoder : Lavf57.76.100
Duration: 00:01:37.58, start: 0.000000, bitrate: 3 kb/s
Stream #0:0: Video: mpeg4 (Simple Profile) (FMP4 / 0x34504D46), yuv420p, 1280x1280 [SAR 1:1 DAR 1:1], 0.01 fps, 0.01 tbr, 0.01 tbn, 40 tbc
Guessed Channel Layout for Input Stream #1.0 : mono
Input #1, wav, from 'C:\Users\Laptop\Documents\program/audio.wav':
Duration: 00:01:37.57, bitrate: 352 kb/s
Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 22050 Hz, mono, s16, 352 kb/s
Output #0, avi, to 'C:\Users\Laptop\Documents\program/video.avi':
Metadata:
ISFT : Lavf57.76.100
Stream #0:0: Video: mpeg4 (Simple Profile) (FMP4 / 0x34504D46), yuv420p, 1280x1280 [SAR 1:1 DAR 1:1], q=2-31, 0.01 fps, 0.01 tbr, 0.01 tbn, 0.01 tbc
Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 22050 Hz, mono, s16, 352 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #1:0 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 1 fps=0.0 q=-1.0 Lsize= 55kB time=00:01:37.57 bitrate= 4.6kbits/s speed=1.95e+005x
video:41kB audio:4kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 21.946869%

最佳答案

一些事情:

1) 检查你的计算。你的输出显示,

frame=    1 fps=0.0 q=-1.0 Lsize=      55kB time=00:01:37.57

在 0.01 fps 下,图像将在 100 秒后切换。由于您的输出是 97 秒,因此只有 1 帧,因此输出中只有 1 张图像。尽管您的输入也仅显示单个图像输入:

Input #0, image2, from 'C:\Users\Laptop\Documents\program/images/image-%d.png':
Duration: 00:00:00.04, start: 0.000000, bitrate: N/A

(使用-framerate代替-r)

2) VLC 和其他一些播放器不喜欢低帧率。在缩放过滤器后添加 -r 6。这应该让您在 VLC 中显示持续时间。

关于c# - 从图像生成视频并添加音频时,视频无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45363921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com