Skip to content

Commit

Permalink
Basic threading
Browse files Browse the repository at this point in the history
Fixed paths for linux & osx
  • Loading branch information
bumbummen99 committed Dec 30, 2018
1 parent 6e087b0 commit 6aefc39
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 48 deletions.
17 changes: 15 additions & 2 deletions SouthParkDownloaderNetCore/Functionality/FFMpeg.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using SouthParkDownloaderNetCore.Helpers;
using SouthParkDownloaderNetCore.Logic;
Expand All @@ -17,12 +18,24 @@ public static String Executable
}
}

public static Boolean Mux( String directory, String filename )
public static Boolean Mux( String directory, String[] fileList, String filename )
{
CreateFilesList(directory, fileList);
String arguments = "-f concat -safe 0 -i files.txt -c copy \"" + filename + '"';
if (ProcessHelper.Run(directory, Executable, arguments))
String logFile = directory + "/ffmpeg.log";
Boolean result = ProcessHelper.Run(directory, Executable, arguments, null, logFile); //ffmpeg for some reason writes to error log
File.Delete(directory + "/files.txt");
if (result)
return true;
return false;
}

public static void CreateFilesList( String targetDirectory, String[] fileList )
{
StreamWriter sw = File.CreateText(targetDirectory + "/files.txt");
foreach (String filePath in fileList)
sw.Write("file '" + filePath + '\'' + sw.NewLine);
sw.Close();
}
}
}
5 changes: 3 additions & 2 deletions SouthParkDownloaderNetCore/Functionality/YouTubeDL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public static String Executable

public static Boolean Download( String url, String directory )
{
String arguments = url;
if (ProcessHelper.Run(directory, Executable, arguments))
String arguments = "-q " + url;
String logFile = directory + "/ytdl.log";
if (ProcessHelper.Run(directory, Executable, arguments, logFile))
return true;
return false;
}
Expand Down
19 changes: 18 additions & 1 deletion SouthParkDownloaderNetCore/Helpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace SouthParkDownloaderNetCore.Helpers
{
class ProcessHelper
{
public static Boolean Run(String workingDirectory, String exeOrCommand, String arguments)
public static Boolean Run(String workingDirectory, String exeOrCommand, String arguments, String logFile = null, String errorLogFile = null)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
Expand All @@ -28,10 +29,26 @@ public static Boolean Run(String workingDirectory, String exeOrCommand, String a
var escapedArgs = cmd.Replace("\"", "\\\"");
startInfo.Arguments = $"-c \"{escapedArgs}\"";
}

startInfo.RedirectStandardOutput = logFile != null;
startInfo.RedirectStandardError = errorLogFile != null;

process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

if (logFile != null)
{
String output = process.StandardOutput.ReadToEnd();
File.WriteAllText(logFile, output);
}

if (errorLogFile != null)
{
String error = process.StandardError.ReadToEnd();
File.WriteAllText(errorLogFile, error);
}

if (process.ExitCode != 0)
return false;
return true;
Expand Down
18 changes: 9 additions & 9 deletions SouthParkDownloaderNetCore/Install/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public Setup( ApplicationLogic applicationLogic )
public void setUpFolderStructure()
{
/* Setup folder structures */
applicationLogic.m_dependencyDirectory = Directory.CreateDirectory(applicationLogic.m_workingDirectory + @"\dep").FullName;
applicationLogic.m_dataDirectory = Directory.CreateDirectory(applicationLogic.m_workingDirectory + @"\data").FullName;
applicationLogic.m_tempDiretory = Directory.CreateDirectory(applicationLogic.m_workingDirectory + @"\tmp").FullName;
applicationLogic.m_dependencyDirectory = Directory.CreateDirectory(applicationLogic.m_workingDirectory + "/dep").FullName;
applicationLogic.m_dataDirectory = Directory.CreateDirectory(applicationLogic.m_workingDirectory + "/data").FullName;
applicationLogic.m_tempDiretory = Directory.CreateDirectory(applicationLogic.m_workingDirectory + "/tmp").FullName;
}

public void setUpIndex()
Expand All @@ -49,7 +49,7 @@ public void setUpYoutubeDL()
return;
}

webClient.DownloadFile("https://yt-dl.org/downloads/latest/youtube-dl.exe", applicationLogic.m_dependencyDirectory + @"\youtube-dl.exe");
webClient.DownloadFile("https://yt-dl.org/downloads/latest/youtube-dl.exe", applicationLogic.m_dependencyDirectory + "/youtube-dl.exe");
}

