C# 使用豆包 AI 模型实现首尾帧模式的视频生成

C# 使用豆包 AI 模型实现首尾帧模式的视频生成

 体验

欲诚其意者,先致其知,致知在格物。人生太多体验,有悲有喜,有好有坏。没有实践就没有发言权,没有亲自尝试就不要轻易否定,适合你的才是最好的。最近在火山引擎火山方舟平台模型广场中看到豆包推出最强视频生成模型 Doubao-Seedance-1.0-pro,于是也想体验一下其魅力如何。模型提供多种生成方式,被其中一项 “首尾帧” 模式所吸引,即提供首图和尾图两张照片,并结合 AI 对话描述生成结果视频。本文则主要讲述如何使用C#调用平台API实现视频生成功能。

调用 API 前需要注册火山引擎帐号并获得 API 开发密钥。

火山引擎注册地址如下:https://console.volcengine.com/auth/login

选择火山方舟 -> API Key 管理 ->  创建 API Key 即可,请注意编辑权限以保证能够调用对应功能的 API 。

构思

如同人与人之间有效的沟通,为 AI 模型提供符合情境的素材,精准、逻辑清晰的对话描述,才能生成符合预期的结果,不过为了测试其 “思考、创意” 能力 ,我提供了两张风马牛不相及的美女图片,简要描述了一下对话,对话内容:“结合图片,请生成类似周杰伦《红颜如霜》的MV,要求生成音乐”。生图照片素材内容如下:

素材图片首帧为有些 “现代风” 的人物座在汽车后排的场景,尾帧是有些 “复古风” 的人物在女生宿舍的场景,看看 AI 模型根据提供的素材会生成什么样的结果视频,期待中还是夹杂着一丝好奇的。

创建视频生成任务

模型 “思考”和生成视频需要一段时间,因此属于异步操作,首先要做的就是向模型申请创建视频生成任务,Doubao_CreateVideo 方法提供了创建视频任务的能力,基本说明如下表:

序号参数名类型说明
1saystring对话描述内容。如“请为我生成一段视频”
2ApiKeystring在火山引擎平台申请的API访问令牌
3first_ImageUrlOrBase64string首帧图片的可访问URL或图片的Base64编码
4last_ImageUrlOrBase64string尾帧图片的可访问URL或图片的Base64编码
5rsstring

指定生成视频的分辨率,默认值为720p

枚举值包括:480p,720p,1080p

6rtstring

指定生成视频的宽高比,默认值为16:9

枚举值包括:16:9,4:3,1:1,3:4,9:16,21:9

7durint

指定生成视频的时长,默认值为 5 秒

最长目前可选择为12秒

8fpsint指定生成视频的帧率,默认值为 24 (fps),
9wmbool指定生成的视频是否包含水印(AI生成字样)
10seedint

种子整数,用于控制生成内容的随机性。默认值为11

11cf=falsebool

是否固定摄像头,默认值为 false。

枚举值:true:固定摄像头。平台会在用户提示词中追加固定摄像头,实际效果不保证。false:不固定摄像头。

12modelstring

使用的模型ID,默认值为官方强烈推荐的 doubao-seedance-1-0-pro-250528。详细 ID 列表请参考官方API文档进行查询。

方法示例代码如下:

