-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
171 lines (146 loc) · 5.58 KB
/
Program.cs
File metadata and controls
171 lines (146 loc) · 5.58 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
161
162
163
164
165
166
167
168
169
170
171
using Qencode.Api.CSharp.Client;
using Qencode.Api.CSharp.Client.Classes;
using Qencode.Api.CSharp.Client.Exceptions;
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading;
using TusDotNetClient;
namespace CustomEncodingJsonTUSUploadExample
{
class Program
{
/*
* This example upload a local file,
* transcode it and then,
* download the transcoded file
*/
static void Main(string[] args)
{
// You can find your API key under Project settings in your Dashboard on Qencode portal
const string apiKey = "YOUR_API_KEY_HERE";
const string relative_output_dir = "TranscodedOutput"; // relative output dir
// This is the full file name of the source video
string sourceVideoFullFileName = "E:\\dev\\My\\Sample720.flv";
// if an argument is specified, the source video file will be readed from the first argument
if (args.Length >= 1 && !string.IsNullOrEmpty(args[0]))
sourceVideoFullFileName = args[0];
try
{
// get access token
Console.WriteLine("Requesting access token...");
var q = new QencodeApiClient(apiKey);
Console.WriteLine("\taccess token: '" + q.AccessToken + "'");
// create a new task
Console.WriteLine("Creating a new task...");
var task = q.CreateTask();
Console.WriteLine("\tcreated new task with token: '" + task.TaskToken + "' and url for direct video upload (TUS) '" + task.UploadUrl + "'");
// direct video upload - initiate upload (get endpoint for task)
Console.WriteLine("Initiate upload...");
var srcFI = new FileInfo(sourceVideoFullFileName);
var client = new TusClient();
var tusUploadLocationUrl = client.CreateAsync(task.UploadUrl + "/" + task.TaskToken, srcFI.Length).Result;
Console.WriteLine("\tobtained TUS upload location: '" + tusUploadLocationUrl + "'");
// direct video upload - send data
var uploadOperation = client.UploadAsync(tusUploadLocationUrl, srcFI);
Console.WriteLine("\ttransfer started");
uploadOperation.Progressed += (transferred, total) =>
{
Console.CursorLeft = 0;
Console.Write($"Progress: {transferred}/{total}");
};
Console.WriteLine();
Console.WriteLine();
uploadOperation.GetAwaiter().GetResult();
Console.WriteLine("\tupload done");
// define a custom task by reading query.json and filling the ##TUS_FILE_UUID## placeholder
var tusFileUUID = tusUploadLocationUrl.Substring(tusUploadLocationUrl.LastIndexOf('/') + 1);
var customTrascodingJSON = File.ReadAllText("query.json").Replace("##TUS_FILE_UUID##", tusFileUUID);
var customTranscodingParams = CustomTranscodingParams.FromJSON(customTrascodingJSON);
// start a custom task
Console.WriteLine("Custom task starting..");
Console.WriteLine(customTrascodingJSON);
// start a custom task - set event handler
bool taskCompletedOrError = false;
task.TaskCompleted = new RunWorkerCompletedEventHandler(
delegate (object o, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
taskCompletedOrError = true;
Console.WriteLine("Error: ", e.Error.Message);
}
var response = e.Result as TranscodingTaskStatus;
if (response.error == 1)
{
taskCompletedOrError = true;
Console.WriteLine("Error: " + response.error_description);
}
else if (response.status == "completed")
{
taskCompletedOrError = true;
Console.WriteLine("Video urls: ");
foreach (var video in response.videos)
Console.WriteLine(video.url);
}
else
{
Console.WriteLine(response.status);
}
});
// start a custom task - start
Console.WriteLine("\tstarting...");
var started = task.StartCustom(customTranscodingParams); // starts and poll
// waiting
Console.WriteLine("Waiting..");
while (!taskCompletedOrError)
Thread.Sleep(1000);
// get download url
if (task.LastStatus == null)
throw new InvalidOperationException("Unable to obtain download url");
var outputDownloadUrl = new Uri(task.LastStatus.videos.First().url);
Console.WriteLine("Output download url: '" + outputDownloadUrl.ToString() + "'");
string output_file_name = GetOutputFileName(sourceVideoFullFileName, outputDownloadUrl.Segments.Last());
Console.WriteLine("Output file name: '" + output_file_name + "'");
// download
Console.WriteLine("Downloading..");
HttpFileDownload(outputDownloadUrl, relative_output_dir, output_file_name);
Console.WriteLine("\tdownload done");
Environment.Exit(0);
}
catch (QencodeApiException e)
{
Console.WriteLine("Qencode API exception: " + e.Message);
Environment.Exit(-1);
}
catch (QencodeException e)
{
Console.WriteLine("API call failed: " + e.Message);
Environment.Exit(-1);
}
}
private static string GetOutputFileName(string inputFullFileName, string outputProposedFileName)
{
var inputFI = new FileInfo(inputFullFileName);
var ouputFI = new FileInfo(outputProposedFileName);
return inputFI.Name + ouputFI.Extension;
}
private static void HttpFileDownload(Uri url, string localFolder, string localFileName)
{
if (!Directory.Exists($"./{localFolder}"))
Directory.CreateDirectory($"./{localFolder}");
var relativeFileName = $"./{localFolder}/{localFileName}";
if (File.Exists(relativeFileName))
{
File.Delete(relativeFileName);
Console.WriteLine("\toutput file already existing: deleted");
}
using (var wc = new System.Net.WebClient())
wc.DownloadFile(url, relativeFileName);
}
}
}