public void setUpFFMpeg()
Expand Down Expand Up @@ -77,11 +77,11 @@ public void setUpFFMpeg()
return;
}

webClient.DownloadFile(url, applicationLogic.m_tempDiretory + @"\ffmpeg-3.4.1.zip");
ZipFile.ExtractToDirectory(applicationLogic.m_tempDiretory + @"\ffmpeg-3.4.1.zip", applicationLogic.m_tempDiretory);
File.Move(applicationLogic.m_tempDiretory + @"\ffmpeg-3.4.1-win64-static\bin\ffmpeg.exe", applicationLogic.m_dependencyDirectory + @"\ffmpeg.exe");
File.Move(applicationLogic.m_tempDiretory + @"\ffmpeg-3.4.1-win64-static\bin\ffplay.exe", applicationLogic.m_dependencyDirectory + @"\ffplay.exe");
File.Move(applicationLogic.m_tempDiretory + @"\ffmpeg-3.4.1-win64-static\bin\ffprobe.exe", applicationLogic.m_dependencyDirectory + @"\ffprobe.exe");
webClient.DownloadFile(url, applicationLogic.m_tempDiretory + "/ffmpeg-3.4.1.zip");
ZipFile.ExtractToDirectory(applicationLogic.m_tempDiretory + "/ffmpeg-3.4.1.zip", applicationLogic.m_tempDiretory);
File.Move(applicationLogic.m_tempDiretory + "/ffmpeg-3.4.1-win64-static/bin/ffmpeg.exe", applicationLogic.m_dependencyDirectory + "/ffmpeg.exe");
File.Move(applicationLogic.m_tempDiretory + "/ffmpeg-3.4.1-win64-static/bin/ffplay.exe", applicationLogic.m_dependencyDirectory + "/ffplay.exe");
File.Move(applicationLogic.m_tempDiretory + "/ffmpeg-3.4.1-win64-static/bin/ffprobe.exe", applicationLogic.m_dependencyDirectory + "/ffprobe.exe");
}

public Boolean IsSetup()
Expand Down
54 changes: 45 additions & 9 deletions SouthParkDownloaderNetCore/Logic/ApplicationLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using SouthParkDownloaderNetCore.Install;
using SouthParkDownloaderNetCore.Database;
using SouthParkDownloaderNetCore.Helpers;
using System.Threading;

namespace SouthParkDownloaderNetCore.Logic
{
Expand All @@ -23,36 +24,38 @@ public String m_indexFile
{
get
{
return m_dataDirectory + @"\data.db";
return m_dataDirectory + "/data.db";
}
}
public String m_youtubeDL
{
get
{
return m_dependencyDirectory + @"\youtube-dl.exe";
return m_dependencyDirectory + "/youtube-dl.exe";
}
}
public String m_ffmpeg
{
get
{
return m_dependencyDirectory + @"\ffmpeg.exe";
return m_dependencyDirectory + "/ffmpeg.exe";
}
}

private ArrayList m_episodes;

private static ApplicationLogic instance;
private static int workingCounter = 0;
private static int workingLimit = 4;
private static int processedCounter = 0;

private static readonly Lazy<ApplicationLogic> lazy =
new Lazy<ApplicationLogic>(() => new ApplicationLogic());
public static ApplicationLogic Instance

{
get
{
if (instance == null)
{
instance = new ApplicationLogic();
}
return instance;
return lazy.Value;
}
}

Expand Down Expand Up @@ -125,11 +128,44 @@ private void PostHelp()

private void Download()
{
int checkCount = m_episodes.Count;
foreach (Episode episode in m_episodes)
{
//wait for free limit...
while (workingCounter >= workingLimit)
{
Thread.Sleep(100);
}
workingCounter += 1;
ParameterizedThreadStart pts = new ParameterizedThreadStart(ProcessEpisode);
Thread th = new Thread(pts);
th.Start(episode);
}

//wait for all threads to complete...
while (processedCounter < checkCount)
{
Thread.Sleep(100);
}
}

private void ProcessEpisode( object obj )
{
Episode episode = (Episode)obj;
try
{
episode.Download();
episode.Merge();
}
catch (Exception ex)
{
Console.WriteLine("Processing episode error: " + ex.Message);
}
finally
{
Interlocked.Decrement(ref workingCounter);
Interlocked.Increment(ref processedCounter);
}
}

private void Merge()
Expand Down
57 changes: 32 additions & 25 deletions SouthParkDownloaderNetCore/Types/Episode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public String FileName
{
get
{
return 'S'+Season+'E'+Number + FSHelper.RemoveSpecialCharacters(this.Name);
return 'S'+Season.ToString()+'E'+Number.ToString() + '-' + FSHelper.RemoveSpecialCharacters(this.Name);
}
}

Expand Down Expand Up @@ -56,6 +56,14 @@ public String Path
}
}

