Fetch icon indicating copy to clipboard operation
Fetch copied to clipboard

Downloading M3U8 manifest file itself instead of Videos

Open rkandroid opened this issue 3 years ago • 11 comments

Thank you for this beautiful library. I really appreciate your work. I am having an issue while downloading M3U8 videos. The library is downloading M3U8 manifest file itself instead of videos. How can I download videos that are mentioned in that manifest file? Thank you.

rkandroid avatar Mar 12 '21 12:03 rkandroid

If HLS is not live stream, you might be able to read each segments relative path inside manifest, then download each segment one by one. But those segments are in .TS format probably and in small length (few seconds) .

jemshit avatar Apr 13 '21 14:04 jemshit

Even after downloading the segment files you'll need ffmpeg to concact the segment files into a full video. This is certainly possible but I would imagine that it requires quite a lot changes around how downloads are scheduled or represented.

wax911 avatar Jun 28 '21 05:06 wax911

Even after downloading the segment files you'll need ffmpeg to concact the segment files into a full video. This is certainly possible but I would imagine that it requires quite a lot changes around how downloads are scheduled or represented.

Use FFmpeg is may not be the good idea. While I use my own downloader (before using Fetch2). I have just get multiple input stream (for each URL one stream) but only one output stream. I won't close output stream unless all file are downloaded. Consider a video file of size 4 bytes(ideally) then next video file adds on same file after 4th byte

Udhayarajan avatar Aug 18 '21 01:08 Udhayarajan

Even after downloading the segment files you'll need ffmpeg to concact the segment files into a full video. This is certainly possible but I would imagine that it requires quite a lot changes around how downloads are scheduled or represented.

Use FFmpeg is may not be the good idea. While I use my own downloader (before using Fetch2). I have just get multiple input stream (for each URL one stream) but only one output stream. I won't close output stream unless all file are downloaded. Consider a video file of size 4 bytes(ideally) then next video file adds on same file after 4th byte

So instead of having individual files (playlist segments) and joining them at the end you just write each segment into one output stream (final file)? I have to say, I've never tried that before

wax911 avatar Aug 18 '21 13:08 wax911

Even after downloading the segment files you'll need ffmpeg to concact the segment files into a full video. This is certainly possible but I would imagine that it requires quite a lot changes around how downloads are scheduled or represented.

Use FFmpeg is may not be the good idea. While I use my own downloader (before using Fetch2). I have just get multiple input stream (for each URL one stream) but only one output stream. I won't close output stream unless all file are downloaded. Consider a video file of size 4 bytes(ideally) then next video file adds on same file after 4th byte

So instead of having individual files (playlist segments) and joining them at the end you just write each segment into one output stream (final file)? I have to say, I've never tried that before

First of all sorry for my bad English for the previous comment I made. But you got the point. Just append all playlist segments' data (byte[]) one after another sequential results in perfect Video Files.

Udhayarajan avatar Aug 18 '21 13:08 Udhayarajan

@wax911 I have tried the same idea in windows but it results in a broken video file. Video plays but frames repeated, skipped etc. However, in android, it runs smoothly. I have no idea why. So don't follow my steps to download the HLS video. If you use FFmpeg there is a command:

ffmpeg -i "<M3U8URL>" -codec copy output.mp4

To download Video from the M3U8 file. No need to worry of downloading individual segment and merging them all-in-one command from FFmpeg.

Udhayarajan avatar Aug 28 '21 03:08 Udhayarajan

Thanks for the heads up @Udhayarajan I am aware of the ffmpeg command, but in this specific scenario if you use that then you'd be delegating the everything to ffmpeg which would defeat the purpose of using fetch

wax911 avatar Sep 01 '21 17:09 wax911

combining ts files to full video without ffmpeg may be possible. Lets say we have all the ts file urls in the list. I am writing this code without IDE so there may be some syntax errors You probably should use this inside AsyncTask

//I am writing this code without IDE so there may be some syntax errors
//You probably should use this inside AsyncTask
ArrayList<String> urls = new ArrayList();
byte[] buf = new byte[512];
for(int i = 0; i < urls.size(); i++){
    try{
        URI uri = new URI(urls.get(i));
        URL url = uri.toURL();
        InputStream is = url.openStream();
        String pathFile = Environment.getExternalStorageDirectory() + "/Download/test.mp4";
        File file = new File(pathFile);
        FileOutputStream fOut = new FileOutputStream(pathFile,file.exists());
        int readBf;
        while((readBf == is.read(buf)) >= 0){
               fOut.write(buf,0,readBf);
          }
    is.close();
    fOut.close();
}catch(IOException e){
  e.printStackTrace();
}
    
    

}




Serkali-sudo avatar Jan 02 '22 19:01 Serkali-sudo

@Serkali-sudo have you tested this though? Does the final output play properly

wax911 avatar Jan 03 '22 18:01 wax911

@Serkali-sudo have you tested this though? Does the final output play properly

i didnt tested but it should work and it will work only if you are already able to parse ts file links from m3u8.I can make full example and test it if you want .

Serkali-sudo avatar Jan 03 '22 19:01 Serkali-sudo

@Serkali-sudo have you tested this though? Does the final output play properly

i didnt tested but it should work and it will work only if you are already able to parse ts file links from m3u8.I can make full example and test it if you want .

Please do, that would be great! 😺 your solution sounds similar to what's below

@wax911 I have tried the same idea in windows but it results in a broken video file. Video plays but frames repeated, skipped etc. However, in android, it runs smoothly. I have no idea why. So don't follow my steps to download the HLS video. If you use FFmpeg there is a command:

ffmpeg -i "<M3U8URL>" -codec copy output.mp4

To download Video from the M3U8 file. No need to worry of downloading individual segment and merging them all-in-one command from FFmpeg.

wax911 avatar Jan 04 '22 09:01 wax911