Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

C#实现视频下载并加文字水印 linux平台可用

  1. User

    C#实现视频下载并加文字水印 linux平台可用

  2. Assistant

    在Linux平台上,你可以使用FFmpeg库来下载视频并添加文字水印。首先,你需要安装FFmpeg库和FFprobe工具。

    以下是一个C#方法的示例,该方法使用FFmpeg来下载视频(使用FFprobe来获取视频信息),并在视频的左上角添加文字水印:

    using System.Diagnostics;
    
    public class VideoDownloader
    {
        // 下载视频并添加文字水印
        public void DownloadVideoWithWatermark(string videoURL, string outputFilePath, string watermarkText)
        {
            // 使用FFprobe来获取视频信息
            ProcessStartInfo probeStartInfo = new ProcessStartInfo("ffprobe");
            probeStartInfo.Arguments = $"-v error -select_streams v:0 -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {videoURL}";
            probeStartInfo.RedirectStandardOutput = true;
            Process probeProcess = Process.Start(probeStartInfo);
            string durationOutput = probeProcess.StandardOutput.ReadToEnd();
            probeProcess.WaitForExit();
    
            double duration;
            if (!double.TryParse(durationOutput, out duration))
            {
                throw new Exception("Failed to get video duration");
            }
    
            // 使用FFmpeg下载视频并添加文字水印
            ProcessStartInfo ffmpegStartInfo = new ProcessStartInfo("ffmpeg");
            ffmpegStartInfo.Arguments = $"-i {videoURL} -vf \"drawtext=text='{watermarkText}':fontsize=20:fontcolor=white:x=10:y=10\" -c:a copy {outputFilePath}";
            Process ffmpegProcess = Process.Start(ffmpegStartInfo);
            ffmpegProcess.WaitForExit();
        }
    }
    

    你可以将以上代码添加到你的C#项目中,并在需要下载并添加水印的地方调用DownloadVideoWithWatermark方法。确保在Linux系统中安装了FFmpeg库和FFprobe工具。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.