-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_to_docs.cs
More file actions
160 lines (137 loc) · 5.83 KB
/
Copy pathvideo_to_docs.cs
File metadata and controls
160 lines (137 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Video-to-Docs: Full pipeline example (C# / .NET 6+)
//
// Submits a video URL, polls for analysis, triggers AI rewrite,
// and downloads the result as markdown, DOCX, and PDF.
//
// Usage:
// export DOCSIE_API_KEY="your_key"
// dotnet script video_to_docs.cs https://example.com/video.mp4
//
// Or create a project:
// dotnet new console -n VideoToDocs
// cp video_to_docs.cs VideoToDocs/Program.cs
// cd VideoToDocs && dotnet run -- https://example.com/video.mp4 sop
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
var apiKey = Environment.GetEnvironmentVariable("DOCSIE_API_KEY")
?? throw new Exception("Set DOCSIE_API_KEY environment variable");
var baseUrl = Environment.GetEnvironmentVariable("DOCSIE_BASE_URL") ?? "https://app.docsie.io";
var api = $"{baseUrl}/api_v2/003";
if (args.Length < 1) { Console.Error.WriteLine("Usage: dotnet run -- <video_url> [doc_style]"); return; }
var videoUrl = args[0];
var docStyle = args.Length > 1 ? args[1] : "guide";
using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(60) };
client.DefaultRequestHeaders.Add("Authorization", $"Api-Key {apiKey}");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// ── Helpers ──────────────────────────────────────────────
async Task<JsonElement> ApiGet(string path)
{
var resp = await client.GetStringAsync($"{api}{path}");
return JsonDocument.Parse(resp).RootElement;
}
async Task<JsonElement> ApiPost(string path, object body)
{
var json = JsonSerializer.Serialize(body);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var resp = await client.PostAsync($"{api}{path}", content);
var text = await resp.Content.ReadAsStringAsync();
return JsonDocument.Parse(text).RootElement;
}
async Task<JsonElement> Poll(string path, int timeoutSec = 900, int intervalSec = 15)
{
var deadline = DateTime.UtcNow.AddSeconds(timeoutSec);
while (DateTime.UtcNow < deadline)
{
var data = await ApiGet(path);
var status = Str(data, "status");
if (string.IsNullOrEmpty(status)) status = Str(data, "job_status");
if (status is "done" or "failed" or "canceled") return data;
Console.WriteLine($" ... {status}");
await Task.Delay(intervalSec * 1000);
}
throw new TimeoutException($"Timed out after {timeoutSec}s");
}
string Str(JsonElement el, string key)
{
if (el.TryGetProperty(key, out var val) && val.ValueKind != JsonValueKind.Null)
return val.ToString();
return "";
}
JsonElement Obj(JsonElement el, string key)
{
if (el.TryGetProperty(key, out var val) && val.ValueKind == JsonValueKind.Object)
return val;
return JsonDocument.Parse("{}").RootElement;
}
int ArrLen(JsonElement el, string key)
{
if (el.TryGetProperty(key, out var val) && val.ValueKind == JsonValueKind.Array)
return val.GetArrayLength();
return 0;
}
// ── Pipeline ─────────────────────────────────────────────
// 1. Submit
Console.WriteLine($"==> Submitting video job (style={docStyle})...");
var submitResp = await ApiPost("/video-to-docs/submit/", new
{
video_url = videoUrl,
quality = "draft",
language = "english",
doc_style = docStyle,
auto_generate = false,
});
var jobId = Str(submitResp, "job_id");
Console.WriteLine($" Job ID: {jobId}");
// 2. Poll analysis
Console.WriteLine("==> Polling analysis...");
await Poll($"/video-to-docs/{jobId}/status/");
// 3. Get result
Console.WriteLine("==> Fetching result...");
var result = await ApiGet($"/video-to-docs/{jobId}/result/");
var markdown = Str(result, "markdown");
var transcription = Str(result, "transcription");
Console.WriteLine($" Markdown: {markdown.Length} chars");
Console.WriteLine($" Transcription: {transcription.Length} chars");
Console.WriteLine($" Sections: {ArrLen(result, "sections")}");
Console.WriteLine($" Images: {ArrLen(result, "images")}");
await File.WriteAllTextAsync("analysis_result.md", markdown);
await File.WriteAllTextAsync("transcription.txt", transcription);
Console.WriteLine(" Saved: analysis_result.md, transcription.txt");
// 4. Generate
Console.WriteLine("==> Triggering AI rewrite...");
var genResp = await ApiPost($"/video-to-docs/{jobId}/generate/", new
{
doc_style = docStyle,
output_formats = new[] { "md", "docx", "pdf" },
});
var genJobId = Str(genResp, "generate_job_id");
Console.WriteLine($" Generate job: {genJobId}");
Console.WriteLine("==> Polling generate job...");
var genResult = await Poll($"/jobs/{genJobId}/", 300, 10);
var gen = Obj(genResult, "result");
Console.WriteLine($" Title: {Str(gen, "title")}");
Console.WriteLine($" Words: {Str(gen, "input_word_count")} -> {Str(gen, "output_word_count")}");
await File.WriteAllTextAsync("generated.md", Str(gen, "markdown"));
Console.WriteLine(" Saved: generated.md");
// 5. Download exports
var exports = Obj(gen, "exports");
foreach (var fmt in new[] { "docx", "pdf" })
{
var info = Obj(exports, fmt);
var exportJobId = Str(info, "job_id");
if (string.IsNullOrEmpty(exportJobId)) continue;
Console.WriteLine($"==> Waiting for {fmt.ToUpper()} export...");
var exportResult = await Poll($"/jobs/{exportJobId}/", 120, 5);
var exportData = Obj(exportResult, "result");
var url = Str(exportData, "url");
var filename = Str(exportData, "filename");
if (string.IsNullOrEmpty(filename)) filename = $"output.{fmt}";
if (!string.IsNullOrEmpty(url))
{
var bytes = await client.GetByteArrayAsync(url);
await File.WriteAllBytesAsync(filename, bytes);
Console.WriteLine($" Downloaded: {filename} ({bytes.Length / 1024} KB)");
}
}
Console.WriteLine("\n==> Complete!");