string; string; public void Doubao_CreateVideo(string say,string ApiKey,string first_ImageUrlOrBase64,string last_ImageUrlOrBase64 ,string rs="720p",string rt="16:9",int dur=5,int fps=24,bool wm=true,int seed=11,bool cf=false , string model= "doubao-seedance-1-0-pro-250528") { string para = string.Format(" --rs {0} --rt {1} --dur {2} --fps {3} --wm {4} --seed {5} --cf {6}",rs,rt,dur,fps,wm,seed,cf); string ApiUrl = "https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks"; WebService ws = new WebService(); string[] headers = new string[3]; headers[0] = "Content-Type:application/json"; headers[1] = "Accept:application/json"; headers[2] = "Authorization:Bearer " + ApiKey + ""; StringWriter sw = new StringWriter(); using (Newtonsoft.Json.JsonWriter writer = new Newtonsoft.Json.JsonTextWriter(sw)) { writer.Formatting = Newtonsoft.Json.Formatting.Indented; writer.WriteStartObject(); writer.WritePropertyName("model"); writer.WriteValue(model); writer.WritePropertyName("content"); writer.WriteStartArray(); writer.WriteStartObject(); writer.WritePropertyName("type"); writer.WriteValue("text"); writer.WritePropertyName("text"); writer.WriteValue(say+para); writer.WriteEndObject(); writer.WriteStartObject(); writer.WritePropertyName("type"); writer.WriteValue("image_url"); writer.WritePropertyName("image_url"); writer.WriteStartObject(); writer.WritePropertyName("url"); writer.WriteValue(first_ImageUrlOrBase64); writer.WriteEndObject(); writer.WritePropertyName("role"); writer.WriteValue("first_frame"); writer.WriteEndObject(); writer.WriteStartObject(); writer.WritePropertyName("type"); writer.WriteValue("image_url"); writer.WritePropertyName("image_url"); writer.WriteStartObject(); writer.WritePropertyName("url"); writer.WriteValue(last_ImageUrlOrBase64); writer.WriteEndObject(); writer.WritePropertyName("role"); writer.WriteValue("last_frame"); writer.WriteEndObject(); writer.WriteEndArray(); writer.WriteEndObject(); writer.Flush(); } sw.Close(); string postData = sw.GetStringBuilder().ToString();;; string rs2 = ws.GetResponseResult(ApiUrl, Encoding.UTF8, "POST", postData, headers); ErrorMessage = ws.ErrorMessage; ResultJson = rs2; } 

 其中 WebService 类示例代码如下:

 public sealed class WebService { #region Internal Members public string; #endregion /// <summary> /// 构造函数,提供初始化数据的功能,打开Ftp站点 /// </summary> public string GetResponseResult(string url, System.Text.Encoding encoding, string method, string postData) { return GetResponseResult(url,encoding,method,postData,null); } private static bool validSecurity(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } public string GetResponseResult(string url, System.Text.Encoding encoding, string method, string postData,string[] headers,string ContentType= "application/x-www-form-urlencoded",bool secValid=true) { method = method.ToUpper(); if (secValid == false) { ServicePointManager.ServerCertificateValidationCallback = validSecurity; } System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12; if (method == "GET") { try { WebRequest request2 = WebRequest.Create(@url); request2.Method = method; if (headers != null) { for (int i = 0; i < headers.GetLength(0); i++) { if (headers[i].Split(':').Length < 2) { continue; } if (headers[i].Split(':').Length > 1) { if (headers[i].Split(':')[0] == "Content-Type") { request2.ContentType = headers[i].Split(':')[1]; continue; } } request2.Headers.Add(headers[i]); } } WebResponse response2 = request2.GetResponse(); try { Stream stream = response2.GetResponseStream(); StreamReader reader = new StreamReader(stream, encoding); string content2 = reader.ReadToEnd(); return content2; } catch (WebException webEx) { if (webEx.Response is HttpWebResponse errorResponse) { string errorBody; using (Stream stream = errorResponse.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { errorBody = reader.ReadToEnd(); } return errorBody; } else { Console.WriteLine($"WebException: {webEx.Message}"); return webEx.Message; } } } catch (Exception ex) { ErrorMessage = ex.Message; return ""; } } if (method == "POST") { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = method; request.Timeout = 1000000; request.ContentType = ContentType; if (headers != null) { for (int i = 0; i < headers.GetLength(0); i++) { if (headers[i].Split(':').Length < 2) { continue; } if (headers[i].Split(':').Length > 1) { if (headers[i].Split(':')[0] == "Host") { request.Host = headers[i].Split(':')[1]; continue; } else if (headers[i].Split(':')[0] == "Content-Type") { request.ContentType = headers[i].Split(':')[1]; continue; } else if (headers[i].Split(':')[0] == "Connection") { request.KeepAlive = headers[i].Split(':')[1] == "close" ? false : true; continue; } else if (headers[i].Split(':')[0] == "Accept") { request.Accept = headers[i].Split(':')[1]; continue; } } request.Headers.Add(headers[i]); } } request.ContentLength = data.Length; try { outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); sr.Close(); sr.Dispose(); return content; } catch (WebException webEx) { if (webEx.Response is HttpWebResponse errorResponse) { string errorBody; using (Stream stream = errorResponse.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { errorBody = reader.ReadToEnd(); } return errorBody; } else { Console.WriteLine($"WebException: {webEx.Message}"); return webEx.Message; } } } catch (Exception ex) { ErrorMessage = ex.Message; return ""; } } ErrorMessage = "不正确的方法类型。(目前仅支持GET/POST)"; return ""; }//get response result 

一些基本说明可参考我的文章:https://blog.ZEEKLOG.net/michaelline/article/details/139123272?spm=1011.2415.3001.5331

成功调用会返回如下JSON:

{"id":"cgt-20251000000008-xxxbk"}

其中的 id 就是创建成功后的任务ID,此时模型正在生成视频...

查询视频生成任务

根据创建视频成功后提供的ID,可以通过查询视频生成任务 API,轮询其生成状态,是否完成。Doubao_QueryVideoTask 方法可产现对任务ID的详情查询,基本说明如下表:

序号参数名称参数类型说明
1taskidstring要查询的视频生成任务 ID 字符串
2ApiKeystring在火山引擎平台申请的API访问令牌

方法示例代码如下:

string; string; public void Doubao_QueryVideoTask(string taskid, string ApiKey) { string ApiUrl = "https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks/" + taskid; WebService ws = new WebService(); string[] headers = new string[2]; headers[0] = "Content-Type:application/json"; headers[1] = "Authorization:Bearer " + ApiKey + "";;; string rs2 = ws.GetResponseResult(ApiUrl, Encoding.UTF8, "GET", "", headers); ErrorMessage = ws.ErrorMessage; ResultJson = rs2; }

成功后会返回如下示例:

{"id":"cgt-2025108-whpbk","model":"doubao-seedance-1-0-pro-250528", "status":"succeeded", "content": {"video_url":"xxx"}, "usage":{"completion_tokens":103818,"total_tokens":103818}, "created_at":1763455989,"updated_at":1763456064, "seed":11, "resolution":"720p", "ratio":"16:9", "duration":5, framespersecond":24}

其中 status 为状态码,succeeded 表示已完成(枚举值包括:queued:排队中、running:任务运行中、cancelled:取消任务,取消状态24h自动删除(只支持排队中状态的任务被取消)、succeeded: 任务成功、failed:任务失败。)。

如果返回JSON中存在 content.video_url 字段的url内容,即可以访问URL或下载播放视频,查看视频生成效果。

查询视频生成任务列表

对于未及时查询单一任务ID的或想查询所有生成中或已完成的视频任务列表,可使用查询视频生成任务列表 API 实现。 该API仅支持查询最近 7 天的历史数据。时间计算统一采用UTC时间戳,返回的7天历史数据范围以用户实际发起批量查询请求的时刻为基准(精确到秒),时间戳区间为 [T-7天, T)。

Doubao_QueryVideoTaskList 方法可产现对任务ID列表的详情查询,基本说明如下表:

序号参数名称参数类型说明
1page_numint

设置查询列表页的起始页码,默认为1,最大为500

2page_sizeint设置查询列表页的每页数量,默认为500,最大为500
3filter_statusstring

视频生成状态的过滤条件,默认为 "succeeded"(成功)

(枚举值包括:queued:排队中、running:任务运行中、cancelled:取消任务,取消状态24h自动删除(只支持排队中状态的任务被取消)、succeeded: 任务成功、failed:任务失败。)

4ApiKeystring在火山引擎平台申请的API访问令牌

方法示例代码如下:

string; string; public void Doubao_QueryVideoTaskList(int page_num=1,int page_size=500,string filter_status= "succeeded",string ApiKey) { string postData = "page_num=" + page_num.ToString() + "&page_size=" + page_size.ToString() + "&filter.status=" + filter_status; string ApiUrl = "https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks?"+postData; WebService ws = new WebService(); string[] headers = new string[2]; headers[0] = "Content-Type:application/json"; headers[1] = "Authorization:Bearer " + ApiKey + "";;; string rs2 = ws.GetResponseResult(ApiUrl, Encoding.UTF8, "GET", "", headers); ErrorMessage = ws.ErrorMessage; ResultJson = rs2; }

成功调用后会返回如下示例:

{"total":5,"items":[ {"id":"cgt-2025","model":"doubao-seedance-1-0-pro-250528"," status":"succeeded", "content":{"video_url":"https://ark"},"usage":{"completion_tokens":103818,"total_tokens":103818},"created_at":1763455989,"updated_at":1763456064,"seed":11,"resolution":"720p","ratio":"16:9","duration":5,"framespersecond":24}, {"id":"cgt-2025","model":"doubao-seedance-1-0-pro-250528", "status":"succeeded", "content":{"video_url":"https://ark"},"usage":{"completion_tokens":586092,"total_tokens":586092},"created_at":1763082221,"updated_at":1763082414,"seed":26522,"resolution":"1080p","ratio":"3:4","duration":12,"framespersecond":24}, {"id":"cgt-2025","model":"doubao-seedance-1-0-pro-250528", "status":"succeeded", "content":{"video_url":"https://arkt"},"usage":{"completion_tokens":586092,"total_tokens":586092},"created_at":1763082221,"updated_at":1763082446,"seed":81742,"resolution":"1080p","ratio":"3:4","duration":12,"framespersecond":24}, {"id":"cgt-2025","model":"doubao-seedance-1-0-pro-250528", "status":"succeeded", "content":{"video_url":"https://ark-"},"usage":{"completion_tokens":586092,"total_tokens":586092},"created_at":1763082221,"updated_at":1763082376,"seed":46824,"resolution":"1080p","ratio":"3:4","duration":12,"framespersecond":24}, {"id":"cgt-2025","model":"doubao-seedance-1-0-pro-250528", "status":"succeeded", "content":{"video_url":"https://ark"},"usage":{"completion_tokens":586092,"total_tokens":586092},"created_at":1763082221,"updated_at":1763082374,"seed":52271,"resolution":"1080p","ratio":"3:4","duration":12,"framespersecond":24}]}

可通过下列示例代码遍历生成的 videio_url (可访问视频):

string; Newtonsoft.Json.Linq.JObject rs2 = Newtonsoft.Json.Linq.JObject.Parse(ResultJson); for (int i = 0; i < rs2["items"].Count(); i++) { list+ = rs2["items"][i]["content"]["video_url"].ToString() + "\r\n"; }

红颜如霜

生成的视频效果还是有点意思,AI模型还是充分发挥了其想象力,对车辆情况的识别,行走的创意,变化的过度还算可以,毕竟只有12秒的时间。人物面部模型的五官部分稍微显得一点点 “拥挤” 变形,但真实还原度还是非常不错的。另外,要求加入的《红颜如霜》的音乐背景模型没有混入合成,不知是不是因为音乐版权的问题,于是自己录制了一首歌曲片断,使用手机视频编辑软件进行了编辑合成,最终效果如下视频:

AI模型生成的视频

小结

生成视频会消耗很多的 tokens,由于注册的时候使用的先用后付模式,发现体验生成视频模型后,帐户发生欠费,还请注意费用说明及消耗情况,满足自己的需求即可。

API的一些相关文档可以参考以下链接:

文档中心入口:

https://www.volcengine.com/docs/82379

创建视频生成任务 API:

https://www.volcengine.com/docs/82379/1520757

查询视频生成任务 API:

https://www.volcengine.com/docs/82379/1521309

查询视频生成任务列表:

https://www.volcengine.com/docs/82379/1521675

Read more

AI的提示词专栏:Prompt 驱动的结构化抽取,从文本中提取表格

AI的提示词专栏:Prompt 驱动的结构化抽取,从文本中提取表格

AI的提示词专栏:Prompt 驱动的结构化抽取,从文本中提取表格 本文围绕 Prompt 驱动的结构化抽取展开,先阐述其价值 —— 解决传统人工整理效率低、代码开发场景适应性差的痛点,借助大语言模型实现非结构化文本到表格的高效转化。接着解析核心概念,明确结构化抽取三要素及 Prompt 的赋能逻辑,随后提供基础版(适简单文本)、进阶版(适复杂文本)、优化版(适专业文本)三类 Prompt 设计框架,搭配实战案例说明操作要点。还通过内容创作、电商运营、学术研究三个跨场景案例,给出行业适配技巧,并针对字段遗漏、信息错误等六类常见问题提供解决方案。最后总结核心知识点,推荐工具与技术趋势,设计课后练习,助力读者掌握从简单到复杂场景的结构化抽取技巧。 人工智能专栏介绍     人工智能学习合集专栏是 AI 学习者的实用工具。它像一个全面的 AI 知识库,把提示词设计、AI 创作、智能绘图等多个细分领域的知识整合起来。无论你是刚接触 AI 的新手,还是有一定基础想提升的人,

一文读懂openClaw:GitHub史上增长最快的开源AI个人助手,附部署教程与免费大模型推荐

一文读懂openClaw:GitHub史上增长最快的开源AI个人助手,附部署教程与免费大模型推荐

哲人言:道生一,一生二,二生三,三生万物。——《道德经》 创作者:查老师并不渣(ZEEKLOG)(一个在哲学与生活中寻找平衡的思考者😊) 目录 引言 一、openClaw 是何方神圣? 与其他项目的对比 二、系统架构深度解析 1. Gateway(网关) 2. Agent(智能体) 3. Skills(技能) 4. Channels(通道) 5. Nodes(节点) 6. Memory(记忆) 三、为什么 openClaw 能引爆社区? 四、快速部署与配置指南 系统要求 一键安装 初始化配置 关键环境变量 Docker 部署(可选) 五、

QClaw 上手指南:我用了一周龙虾,感觉自己白用了两年 AI

QClaw 上手指南:我用了一周龙虾,感觉自己白用了两年 AI

欢迎来到我的博客,代码的世界里,每一行都是一个故事 🎏:你只管努力,剩下的交给时间 🏠 :小破站 QClaw 上手指南:我用了一周龙虾,感觉自己白用了两年 AI * 先说清楚:OpenClaw 是什么,龙虾又是怎么来的 * 第一次打开:它先问你是谁 * 微信直联:手机变成了 AI 的遥控器 * 接入自定义模型:你的 API 你做主 * Skills 插件:能力边界一直在扩 * 角色系统:不是换个语气,是换个工作模式 * 定时任务:让 AI 主动替你干活 * 它是怎么「记住你」的 * 本地跑意味着什么 * 适合什么人用 * 最后 如果你最近在关注 AI 工具圈,大概率听说过一个叫 OpenClaw 的东西,中文社区管它叫「龙虾」。这个开源项目在

一个人就是一支影视团队:实测国内最强影视级 AI 视频创作平台 TapNow——告别抽卡,导演级精准控制

一个人就是一支影视团队:实测国内最强影视级 AI 视频创作平台 TapNow——告别抽卡,导演级精准控制

实测国内最强影视级 AI 视频平台 TapNow:告别“盲盒抽卡”,实现导演级精准调度         在过去的一年里,文生视频赛道经历了爆发式增长。但对于真正需要将 AI 投入到生产环境中的创作者、产品经理和开发者来说,目前的 AI 视频工具普遍存在一个致命痛点——不可控。        跑偏的物理规律、诡异的肢体形变、如同“开盲盒”般的提示词玄学,让很多原本充满创意的构想,最终沦为废弃的半成品。如果你也受够了这种低效的“抽卡式”创作,那么今天介绍的这款号称国内最强影视级 AI 视频创作平台——TapNow,或许能彻底重塑你的工作流。 核心痛点突破:从“AI 幻觉”到真正的物理一致性 技术社区的受众深知,评价一个 AI 视频大模型底座的强弱,不仅看它能生成多惊艳的单帧,更要看它在长镜头下的时空一致性。 TapNow 在底层架构上进行了深度优化,重点解决了以下三个核心问题: 1. 极高保真度的物理交互: 无论是光影在水面的流动、烟雾的自然消散,