6
6
using Microsoft . WindowsAPICodePack . Taskbar ;
7
7
using Timer = System . Windows . Forms . Timer ;
8
8
using static WebMConverter . Utility ;
9
+ using System . Linq ;
9
10
10
11
namespace WebMConverter . Dialogs
11
12
{
12
13
public partial class DownloadDialog : Form
13
14
{
14
15
public string Outfile ;
16
+ public string OutputPath ;
15
17
16
18
private readonly string _infile ;
17
- private YoutubeDL _filenameGetterProcess ;
18
19
private YoutubeDL _downloaderProcess ;
19
20
20
21
private Timer _timer ;
21
22
private bool _ended ;
22
23
private bool _panic ;
23
- private bool _gettingFilename ;
24
24
25
25
private TaskbarManager taskbarManager ;
26
26
27
- public DownloadDialog ( string url )
27
+ public DownloadDialog ( string url , string outputPath )
28
28
{
29
29
InitializeComponent ( ) ;
30
30
pictureStatus . BackgroundImage = StatusImages . Images [ "Happening" ] ;
31
31
32
32
_infile = '"' + url . Replace ( @"""" , @"\""" ) + '"' ;
33
- Outfile = Path . GetTempPath ( ) ;
33
+ OutputPath = outputPath ;
34
34
35
35
taskbarManager = TaskbarManager . Instance ;
36
36
}
@@ -41,26 +41,32 @@ private void ProcessOnErrorDataReceived(object sender, DataReceivedEventArgs arg
41
41
boxOutput . Invoke ( ( Action ) ( ( ) => boxOutput . AppendText ( Environment . NewLine + args . Data ) ) ) ;
42
42
}
43
43
44
- private void FilenameGetterOnOutputDataReceived ( object sender , DataReceivedEventArgs args )
45
- {
46
- if ( args . Data != null )
47
- Outfile = Path . Combine ( Outfile , args . Data ) ;
48
- }
49
-
50
44
private void ProcessOnOutputDataReceived ( object sender , DataReceivedEventArgs args )
51
45
{
52
46
if ( args . Data != null )
53
47
{
54
48
boxOutput . Invoke ( ( Action ) ( ( ) => boxOutput . AppendText ( Environment . NewLine + args . Data ) ) ) ;
55
49
56
50
if ( DataContainsProgress ( args . Data ) )
51
+ {
57
52
ParseAndUpdateProgress ( args . Data ) ;
53
+ if ( String . IsNullOrEmpty ( Outfile ) )
54
+ GetId ( args . Data ) ;
55
+ }
56
+
58
57
}
59
58
}
60
59
60
+ private void GetId ( string data )
61
+ {
62
+ if ( data . Contains ( "Destination:" ) )
63
+ Outfile = data . Split ( '-' ) . LastOrDefault ( ) . Split ( '.' ) [ 0 ] ;
64
+ }
65
+
61
66
// example youtube-dl line:
62
67
// [download] 51.5% of ~3.85MiB at 20.49MiB/s ETA 00:00
63
68
69
+ private bool DataContainsFFMPEG ( string data ) => data . StartsWith ( "[ffmpeg]" ) ;
64
70
private bool DataContainsProgress ( string data ) => data . StartsWith ( "[download]" ) ;
65
71
66
72
private void ParseAndUpdateProgress ( string input )
@@ -80,29 +86,18 @@ private void ParseAndUpdateProgress(string input)
80
86
81
87
private void DownloadDialog_Load ( object sender , EventArgs e )
82
88
{
83
- _filenameGetterProcess = new YoutubeDL ( "--get-filename " + _infile ) ;
89
+ boxOutput . AppendText ( $ " { Environment . NewLine } Starting Process" ) ;
84
90
_downloaderProcess = new YoutubeDL ( null ) ;
85
91
86
- _gettingFilename = true ;
87
-
88
- _filenameGetterProcess . ErrorDataReceived += ProcessOnErrorDataReceived ;
89
- _filenameGetterProcess . OutputDataReceived += FilenameGetterOnOutputDataReceived ;
90
- _filenameGetterProcess . Exited += ( o , args ) => boxOutput . Invoke ( ( Action ) ( ( ) =>
91
- {
92
- if ( _panic ) return ;
93
- boxOutput . AppendText ( $ "{ Environment . NewLine } Downloading to { Outfile } ") ;
94
- _downloaderProcess . StartInfo . Arguments = $@ "-o ""{ Outfile } "" { _infile } ";
95
-
96
- _timer = new Timer { Interval = 500 } ;
97
- _timer . Tick += Exited ;
98
- _timer . Start ( ) ;
99
- } ) ) ;
92
+ if ( _infile . Contains ( "youtube" ) )
93
+ _downloaderProcess . StartInfo . Arguments = $@ "-f bestvideo+bestaudio { _infile } ";
94
+ else
95
+ _downloaderProcess . StartInfo . Arguments = $@ " { _infile } ";
100
96
101
97
_downloaderProcess . ErrorDataReceived += ProcessOnErrorDataReceived ;
102
98
_downloaderProcess . OutputDataReceived += ProcessOnOutputDataReceived ;
103
99
_downloaderProcess . Exited += ( o , args ) => boxOutput . Invoke ( ( Action ) ( ( ) =>
104
100
{
105
- if ( _panic ) return ; //This should stop that one exception when closing the converter
106
101
boxOutput . AppendText ( $ "{ Environment . NewLine } --- YOUTUBE-DL HAS EXITED ---") ;
107
102
buttonCancel . Enabled = false ;
108
103
@@ -111,35 +106,15 @@ private void DownloadDialog_Load(object sender, EventArgs e)
111
106
_timer . Start ( ) ;
112
107
} ) ) ;
113
108
114
- taskbarManager . SetProgressState ( TaskbarProgressBarState . Indeterminate ) ; // can't get progress for filename getter
115
- progressBar . Style = ProgressBarStyle . Marquee ;
116
- _filenameGetterProcess . Start ( ) ;
109
+ taskbarManager . SetProgressState ( TaskbarProgressBarState . Normal ) ;
110
+ progressBar . Style = ProgressBarStyle . Blocks ;
111
+ _downloaderProcess . Start ( ) ;
117
112
}
118
113
119
114
private void Exited ( object sender , EventArgs eventArgs )
120
115
{
121
116
_timer . Stop ( ) ;
122
117
123
- if ( _gettingFilename )
124
- {
125
- if ( _filenameGetterProcess . ExitCode != 0 )
126
- {
127
- boxOutput . AppendText ( $ "{ Environment . NewLine } { Environment . NewLine } youtube-dl.exe exited with exit code { _filenameGetterProcess . ExitCode } . That's usually bad.") ;
128
- boxOutput . AppendText ( $ "{ Environment . NewLine } If you have no idea what went wrong, open an issue on GitGud and copy paste the output of this window there.") ;
129
- pictureStatus . BackgroundImage = StatusImages . Images [ "Failure" ] ;
130
- buttonCancel . Enabled = true ;
131
- _ended = true ;
132
- }
133
- else
134
- {
135
- _gettingFilename = false ;
136
- taskbarManager . SetProgressState ( TaskbarProgressBarState . Normal ) ;
137
- progressBar . Style = ProgressBarStyle . Blocks ;
138
- _downloaderProcess . Start ( ) ;
139
- }
140
- return ;
141
- }
142
-
143
118
if ( _downloaderProcess . ExitCode != 0 )
144
119
{
145
120
boxOutput . AppendText ( $ "{ Environment . NewLine } { Environment . NewLine } youtube-dl.exe exited with exit code { _downloaderProcess . ExitCode } . That's usually bad.") ;
@@ -153,27 +128,45 @@ private void Exited(object sender, EventArgs eventArgs)
153
128
boxOutput . AppendText ( $ "{ Environment . NewLine } { Environment . NewLine } Video downloaded succesfully!") ;
154
129
pictureStatus . BackgroundImage = StatusImages . Images [ "Success" ] ;
155
130
buttonLoad . Enabled = true ;
131
+ buttonCancel . Enabled = true ;
132
+ buttonCancel . Text = "Close" ;
133
+ MoveNewFile ( ) ;
134
+ this . Activate ( ) ;
135
+ }
136
+
137
+ _ended = true ;
138
+ }
139
+
140
+ private void MoveNewFile ( )
141
+ {
142
+ if ( String . IsNullOrEmpty ( Outfile ) )
143
+ return ;
156
144
157
- // workaround for https://github.com/rg3/youtube-dl/issues/11472
158
- if ( ! File . Exists ( Outfile ) )
145
+ string [ ] fileEntries = Directory . GetFiles ( "." ) ;
146
+ foreach ( string fileName in fileEntries )
147
+ {
148
+ if ( fileName . Contains ( Outfile ) )
159
149
{
160
- Outfile = Path . ChangeExtension ( Outfile , "mkv" ) ;
150
+ Outfile = fileName ;
151
+ break ;
161
152
}
162
153
}
163
-
164
- _ended = true ;
154
+ File . Move ( Outfile , Path . Combine ( OutputPath , Outfile ) ) ;
165
155
}
166
156
167
157
private void buttonCancel_Click ( object sender , EventArgs e )
168
158
{
169
- if ( ! _ended || _panic ) //Prevent stack overflow
159
+ if ( buttonCancel . Text . Equals ( "Close" ) )
170
160
{
171
- if ( ! _filenameGetterProcess . HasExited )
172
- KillProcessAndChildren ( _filenameGetterProcess . Id ) ;
161
+ if ( ! _downloaderProcess . HasExited )
162
+ KillProcessAndChildren ( _downloaderProcess . Id ) ;
173
163
174
- if ( _gettingFilename )
175
- return ;
164
+ Dispose ( ) ;
165
+ return ;
166
+ }
176
167
168
+ if ( ! _ended || _panic ) //Prevent stack overflow
169
+ {
177
170
if ( ! _downloaderProcess . HasExited )
178
171
KillProcessAndChildren ( _downloaderProcess . Id ) ;
179
172
}
@@ -190,7 +183,6 @@ private void ConverterForm_FormClosing(object sender, FormClosingEventArgs e)
190
183
191
184
private void ConverterForm_FormClosed ( object sender , FormClosedEventArgs e )
192
185
{
193
- _filenameGetterProcess . Dispose ( ) ;
194
186
_downloaderProcess . Dispose ( ) ;
195
187
}
196
188
@@ -201,5 +193,13 @@ private void buttonLoad_Click(object sender, EventArgs e)
201
193
}
202
194
203
195
private void boxOutput_TextChanged ( object sender , EventArgs e ) => NativeMethods . SendMessage ( boxOutput . Handle , 0x115 , 7 , 0 ) ;
196
+
197
+ internal string GetOutfile ( )
198
+ {
199
+ if ( String . IsNullOrEmpty ( Outfile ) )
200
+ return String . Empty ;
201
+
202
+ return OutputPath + Outfile . Substring ( 1 ) ;
203
+ }
204
204
}
205
205
}
0 commit comments