Android Question Exoplayer Youtube Dash

Syd Wright

Well-Known Member
Licensed User
a simple way is connect saveitoffline api https://www.saveitoffline.com/api/
get video url from json or xml then play with exoplayer
I tried this with the B4A Youtube video: ....//www.youtube.com/watch?v=cgxWP4Wu77s
but no usable streams. I can download the MP4 with one of the found links: https://www.saveitoffline.com/get/?...aHnAGjY0ns&u=d5eaRrcHKaVgmRfXXp9Z1FSp73GgtX8T but cannot use it to instantly stream the video. How to find the ...mp4 URL?

Kindly show us how to do this with saveitoffline. Thank you!
 
Last edited:
Upvote 0

Syd Wright

Well-Known Member
Licensed User
Update: Here is my test project to find and play Youtube video's, using the saveitoffline API:
It works fine! The only thing is that the download of the video needs to be completed before it can be played.
Maybe someone knows a faster (asyncstream) solution?

B4X:
#Region  Project Attributes
    #ApplicationLabel: DSH YOUTUBE PLAYER
    #FullScreen: True
    #IncludeTitle: False
    #VersionCode: 1
    #VersionName: 1.0
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim Job1 As HttpJob
    Dim Job2 As HttpJob
    Dim SourceText1 As String
    Dim Timer1 As Timer
    Dim YouDown1 As String
    Dim YouLink1 As String
End Sub

Sub Globals
    Dim VV1 As VideoView
    Dim LayoutVal As LayoutValues
End Sub

Sub Activity_Create(FirstTime As Boolean)
    VV1.Initialize("VV1")
    Activity.AddView(VV1,0,0,100%x,100%y)
    Timer1.Initialize("Timer1",100)
    Timer1.Enabled = False
    LayoutVal = GetDeviceLayoutValues
    YouDown1 = "https://www.youtube.com/watch?v=cgxWP4Wu77s"
    YouDown1 = "https://www.saveitoffline.com/process/?url=" & YouDown1 & "&type=xml"
 
    Job1.Initialize("Job1", Me)
    Job1.Download(YouDown1)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JobDone(job As HttpJob)
    Dim n, m As Long
    If job.Success = True Then
      Select job.JobName
        Case "Job1"                         'Get Link for 360p video
          SourceText1 = Job1.GetString     
          n=SourceText1.indexof("<label>360p - mp4")
          If n=-1 Then Return
          n=SourceText1.indexof2("<url>",n+15)
          If n=-1 Then Return
          n=n+5
          m=SourceText1.indexof2("</url>",n+5)
          If m=-1 Or m<n Then Return
          YouLink1 = SourceText1.SubString2(n,m)
          Job2.Initialize("Job2", Me)
          Job2.Download(YouLink1)
          
        Case "Job2"                         'Download MP4
          Dim out As OutputStream
          If File.Exists(File.DirRootExternal,"youvideo1.mp4") Then File.Delete(File.DirRootExternal,"youvideo1.mp4")
          out = File.OpenOutput(File.DirRootExternal,"youvideo1.mp4",False )
          File.Copy2(job.GetInputStream, out)
          out.Close        'Directly starting to play the MP4 outputstream in stead of downloading it might be possible??
      
          Timer1.enabled = True
          VV1.LoadVideo(File.DirRootExternal,"youvideo1.mp4")
          VV1.Play

        End Select
        job.Release
     Else
         Log(job.ErrorMessage)
     End If
End Sub

Sub Timer1_tick                        'Used to center VV1, depending on the actual video width!
    Dim VV1width1 As Long
    Dim VV1height1 As Long
    If VV1.IsPlaying = False Then Return
    If VV1.Position > 800 Then
      Timer1.Enabled = False
      Return
    End If
    Dim jo As JavaObject=  VV1
    Dim vvwidth As String = jo.RunMethodJO("getMeasuredWidth", Null)
    Dim vvheight As String = jo.RunMethodJO("getMeasuredHeight", Null)
    VV1width1 = vvwidth.Replace("(Integer) ","")
    VV1height1 = vvheight.Replace("(Integer) ","")
    Log(VV1width1 & " x " & VV1height1)
    VV1.Left = (LayoutVal.Width - VV1width1)/2
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Upvote 0

Syd Wright

Well-Known Member
Licensed User
The idea is to (somehow) start playing the MP4 file before the download is completed, i.e. stream it, not download it.
With a 4 Mbytes Youtube video, playing starts within 5 seconds (in my code above), but with larger/longer files it will take much longer.
If you (or anyone else) know any other method to find the real Youtube stream URL, then please share it with us.
 
Upvote 0

analizer3816

Member
Licensed User
I tried this with the B4A Youtube video: ....//www.youtube.com/watch?v=cgxWP4Wu77s
but no usable streams. I can download the MP4 with one of the found links: https://www.saveitoffline.com/get/?...aHnAGjY0ns&u=d5eaRrcHKaVgmRfXXp9Z1FSp73GgtX8T but cannot use it to instantly stream the video. How to find the ...mp4 URL?

Kindly show us how to do this with saveitoffline. Thank you!

you must add json , text or xml
example:
B4X:
https://www.saveitoffline.com/process/?url=www.youtube.com/watch?v=cgxWP4Wu77s&type=json
 
Upvote 0

John W. Miescher

Member
Licensed User
Looking at all the replies I think there must be somewhere a simple and reliable solution. So I concocted the following which failed miserably and I wonder why:

Dim videoID As String = "FaFftJh7nKc"
Dim su As StringUtils
Dim A, encoded As String
dim j, k as inte

Dim job As HttpJob
job.Initialize("YouTube",Me)
job.Download("http://www.youtube.com/get_video_info?video_id="&videoID)
wait for (job) JobDone(job As HttpJob)
If job.Success Then
A=job.GetString
encoded=su.DecodeUrl(A,"UTF-8")
j=A.IndexOf("dashmpd")
k=encoded.IndexOf("dashmpd")

no dashmpd in sight and when I do it ten times in a row I get ten different strings returned.

Can anyone enlighten me, please. Thanks in advance

john m.
 
Upvote 0
Top