-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecorderHelper.java
More file actions
525 lines (385 loc) · 13.6 KB
/
RecorderHelper.java
File metadata and controls
525 lines (385 loc) · 13.6 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.CharBuffer;
import java.security.CodeSource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.Scanner;
public class RecorderHelper {
private static String fileSeparator = File.separator;
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_RESET = "\u001B[0m";
private static String timeFrom = "";
private static String timeTo = "";
private static String name = "";
private static String url = "";
private static String code = "";
private static int lengthAccepted = 160;
private static String jarDir = "";
private static String errorMessage = "";
private static String OS = System.getProperty("os.name").toLowerCase();
public static String getRunningDir(String myFileName){
CodeSource codeSource = StartRecorder.class.getProtectionDomain().getCodeSource();
try {
File jarFile = new File(codeSource.getLocation().toURI().getPath());
jarDir = jarFile.getParentFile().getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
System.exit(1);
}
File f = new File(jarDir + fileSeparator + myFileName);
if (!f.exists()) {
System.out.println("Could not find config.properties, this program tried to find the file in this location:");
System.out.println(f.getPath());
System.out.println("Closing the application!");
System.exit(1);
}
return jarDir;
}
//Laddar properties från config.properties
public Properties readPropertiesFile(String fileName) throws IOException {
FileInputStream fis = null;
Properties prop = null;
try {
fis = new FileInputStream(fileName);
prop = new Properties();
prop.load(fis);
} catch(FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
} finally {
fis.close();
}
return prop;
}
public boolean loadChannels(ArrayList<M3UHolder> myChannels, boolean restarted, String text){
clearScreen();
Iterator<M3UHolder> itr = myChannels.iterator();
int counter = 0;
while(itr.hasNext()) {
M3UHolder mH = new M3UHolder();
mH = itr.next();
name = "Channel name: " + mH.getName().trim();
code = "Channel code: " + mH.getCode().trim();
int totalOfBoth = name.length() + code.length();
int lengthOfSpaces = lengthAccepted - totalOfBoth;
if (code.length() == 18) {
lengthOfSpaces = lengthOfSpaces -1;
}
String space = createSpaces(lengthOfSpaces);
int lengthTotal = name.length() + space.length() + code.length();
String underLine = createUnderLine(lengthTotal);
System.out.println(name + space + code);
System.out.println(underLine);
counter++;
if (counter==20) {
if (waitForChannelInput(myChannels, restarted, text)) {
break;
}else {
if (getErrorMessage()!="") {
return false;
}
}
counter=0;
clearScreen();
}
}
return true;
}
public boolean waitForChannelInput(ArrayList<M3UHolder> myChannels, boolean restarted, String text) {
String input = "";
if (getErrorMessage()!="") {
printErrorMessage(getErrorMessage());
setErrorMessage("");
}
System.out.println("\nPlease choose a channel code you want to record from the list or \"ENTER\" to continue: ");
boolean done = false;
while(true) {
input = readInput();
if (input == "" || input.length()==0) {
return false;
}
if(isNumeric(input)) {
Iterator<M3UHolder> itr = myChannels.iterator();
while(itr.hasNext()) {
M3UHolder mH = new M3UHolder();
mH = itr.next();
if (mH.getCode().equalsIgnoreCase(input)) {
url = mH.getUrl();
code = mH.getCode();
name = mH.getName();
done = true;
}
}
}else {
setErrorMessage( "\nYou have entered a channel code that is not numeric, reloading channel list!");
return false;
}
if(done) {
break;
}else {
setErrorMessage("\nThe channel that you have entered does not exist, reloading channel list");
return false;
}
}
return true;
}
public String waitForTimeInput(String message, boolean startTime) {
clearScreen();
String input = "";
System.out.println(message);
while(true) {
input = readInput();
if (correctTimeSyntax(input, startTime)) {
return input;
}else {
clearScreen();
printErrorMessage("You have entered wrong time format, should be HH:MM, please try again: \n");
}
}
}
public boolean correctTimeSyntax(String time, boolean startTime) {
try {
LocalTime.parse(time);
if (startTime) {
timeFrom = time;
}else {
timeTo = time;
}
return true;
} catch (DateTimeParseException | NullPointerException e) {
return false;
}
}
public boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
@SuppressWarnings("unused")
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
public static void clearScreen(){
if (OS.contains("win")) {
myRunnable("cls", OS);
}else {
System.out.print("\033\143");
}
}
private static String createSpaces( int spaces ) {
return CharBuffer.allocate( spaces ).toString().replace( '\0', ' ' );
}
private static String createUnderLine( int lines ) {
return CharBuffer.allocate( lines ).toString().replace( '\0', '_' );
}
public void startCounter() {
String currentTime = new SimpleDateFormat("HH:mm").format(new Date());
while(true) {
if (currentTime.trim().equalsIgnoreCase(timeFrom.trim())){
break;
}
waitingForStartDisplay("Waiting for recording to start ... ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//
}
currentTime = new SimpleDateFormat("HH:mm").format(new Date());
}
}
public void endCounter() {
String currentTime = new SimpleDateFormat("HH:mm").format(new Date());
while(true) {
if (currentTime.trim().equalsIgnoreCase(timeTo.trim())){
break;
}
waitingForEndDisplay("Recording has started, waiting for recording to end ... ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//
}
currentTime = new SimpleDateFormat("HH:mm").format(new Date());
}
}
public void waitingForStartDisplay(String text) {
clearScreen();
String displayTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
displayTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
System.out.println("\nRecording channel: " + name.trim());
System.out.println("Recording code: " + code.trim());
System.out.println("Recording startime: " + timeFrom.trim());
System.out.println("Recording stoptime: " + timeTo.trim());
System.out.println("URL of channel: " + url.trim() + "\n");
printWaitingMessage("\n" + text + displayTime);
}
public void waitingForEndDisplay(String text) {
clearScreen();
String displayTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
displayTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
System.out.println("\nRecording channel: " + name.trim());
System.out.println("Recording code: " + code.trim());
System.out.println("Recording startime: " + timeFrom.trim());
System.out.println("Recording stoptime: " + timeTo.trim());
System.out.println("URL of channel: " + url.trim() + "\n");
printOkMessage("\n" + text + displayTime);
}
public void startRecFFMPEG(String filePath) throws InterruptedException {
String startString = "ffmpeg -hide_banner -loglevel panic -i " + url + " -c copy " + createFileName(filePath);
new Thread(new Runnable() {
@Override
public void run() {
myRunnable(startString, OS);
}
}).start();
}
public void startRecRegular(String filePath) {
new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream input = new URL(url).openStream();
FileOutputStream outputStream = new FileOutputStream(new File(createFileName(filePath)));
int read;
byte[] bytes = new byte[8096];
while ((read = input.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
String currentTime = new SimpleDateFormat("HH:mm").format(new Date());
if (currentTime.trim().equalsIgnoreCase(timeTo.trim())){
outputStream.close();
input.close();
break;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
printErrorMessage("Could not open the stream, and therefor an FileNotFoundException occured, exiting program!");
System.exit(1);
}
}
}).start();
}
public static String createFileName(String filePath) {
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
LocalDateTime now = LocalDateTime.now();
int hour = now.getHour();
int minute = now.getMinute();
String h = "";
String m = "";
if (hour<10) {
h = "0" + hour;
}else {
h = String.valueOf(hour);
}
if (minute < 10) {
m = "0" + m;
if (m.equalsIgnoreCase("0")) {
m = "00";
}
}else {
m = String.valueOf(minute);
}
String displayTime = date + "-" + h + m;
String fileName = filePath + fileSeparator + "MyRecording-" + displayTime + ".ts";
return fileName;
}
public void stopRecording() {
String stopString = "kill -9 $(ps -ef | grep -v grep | grep ffmpeg | awk '{print $2}')";
new Thread(new Runnable() {
@Override
public void run() {
myRunnable(stopString, OS);
}
}).start();
}
private static void myRunnable(String command, String OS) {
if (OS.contains("win")) {
try {
new ProcessBuilder("cmd", "/c", command).inheritIO().start().waitFor();
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}else {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("bash", "-c", command);
try {
Process process = processBuilder.start();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void getM3UFile(String myUrl, String path, String m3uName) {
try {
InputStream input = new URL(myUrl).openStream();
FileOutputStream outputStream = new FileOutputStream(new File(path + fileSeparator + m3uName));
int read;
byte[] bytes = new byte[8096];
while ((read = input.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
input.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String readInput() {
String input = "";
@SuppressWarnings("resource")
Scanner userInput = new Scanner(System.in);
input = userInput.nextLine();
if (input=="" || input.length()==0) {
return "";
}else {
return input.trim();
}
}
public void endMessage() {
String currentTime = new SimpleDateFormat("HH:mm").format(new Date());
printOkMessage("\nRecording ended successfully: " + currentTime);
}
public static String getErrorMessage() {
return errorMessage;
}
public static void setErrorMessage(String errorMessage) {
RecorderHelper.errorMessage = errorMessage;
}
public static void printErrorMessage(String message) {
System.out.println(ANSI_RED + message + ANSI_RESET);
}
public static void printOkMessage(String message) {
System.out.println(ANSI_GREEN + message + ANSI_RESET);
}
public static void printWaitingMessage(String message) {
System.out.println(ANSI_YELLOW + message + ANSI_RESET);
}
}