forked from yausername/youtubedl-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadingExampleActivity.java
212 lines (183 loc) · 7.96 KB
/
DownloadingExampleActivity.java
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
package com.yausername.youtubedl_android_example;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.yausername.youtubedl_android.YoutubeDL;
import com.yausername.youtubedl_android.YoutubeDLRequest;
import java.io.File;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.functions.Function3;
public class DownloadingExampleActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnStartDownload;
private Button btnStopDownload;
private EditText etUrl;
private Switch useConfigFile;
private ProgressBar progressBar;
private TextView tvDownloadStatus;
private TextView tvCommandOutput;
private ProgressBar pbLoading;
private boolean downloading = false;
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
private String processIdFacebook = "Myfacebookprocess";
private String processIdCbc = "MyCbcprocess";
private final Function3<Float, Long, String, Unit> callback = new Function3<Float, Long, String, Unit>() {
@Override
public Unit invoke(Float progress, Long o2, String line) {
runOnUiThread(() -> {
progressBar.setProgress((int) progress.floatValue());
tvDownloadStatus.setText(line);
}
);
return Unit.INSTANCE;
}
};
Function2<Integer,String, Unit> progressCallback = new Function2<Integer,String,Unit>() {
@Override
public Unit invoke(Integer size,String line) {
// Your implementation of the progressCallback function
Log.e(TAG,"FFMPEG size: "+line);
return null;
}
};
// Define the onComplete function
private static final String TAG = DownloadingExampleActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_downloading_example);
initViews();
initListeners();
}
private void initViews() {
btnStartDownload = findViewById(R.id.btn_start_download);
btnStopDownload = findViewById(R.id.btn_stop_download);
etUrl = findViewById(R.id.et_url);
etUrl.setText("https://www.cbsnews.com/video/a-nation-in-transition-cbs-reports");
useConfigFile = findViewById(R.id.use_config_file);
progressBar = findViewById(R.id.progress_bar);
tvDownloadStatus = findViewById(R.id.tv_status);
pbLoading = findViewById(R.id.pb_status);
tvCommandOutput = findViewById(R.id.tv_command_output);
}
private void initListeners() {
btnStartDownload.setOnClickListener(this);
btnStopDownload.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start_download:
String cbcurl = "https://baijiahao.baidu.com/s?id=1783258486309390969"; //"https://www.cbsnews.com/video/a-nation-in-transition-cbs-reports";
startDownload(cbcurl,processIdCbc);
//String facebookurl = "https://www.facebook.com/peopleareawesome/videos/best-videos-of-the-year-so-far/1393626100686564/";
//startDownload(facebookurl,processIdFacebook);
break;
case R.id.btn_stop_download:
try {
YoutubeDL.getInstance().destroyProcessById(processIdCbc);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
break;
}
}
private void startDownload(String url,String processId) {
/*if (downloading) {
Toast.makeText(DownloadingExampleActivity.this, "cannot start download. a download is already in progress", Toast.LENGTH_LONG).show();
return;F
}*/
if (!isStoragePermissionGranted()) {
Toast.makeText(DownloadingExampleActivity.this, "grant storage permission and retry", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(url)) {
etUrl.setError(getString(R.string.url_error));
return;
}
YoutubeDLRequest request = new YoutubeDLRequest(url);
File youtubeDLDir = getDownloadLocation();
File config = new File(youtubeDLDir, "config.txt");
if (useConfigFile.isChecked() && config.exists()) {
request.addOption("--config-location", config.getAbsolutePath());
} else {
request.addOption("--no-mtime");
request.addOption("--downloader", "ffmpeg");
//request.addOption("-f", "bestvideo+bestaudio");
request.addOption("-o", youtubeDLDir.getAbsolutePath() + "/%(title)s.%(ext)s");
}
showStart();
downloading = true;
Disposable disposable = Observable.fromCallable(() ->
YoutubeDL.getInstance().execute(request, processId, callback, progressCallback)
)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(youtubeDLResponse -> {
pbLoading.setVisibility(View.GONE);
progressBar.setProgress(100);
tvDownloadStatus.setText(getString(R.string.download_complete));
tvCommandOutput.setText(youtubeDLResponse.getOut());
Toast.makeText(DownloadingExampleActivity.this, "download successful", Toast.LENGTH_LONG).show();
downloading = false;
}, e -> {
if (BuildConfig.DEBUG) Log.e(TAG, "failed to download "+e.getMessage());
pbLoading.setVisibility(View.GONE);
tvDownloadStatus.setText(getString(R.string.download_failed));
tvCommandOutput.setText(e.getMessage());
Toast.makeText(DownloadingExampleActivity.this, "download failed", Toast.LENGTH_LONG).show();
downloading = false;
});
compositeDisposable.add(disposable);
}
@Override
protected void onDestroy() {
compositeDisposable.dispose();
super.onDestroy();
}
@NonNull
private File getDownloadLocation() {
File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File youtubeDLDir = new File(downloadsDir, "youtubedl-android");
if (!youtubeDLDir.exists()) youtubeDLDir.mkdir();
return youtubeDLDir;
}
private void showStart() {
tvDownloadStatus.setText(getString(R.string.download_start));
progressBar.setProgress(0);
pbLoading.setVisibility(View.VISIBLE);
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
} else {
return true;
}
}
}