All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.schoolwow.download.module.task.flow.downloader.M3u8DownloadFlow Maven / Gradle / Ivy

package cn.schoolwow.download.module.task.flow.downloader;

import cn.schoolwow.download.domain.pool.PoolConfig;
import cn.schoolwow.download.domain.progress.DownloadProgress;
import cn.schoolwow.download.domain.progress.GetFileDownloadedSize;
import cn.schoolwow.download.domain.task.DownloadTask;
import cn.schoolwow.download.module.pool.domain.PoolContext;
import cn.schoolwow.download.module.task.domain.m3u8.M3u8Type;
import cn.schoolwow.download.module.task.domain.m3u8.MediaPlaylist;
import cn.schoolwow.download.module.task.domain.m3u8.tag.SEGMENT;
import cn.schoolwow.download.util.M3u8Util;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.quickhttp.response.Response;

import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class M3u8DownloadFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkM3u8(flowContext);
        setMediaPlaylist(flowContext);
        setSubFileList(flowContext);
        setFileDownloadedSize(flowContext);
        startDownloadFile(flowContext);
    }

    @Override
    public String name() {
        return "m3u8方式下载流程";
    }

    private void checkM3u8(FlowContext flowContext) throws IOException {
        Response response = (Response) flowContext.checkData("response");

        String responseBody = response.body();
        if(!M3u8Type.MediaPlayList.equals(M3u8Util.getM3u8Type(responseBody))){
            flowContext.broken("m3u8地址不是媒体播放列表");
        }
        flowContext.putTemporaryData("url", response.url());
        flowContext.putTemporaryData("responseBody", responseBody);
    }

    private void setMediaPlaylist(FlowContext flowContext){
        String url = (String) flowContext.checkData("url");
        String responseBody = (String) flowContext.checkData("responseBody");

        MediaPlaylist mediaPlaylist = M3u8Util.getMediaPlaylist(responseBody);
        //补充相对路径
        String relativePath = url.substring(0,url.lastIndexOf('/')+1);
        for(SEGMENT segment:mediaPlaylist.segmentList){
            if(!segment.URI.startsWith("http")){
                segment.URI = relativePath + segment.URI;
            }
        }
        flowContext.putTemporaryData("mediaPlaylist", mediaPlaylist);
    }

    private void setSubFileList(FlowContext flowContext){
        PoolConfig poolConfig = flowContext.checkInstanceData(PoolConfig.class);
        DownloadProgress downloadProgress = flowContext.checkInstanceData(DownloadProgress.class);
        MediaPlaylist mediaPlaylist = (MediaPlaylist) flowContext.checkData("mediaPlaylist");
        Response response = (Response) flowContext.checkData("response");
        String fileName = (String) flowContext.checkData("fileName");

        List segmentList = mediaPlaylist.segmentList;
        File[] subFileList = new File[segmentList.size()];
        for(int i=0;i segmentList = mediaPlaylist.segmentList;

        CountDownLatch countDownLatch = new CountDownLatch(subFileList.length);
        for(int i=0;i{
                try {
                    downloadTask.request.clone()
                            .url(url)
                            .execute()
                            .bodyAsFile(tempFile.toPath());
                    Files.move(tempFile.toPath(), subFile.toPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await(1, TimeUnit.HOURS);
    }

    private boolean checkDownloadComplete(FlowContext flowContext){
        File[] subFileList = (File[]) flowContext.checkData("subFileList");

        for(int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy