1
+ package com .example .youtubedownloader ;
2
+
3
+ import android .annotation .SuppressLint ;
4
+ import android .app .DownloadManager ;
5
+ import android .content .Context ;
6
+ import android .net .Uri ;
7
+ import android .os .Bundle ;
8
+ import android .os .Environment ;
9
+ import android .util .SparseArray ;
10
+ import android .view .View ;
11
+ import android .widget .ImageView ;
12
+ import android .widget .TextView ;
13
+ import android .widget .Toast ;
14
+
15
+ import androidx .appcompat .app .AppCompatActivity ;
16
+ import androidx .recyclerview .widget .RecyclerView ;
17
+
18
+ import com .bumptech .glide .Glide ;
19
+ import com .google .android .material .appbar .MaterialToolbar ;
20
+ import com .google .android .material .button .MaterialButton ;
21
+ import com .google .android .material .textfield .TextInputEditText ;
22
+
23
+ import java .text .MessageFormat ;
24
+ import java .util .Objects ;
25
+
26
+ import at .huber .youtubeExtractor .VideoMeta ;
27
+ import at .huber .youtubeExtractor .YouTubeExtractor ;
28
+ import at .huber .youtubeExtractor .YtFile ;
29
+
30
+ public class MainActivity extends AppCompatActivity {
31
+
32
+ @ Override
33
+ protected void onCreate (Bundle savedInstanceState ) {
34
+ super .onCreate (savedInstanceState );
35
+ setContentView (R .layout .activity_main );
36
+
37
+ MaterialToolbar toolbar = findViewById (R .id .toolbar );
38
+ setSupportActionBar (toolbar );
39
+
40
+ MaterialButton download = findViewById (R .id .download );
41
+ TextInputEditText editText = findViewById (R .id .urlET );
42
+ ImageView imageView = findViewById (R .id .thumbnail );
43
+ TextView title = findViewById (R .id .titleTV );
44
+ TextView channelName = findViewById (R .id .channelName );
45
+ TextView videoLength = findViewById (R .id .videoLength );
46
+ TextView viewCount = findViewById (R .id .viewCount );
47
+ RecyclerView recyclerView = findViewById (R .id .recycler );
48
+
49
+ download .setOnClickListener (new View .OnClickListener () {
50
+ @ Override
51
+ @ SuppressLint ("StaticFieldLeak" )
52
+ public void onClick (View view ) {
53
+ new YouTubeExtractor (MainActivity .this ) {
54
+ @ Override
55
+ public void onExtractionComplete (SparseArray <YtFile > files , VideoMeta videoMeta ) {
56
+ if (files == null ) {
57
+ Toast .makeText (MainActivity .this , "There was an error while extracting video" , Toast .LENGTH_SHORT ).show ();
58
+ return ;
59
+ }
60
+ title .setText (MessageFormat .format ("Title: {0}" , videoMeta .getTitle ()));
61
+ channelName .setText (MessageFormat .format ("Channel Name: {0}" , videoMeta .getAuthor ()));
62
+ videoLength .setText (MessageFormat .format ("Video Duration: {0}" , getDuration (videoMeta .getVideoLength ())));
63
+ viewCount .setText (MessageFormat .format ("Views Count: {0}" , videoMeta .getViewCount ()));
64
+ Glide .with (getApplicationContext ()).load (videoMeta .getMaxResImageUrl ().replace ("http" ,"https" )).fitCenter ().into (imageView );
65
+
66
+ SparseArray <YtFile > array = new SparseArray <>();
67
+
68
+ for (int i = 0 ; i < files .size (); i ++) {
69
+ YtFile ytFile = files .get (files .keyAt (i ));
70
+
71
+ if (ytFile .getFormat ().getHeight () == -1 || ytFile .getFormat ().getHeight () >= 360 ) {
72
+ array .put (files .keyAt (i ), ytFile );
73
+ }
74
+ }
75
+
76
+ ResultAdapter adapter = new ResultAdapter (MainActivity .this , array );
77
+ recyclerView .setAdapter (adapter );
78
+
79
+ adapter .setOnItemClickListener (new ResultAdapter .OnItemClickListener () {
80
+ @ Override
81
+ public void onClick (YtFile ytFile ) {
82
+ String filename = videoMeta .getTitle () + "." + ytFile .getFormat ().getExt ();
83
+ filename = filename .replaceAll ("[\\ \\ ><\" |*?%:#/]" , "" );
84
+ download (ytFile .getUrl (), videoMeta .getTitle (), filename );
85
+ }
86
+ });
87
+ }
88
+ }.extract (Objects .requireNonNull (editText .getText ()).toString ());
89
+ }
90
+ });
91
+ }
92
+
93
+ public String getDuration (long duration ) {
94
+ final int MINUTES_IN_AN_HOUR = 60 ;
95
+ final int SECONDS_IN_A_MINUTE = 60 ;
96
+
97
+ int seconds = (int ) (duration % SECONDS_IN_A_MINUTE );
98
+ int totalMinutes = (int ) (duration / SECONDS_IN_A_MINUTE );
99
+ int minutes = totalMinutes % MINUTES_IN_AN_HOUR ;
100
+ int hours = totalMinutes / MINUTES_IN_AN_HOUR ;
101
+
102
+ return hours + ":" + minutes + ":" + seconds ;
103
+ }
104
+
105
+ private void download (String youtubeDlUrl , String downloadTitle , String fileName ) {
106
+ Uri uri = Uri .parse (youtubeDlUrl );
107
+ DownloadManager .Request request = new DownloadManager .Request (uri );
108
+ request .setTitle (downloadTitle );
109
+
110
+ request .allowScanningByMediaScanner ();
111
+ request .setNotificationVisibility (DownloadManager .Request .VISIBILITY_VISIBLE_NOTIFY_COMPLETED );
112
+ request .setDestinationInExternalPublicDir (Environment .DIRECTORY_DOWNLOADS , fileName );
113
+
114
+ DownloadManager manager = (DownloadManager ) getSystemService (Context .DOWNLOAD_SERVICE );
115
+ manager .enqueue (request );
116
+ Toast .makeText (this , "Downloading..." , Toast .LENGTH_SHORT ).show ();
117
+ }
118
+ }
0 commit comments