();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/HowToFileUploads/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "iisSettings": {
3 | "windowsAuthentication": false,
4 | "anonymousAuthentication": true,
5 | "iisExpress": {
6 | "applicationUrl": "http://localhost:50792",
7 | "sslPort": 44343
8 | }
9 | },
10 | "profiles": {
11 | "IIS Express": {
12 | "commandName": "IISExpress",
13 | "launchBrowser": true,
14 | "environmentVariables": {
15 | "ASPNETCORE_ENVIRONMENT": "Development"
16 | }
17 | },
18 | "HowToFileUploads": {
19 | "commandName": "Project",
20 | "launchBrowser": true,
21 | "applicationUrl": "https://localhost:5001;http://localhost:5000",
22 | "environmentVariables": {
23 | "ASPNETCORE_ENVIRONMENT": "Development"
24 | }
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/HowToFileUploads/Startup.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Builder;
2 | using Microsoft.AspNetCore.Hosting;
3 | using Microsoft.Extensions.DependencyInjection;
4 |
5 | namespace HowToFileUploads
6 | {
7 | public class Startup
8 | {
9 | public void ConfigureServices(IServiceCollection services)
10 | {
11 | services.AddMvc();
12 | }
13 |
14 | public void Configure(IApplicationBuilder app, IHostingEnvironment env)
15 | {
16 | if (env.IsDevelopment())
17 | {
18 | app.UseDeveloperExceptionPage();
19 | }
20 |
21 | app.UseMvcWithDefaultRoute();
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/HowToFileUploads/Views/Home/Index.cshtml:
--------------------------------------------------------------------------------
1 | Home Page
2 |
3 |
4 |
5 |
6 |
Single File Upload
7 |
12 |
13 |
14 |
15 |
16 |
Mutliple File Upload
17 |
22 |
23 |
24 |
25 |
File in Model Upload
26 |
32 |
33 |
34 |
35 |
36 |
37 |
Single File Upload (JS)
38 |
39 |
40 |
41 |
42 |
43 |
Multiple File Upload (JS)
44 |
45 |
46 |
47 |
48 |
49 |
File in Model Upload (JS)
50 |
51 |
52 |
53 |
54 | @section scripts {
55 |
89 | }
--------------------------------------------------------------------------------
/HowToFileUploads/Views/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | @ViewBag.Title
7 |
8 |
9 |
10 | @RenderBody()
11 |
12 |
13 |
14 |
15 | @RenderSection("scripts")
16 |
17 |
18 |
--------------------------------------------------------------------------------
/HowToFileUploads/Views/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
--------------------------------------------------------------------------------
/HowToFileUploads/Views/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
4 |
--------------------------------------------------------------------------------
/HowToFileUploads/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/HowToFileUploads/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Warning"
5 | }
6 | },
7 | "AllowedHosts": "*"
8 | }
9 |
--------------------------------------------------------------------------------
/HowToVideoFiles/BackgroundTasks/BackgroundQueue.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace HowToVideoFiles.BackgroundTasks
7 | {
8 | public interface IBackgroundQueue
9 | {
10 | void QueueTask(Func task);
11 |
12 | Task> PopQueue(CancellationToken cancellationToken);
13 | }
14 |
15 | public class BackgroundQueue : IBackgroundQueue
16 | {
17 | private ConcurrentQueue> Tasks;
18 |
19 | private SemaphoreSlim Signal;
20 |
21 | public BackgroundQueue()
22 | {
23 | Tasks = new ConcurrentQueue>();
24 | Signal = new SemaphoreSlim(0);
25 | }
26 |
27 | public void QueueTask(Func task)
28 | {
29 | Tasks.Enqueue(task);
30 | Signal.Release();
31 | }
32 |
33 | public async Task> PopQueue(CancellationToken cancellationToken)
34 | {
35 | await Signal.WaitAsync(cancellationToken);
36 | Tasks.TryDequeue(out var task);
37 |
38 | return task;
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/HowToVideoFiles/BackgroundTasks/QueueService.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.Hosting;
2 | using System;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace HowToVideoFiles.BackgroundTasks
7 | {
8 | public class QueueService : BackgroundService
9 | {
10 | private IBackgroundQueue _queue;
11 |
12 | public QueueService(IBackgroundQueue queue)
13 | {
14 | _queue = queue;
15 | }
16 |
17 | protected async override Task ExecuteAsync(CancellationToken stoppingToken)
18 | {
19 | while (!stoppingToken.IsCancellationRequested)
20 | {
21 | var task = await _queue.PopQueue(stoppingToken);
22 |
23 | if (stoppingToken.IsCancellationRequested)
24 | {
25 | return;
26 | }
27 |
28 | using (var source = new CancellationTokenSource())
29 | {
30 | source.CancelAfter(TimeSpan.FromMinutes(1));
31 | var timeoutToken = source.Token;
32 |
33 | await task(timeoutToken);
34 | }
35 | }
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/HowToVideoFiles/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using HowToVideoFiles.BackgroundTasks;
2 | using Microsoft.AspNetCore.Hosting;
3 | using Microsoft.AspNetCore.Http;
4 | using Microsoft.AspNetCore.Mvc;
5 | using System;
6 | using System.IO;
7 | using System.Linq;
8 | using System.Threading;
9 | using System.Threading.Tasks;
10 | using Xabe.FFmpeg;
11 | using Xabe.FFmpeg.Enums;
12 |
13 | namespace HowToVideoFiles.Controllers
14 | {
15 | public class HomeController : Controller
16 | {
17 | private string _dir;
18 | private IBackgroundQueue _queue;
19 |
20 | public HomeController(
21 | IHostingEnvironment env,
22 | IBackgroundQueue queue)
23 | {
24 | _dir = env.WebRootPath;
25 | _queue = queue;
26 | }
27 |
28 | [HttpGet]
29 | public IActionResult Index() => View();
30 |
31 | [HttpPost]
32 | public async Task UploadFile(IFormFile file)
33 | {
34 | using (var fileStream =
35 | new FileStream(Path.Combine(_dir, "file.mp4"), FileMode.Create, FileAccess.Write))
36 | {
37 | await file.CopyToAsync(fileStream);
38 | }
39 |
40 | return Ok();
41 | }
42 |
43 | [HttpPost]
44 | public IActionResult TrimVideo(double start, double end)
45 | {
46 | _queue.QueueTask(async token =>
47 | {
48 | await ConvertVideo(start, end, token);
49 | });
50 |
51 | return RedirectToAction("Index");
52 | }
53 |
54 | public async Task ConvertVideo(double start, double end, CancellationToken ct)
55 | {
56 | try
57 | {
58 | var input = Path.Combine(_dir, "file.mp4");
59 | var output = Path.Combine(_dir, "converted.mp4");
60 |
61 | FFmpeg.ExecutablesPath = Path.Combine(_dir, "ffmpeg");
62 |
63 | var startSpan = TimeSpan.FromSeconds(start);
64 | var endSpan = TimeSpan.FromSeconds(end);
65 | var duration = endSpan - startSpan;
66 |
67 | var info = await MediaInfo.Get(input);
68 |
69 | var videoStream = info.VideoStreams.First()
70 | .SetCodec(VideoCodec.H264)
71 | .SetSize(VideoSize.Hd480)
72 | .Split(startSpan, duration);
73 |
74 | await Conversion.New()
75 | .AddStream(videoStream)
76 | .SetOutput(output)
77 | .Start(ct);
78 | }
79 | catch (Exception e)
80 | {
81 | Console.WriteLine(e.Message);
82 | }
83 |
84 | return true;
85 | }
86 |
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/HowToVideoFiles/HowToVideoFiles.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.2
5 | InProcess
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/HowToVideoFiles/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore;
7 | using Microsoft.AspNetCore.Hosting;
8 | using Microsoft.Extensions.Configuration;
9 | using Microsoft.Extensions.Logging;
10 |
11 | namespace HowToVideoFiles
12 | {
13 | public class Program
14 | {
15 | public static void Main(string[] args)
16 | {
17 | CreateWebHostBuilder(args).Build().Run();
18 | }
19 |
20 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21 | WebHost.CreateDefaultBuilder(args)
22 | .UseStartup();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/HowToVideoFiles/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "iisSettings": {
3 | "windowsAuthentication": false,
4 | "anonymousAuthentication": true,
5 | "iisExpress": {
6 | "applicationUrl": "http://localhost:50905",
7 | "sslPort": 44383
8 | }
9 | },
10 | "profiles": {
11 | "IIS Express": {
12 | "commandName": "IISExpress",
13 | "launchBrowser": true,
14 | "environmentVariables": {
15 | "ASPNETCORE_ENVIRONMENT": "Development"
16 | }
17 | },
18 | "HowToVideoFiles": {
19 | "commandName": "Project",
20 | "launchBrowser": true,
21 | "applicationUrl": "https://localhost:5001;http://localhost:5000",
22 | "environmentVariables": {
23 | "ASPNETCORE_ENVIRONMENT": "Development"
24 | }
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/HowToVideoFiles/Startup.cs:
--------------------------------------------------------------------------------
1 | using HowToVideoFiles.BackgroundTasks;
2 | using Microsoft.AspNetCore.Builder;
3 | using Microsoft.AspNetCore.Hosting;
4 | using Microsoft.Extensions.DependencyInjection;
5 |
6 | namespace HowToVideoFiles
7 | {
8 | public class Startup
9 | {
10 | public void ConfigureServices(IServiceCollection services)
11 | {
12 | services.AddHostedService();
13 | services.AddSingleton();
14 | services.AddMvc();
15 | }
16 |
17 | public void Configure(IApplicationBuilder app, IHostingEnvironment env)
18 | {
19 | app.UseDeveloperExceptionPage();
20 | app.UseStaticFiles();
21 | app.UseMvcWithDefaultRoute();
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/HowToVideoFiles/Views/Home/Index.cshtml:
--------------------------------------------------------------------------------
1 | Working with Videos
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
25 |
26 |
27 | @section scripts {
28 |
29 |
61 | }
--------------------------------------------------------------------------------
/HowToVideoFiles/Views/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | @ViewBag.Title
7 |
8 |
9 |
10 | @RenderBody()
11 |
12 |
13 | @RenderSection("scripts", false)
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/HowToVideoFiles/Views/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
--------------------------------------------------------------------------------
/HowToVideoFiles/Views/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
4 |
--------------------------------------------------------------------------------
/HowToVideoFiles/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/HowToVideoFiles/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Warning"
5 | }
6 | },
7 | "AllowedHosts": "*"
8 | }
9 |
--------------------------------------------------------------------------------