public String ConsoleTag
{
get
{
return "[S" + Season.ToString() + 'E' + Number.ToString() + ']';
}
}

public void Download(Boolean overwrite = false)
{
System.IO.Directory.CreateDirectory(this.SeasonDirectory); // Create directories in case they dont exist
Expand All @@ -64,15 +72,25 @@ public void Download(Boolean overwrite = false)
if (File.Exists(this.Directory + "/dlfinish") && !overwrite)
return; // Skip already downloaded episode

Console.WriteLine("Start downloading episode " + this.Number + ' ' + this.Name);
Console.WriteLine(ConsoleTag + " Starting download " + this.Number + ' ' + this.Name);
if (!YouTubeDL.Download(this.Address, this.Directory))
{
FSHelper.CleanDirectory(this.Directory);
Console.WriteLine("YoutubeDL failed for some reason.");
Console.WriteLine(ConsoleTag + " YoutubeDL failed.");
return;
}
Console.WriteLine("Finished download of episode " + this.Number);
Console.WriteLine(ConsoleTag + " Finished download");

SortDownloadedParts();

File.Create(this.Directory + "/dlfinish");
#if RELEASE
File.SetAttributes( this.Directory + "/dlfinish", File.GetAttributes( this.Directory + "/dlfinish" ) | FileAttributes.Hidden );
#endif
}

private void SortDownloadedParts()
{
/* Sort video parts and rename */
String[] files = System.IO.Directory.GetFiles(this.Directory);
ArrayList videoParts = new ArrayList();
Expand All @@ -96,18 +114,13 @@ public void Download(Boolean overwrite = false)
File.Move(_file, System.IO.Path.GetDirectoryName(_file) + "/part" + index + extension);
videoParts.Add(_file);
}

File.Create(this.Directory + "/dlfinish");
#if RELEASE
File.SetAttributes( this.Directory + "/dlfinish", File.GetAttributes( this.Directory + "/dlfinish" ) | FileAttributes.Hidden );
#endif
}

public void Merge()
{
if (!File.Exists(this.Directory + "/dlfinish"))
{
Console.WriteLine("No video files to merge!");
Console.WriteLine(ConsoleTag + " No video files to merge!");
return;
}

Expand All @@ -116,28 +129,20 @@ public void Merge()

var videoFiles = System.IO.Directory.GetFiles(this.Directory, "*.*", SearchOption.AllDirectories)
.Where(s => System.IO.Path.GetExtension(s) == this.Extension)
.OrderBy(x => Int32.Parse(x.Substring(x.IndexOf("part") + 4, 1)));

/* Output parts into files.txt for ffmpeg */
StreamWriter sw = File.CreateText(this.Directory + "/files.txt");
foreach (String filePath in videoFiles)
{
sw.Write("file '" + filePath + '\'' + sw.NewLine);
}
sw.Close();
.OrderBy(x => Int32.Parse(x.Substring(x.IndexOf("part") + 4, 1)))
.ToArray<string>();

if (!FFMpeg.Mux(this.Directory, this.FileName + this.Extension))
Console.WriteLine(ConsoleTag + " Start muxing");
if (!FFMpeg.Mux(this.Directory, videoFiles, this.FileName + this.Extension))
{
Console.WriteLine("ffmpeg failed for some reason.");
Console.WriteLine(ConsoleTag + " ffmpeg failed for some reason.");
return;
}
Console.WriteLine(ConsoleTag + " Successfully muxed episode to \"" + Path + '"');

/* Delete previous data after successfull muxing */
File.Delete(this.Directory + "/files.txt");
/* Delete video parts after successfully muxing */
foreach (String oldFile in videoFiles)
{
File.Delete(oldFile);
}

File.Create(this.Directory + "/mergefinish");
#if RELEASE
Expand All @@ -148,9 +153,11 @@ public void Merge()

public void AddMeta()
{
Console.WriteLine(ConsoleTag + " Adding meta information.");
TagLib.File file = TagLib.File.Create(this.Path); // Change file path accordingly.
file.Tag.Title = this.Name;
file.Save();
Console.WriteLine(ConsoleTag + " Meta information complete.");
}
}
}

0 comments on commit 6aefc39

Please sign in to comment.