@@ -91,6 +91,49 @@ static void m3u8ds_free(struct M3U8DownloadedStreams* const streams) {
91
91
92
92
}
93
93
94
+ const struct M3U8StreamItem * m3u8stream_finditem (const struct M3U8Stream * const root , const struct M3U8Stream * const resource ) {
95
+ /*
96
+ Look for a M3U8 item that matches the given M3U8 stream.
97
+ */
98
+
99
+ size_t index = 0 ;
100
+
101
+ if (root -> playlist .type != M3U8_PLAYLIST_TYPE_MASTER ) {
102
+ return NULL ;
103
+ }
104
+
105
+ for (index = 0 ; index < root -> offset ; index ++ ) {
106
+ const struct M3U8StreamItem * const item = & root -> items [index ];
107
+
108
+ switch (item -> type ) {
109
+ case M3U8_STREAM_VARIANT_STREAM : {
110
+ const struct M3U8VariantStream * const variant_stream = item -> item ;
111
+
112
+ if (& variant_stream -> stream != resource ) {
113
+ break ;
114
+ }
115
+
116
+ return item ;
117
+ }
118
+ case M3U8_STREAM_MEDIA : {
119
+ const struct M3U8Media * const media = item -> item ;
120
+
121
+ if (& media -> stream != resource ) {
122
+ break ;
123
+ }
124
+
125
+ return item ;
126
+ }
127
+ default : {
128
+ break ;
129
+ }
130
+ }
131
+ }
132
+
133
+ return NULL ;
134
+
135
+ }
136
+
94
137
int main (int argc , argv_t * argv []) {
95
138
96
139
int err = M3U8ERR_SUCCESS ;
@@ -116,6 +159,7 @@ int main(int argc, argv_t* argv[]) {
116
159
int referer = 0 ;
117
160
int insecure = 0 ;
118
161
int debug = 0 ;
162
+ int disable_autoselect = 0 ;
119
163
120
164
int select_all_medias = 0 ;
121
165
int exists = 0 ;
@@ -744,6 +788,14 @@ int main(int argc, argv_t* argv[]) {
744
788
goto end ;
745
789
}
746
790
}
791
+
792
+ /*
793
+ No media have been explicitly selected; let's select the ones attached to
794
+ that variant stream.
795
+ */
796
+ if (selected_medias .offset == 0 && !disable_autoselect ) {
797
+
798
+ }
747
799
}
748
800
749
801
break ;
@@ -762,28 +814,28 @@ int main(int argc, argv_t* argv[]) {
762
814
if (key != NULL ) {
763
815
for (index = 0 ; index < stream .offset ; index ++ ) {
764
816
struct M3U8StreamItem * const item = & stream .items [index ];
765
- struct M3U8Stream * substream = NULL ;
817
+ struct M3U8Stream * resource = NULL ;
766
818
767
819
switch (item -> type ) {
768
820
case M3U8_STREAM_VARIANT_STREAM : {
769
- substream = & ((struct M3U8VariantStream * ) item -> item )-> stream ;
821
+ resource = & ((struct M3U8VariantStream * ) item -> item )-> stream ;
770
822
break ;
771
823
}
772
824
case M3U8_STREAM_MEDIA : {
773
- substream = & ((struct M3U8Media * ) item -> item )-> stream ;
825
+ resource = & ((struct M3U8Media * ) item -> item )-> stream ;
774
826
break ;
775
827
}
776
828
default : {
777
829
break ;
778
830
}
779
831
}
780
832
781
- if (substream == NULL ) {
833
+ if (resource == NULL ) {
782
834
continue ;
783
835
}
784
836
785
- for (subindex = 0 ; subindex < substream -> offset ; subindex ++ ) {
786
- struct M3U8StreamItem * const subitem = & substream -> items [subindex ];
837
+ for (subindex = 0 ; subindex < resource -> offset ; subindex ++ ) {
838
+ struct M3U8StreamItem * const subitem = & resource -> items [subindex ];
787
839
struct M3U8Segment * const segment = subitem -> item ;
788
840
struct M3U8Attribute * attribute = NULL ;
789
841
@@ -816,8 +868,10 @@ int main(int argc, argv_t* argv[]) {
816
868
}
817
869
818
870
for (index = 0 ; index < selected_streams .offset ; index ++ ) {
819
- struct M3U8Stream * const substream = selected_streams .items [index ];
820
- name = malloc (strlen (temporary_directory ) + strlen (PATHSEP ) + uintlen ((biguint_t ) substream ) + 1 + 4 + 1 );
871
+ const struct M3U8StreamItem * item = NULL ;
872
+
873
+ struct M3U8Stream * const resource = selected_streams .items [index ];
874
+ name = malloc (strlen (temporary_directory ) + strlen (PATHSEP ) + uintlen ((biguint_t ) resource ) + 1 + 4 + 1 );
821
875
822
876
if (name == NULL ) {
823
877
err = M3U8ERR_MEMORY_ALLOCATE_FAILURE ;
@@ -827,7 +881,7 @@ int main(int argc, argv_t* argv[]) {
827
881
strcpy (name , temporary_directory );
828
882
strcat (name , PATHSEP );
829
883
830
- wsize = snprintf (name + strlen (name ), 4096 , "%" FORMAT_BIGGEST_UINT_T , (biguint_t ) substream );
884
+ wsize = snprintf (name + strlen (name ), 4096 , "%" FORMAT_BIGGEST_UINT_T , (biguint_t ) resource );
831
885
832
886
if (wsize < 1 ) {
833
887
err = M3U8ERR_PRINTF_WRITE_FAILURE ;
@@ -838,58 +892,35 @@ int main(int argc, argv_t* argv[]) {
838
892
839
893
printf ("- Download Stream #%zu\n" , index );
840
894
841
- switch (stream .playlist .type ) {
842
- case M3U8_PLAYLIST_TYPE_MASTER : {
843
-
844
- for (subindex = 0 ; subindex < stream .offset ; subindex ++ ) {
845
- const struct M3U8StreamItem * const item = & stream .items [subindex ];
846
-
847
- switch (item -> type ) {
848
- case M3U8_STREAM_VARIANT_STREAM : {
849
- const struct M3U8VariantStream * const variant_stream = item -> item ;
850
-
851
- if (& variant_stream -> stream != substream ) {
852
- break ;
853
- }
854
-
855
- show_variant_stream (item );
856
-
857
- break ;
858
- }
859
- case M3U8_STREAM_MEDIA : {
860
- const struct M3U8Media * const media = item -> item ;
861
-
862
- if (& media -> stream != substream ) {
863
- break ;
864
- }
865
-
866
- show_media (item );
867
-
868
- break ;
869
- }
870
- default : {
871
- break ;
872
- }
873
- }
895
+ item = m3u8stream_finditem (& stream , resource );
896
+
897
+ if (item == NULL ) {
898
+ show_media_playlist_metadata (resource );
899
+ } else {
900
+ switch (item -> type ) {
901
+ case M3U8_STREAM_VARIANT_STREAM : {
902
+ show_variant_stream (item );
903
+ break ;
904
+ }
905
+ case M3U8_STREAM_MEDIA : {
906
+ show_media (item );
907
+ break ;
908
+ }
909
+ default : {
910
+ break ;
874
911
}
875
-
876
- break ;
877
- }
878
- case M3U8_PLAYLIST_TYPE_MEDIA : {
879
- show_media_playlist_metadata (substream );
880
- break ;
881
912
}
882
913
}
883
914
884
- err = m3u8stream_download (& stream , substream , & download_options );
915
+ err = m3u8stream_download (& stream , resource , & download_options );
885
916
886
917
if (err != M3U8ERR_SUCCESS ) {
887
918
goto end ;
888
919
}
889
920
890
921
printf ("- Dumping Media Playlist to '%s'\n\n" , name );
891
922
892
- err = m3u8_dump_file (& substream -> playlist , name );
923
+ err = m3u8_dump_file (& resource -> playlist , name );
893
924
894
925
if (err != M3U8ERR_SUCCESS ) {
895
926
goto end ;
0 commit comments