VideoDownloader Android SDK

repository·master·Indexed 19 days ago

https://github.com/jeffmony/videodownloader

An Android SDK for downloading video formats including standard files (mp4, mkv) and HLS (M3U8) streams. It features task queuing, M3U8 to MP4 merging via JeffFFmpegDemo or JeffM3U8Lib, and detailed progress tracking through VideoDownloadManager and VideoTaskItem.

Tokens
2.8K
Snippets
7
Records
7
Agent score
18%

What's inside VideoDownloader

  1. Install VideoDownloader via Gradle

    master

    To use the VideoDownloader SDK, you must first add the JitPack repository to your allprojects block in build.gradle, then add the specific dependency to your app's dependencies block.

    If you require M3U8 to MP4 merging capabilities, you must also include the JeffFFmpegDemo library. Alternatively, for a smaller footprint, you can use JeffM3U8Lib.

    Dependencies:

    • Core SDK: com.github.JeffMony:VideoDownloader:5.8.0
    • FFmpeg (for M3U8 to MP4): com.github.JeffMony:JeffFFmpegDemo:1.6.0
    • Lightweight M3U8 Lib: com.github.JeffMony:JeffM3U8Lib:1.2.0
    // 1. Add JitPack to allprojects
    allprojects {
        repositories {
    	    maven { url 'https://jitpack.io' }
    	}
    }
    
    // 2. Add dependencies
    dependencies {
        implementation 'com.github.JeffMony:VideoDownloader:5.8.0'
        // Optional: for M3U8 to MP4 merging
        implementation 'com.github.JeffMony:JeffFFmpegDemo:1.6.0'
        // OR: for a smaller library
        // implementation 'com.github.JeffMony:JeffM3U8Lib:1.2.0'
    }
  2. Register a global DownloadListener

    master

    Register a single global listener via VideoDownloadManager.getInstance().setGlobalDownloadListener(mListener) to receive updates for all download tasks, including progress, speed, errors, and success states.

    VideoDownloadManager.getInstance().setGlobalDownloadListener(mListener);
    
    private DownloadListener mListener = new DownloadListener() {
        @Override
        public void onDownloadDefault(VideoTaskItem item) {}
        @Override
        public void onDownloadPending(VideoTaskItem item) {}
        @Override
        public void onDownloadPrepare(VideoTaskItem item) {}
        @Override
        public void onDownloadStart(VideoTaskItem item) {}
        @Override
        public void onDownloadProgress(VideoTaskItem item) {}
        @Override
        public void onDownloadSpeed(VideoTaskItem item) {}
        @Override
        public void onDownloadPause(VideoTaskItem item) {}
        @Override
        public void onDownloadError(VideoTaskItem item) {}
        @Override
        public void onDownloadSuccess(VideoTaskItem item) {}
    };
  3. Initialize VideoDownloadManager configuration

    master

    Before downloading, you must register a VideoDownloadConfig with the VideoDownloadManager. This setup defines the cache directory, timeout settings, concurrency, and whether M3U8 files should be merged into MP4.

    File file = VideoDownloadUtils.getVideoCacheDir(this);
    if (!file.exists()) {
        file.mkdir();
    }
    VideoDownloadConfig config = new VideoDownloadManager.Build(this)
        .setCacheRoot(file)
        .setUrlRedirect(true)
        .setTimeOut(DownloadConstants.READ_TIMEOUT, DownloadConstants.CONN_TIMEOUT)
        .setConcurrentCount(DownloadConstants.CONCURRENT)
        .setIgnoreCertErrors(true)
        .setShouldM3U8Merged(true)
        .buildConfig();
    VideoDownloadManager.getInstance().initConfig(config);
  4. Merge M3U8 TS files into MP4

    master

    If you have the necessary FFmpeg dependency, you can manually merge TS segments into an MP4 file using VideoProcessManager.mergeTs.

    VideoProcessManager.getInstance().mergeTs(inputFilePath, outputFilePath, new IM3U8MergeListener() {
        @Override
        public void onMergedFinished() {
            // Success logic
        }
    
        @Override
        public void onMergeFailed(Exception e) {
            // Error logic
        }
    });
  5. Manage video download tasks

    master

    Use the VideoDownloadManager singleton to control the lifecycle of download tasks:

    • Fetch tasks: Get a list of current tasks using fetchDownloadItems(IDownloadInfosCallback).
    • Start: startDownload(item)
    • Pause: pauseDownloadTask(item.getUrl()) or pauseDownloadTask(List<String> urlList) for multiple URLs.
    • Resume: pauseDownloadTask(item.getUrl()) (Note: The documentation uses the same method name for pause and resume).
    • Delete: deleteVideoTask(String videoUrl, boolean shouldDeleteSourceFile) to remove a task and optionally its source file.
    // Fetch current tasks
    VideoDownloadManager.getInstance().fetchDownloadItems(new IDownloadInfosCallback() {
        @Override
        public void onDownloadInfos(List<VideoTaskItem> items) {
            // Handle list of VideoTaskItem
        }
    });
    
    // Start a task
    VideoDownloadManager.getInstance().startDownload(item);
    
    // Pause a task
    VideoDownloadManager.getInstance().pauseDownloadTask(item.getUrl());
    
    // Delete a task
    VideoDownloadManager.getInstance().deleteVideoTask(item.getUrl(), true);
  6. Reference: VideoTaskState constants

    master

    The VideoTaskState class defines the possible states of a VideoTaskItem:

    ConstantValueDescription
    DEFAULT0Default state
    PENDING-1Queued for download
    PREPARE1Preparing download
    START2Starting download
    DOWNLOADING3Currently downloading
    PROXYREADY4Video ready for side-by-side playback
    SUCCESS5Download completed
    ERROR6Download error
    PAUSE7Download paused
    ENOSPC8Insufficient disk space
    public class VideoTaskState {
        public static final int DEFAULT = 0;
        public static final int PENDING = -1;
        public static final int PREPARE = 1;
        public static final int START = 2;
        public static final int DOWNLOADING = 3;
        public static final int PROXYREADY = 4;
        public static final int SUCCESS = 5;
        public static final int ERROR = 6;
        public static final int PAUSE = 7;
        public static final int ENOSPC = 8;
    }
  7. Reference: VideoTaskItem fields

    master

    The VideoTaskItem object contains detailed metadata about a download task. Key fields include:

    • mUrl: The source video URL.
    • mFinalUrl: The URL after redirects.
    • mTaskState: Current status (see VideoTaskState).
    • mPercent: Download percentage (0.0 to 100.0).
    • mSpeed: Current download speed.
    • mFilePath: The full local path to the downloaded file.
    • mIsCompleted: Boolean indicating if the download finished.
    • mM3U8: M3U8 structure (null if not an M3U8 video).
    public class VideoTaskItem implements Cloneable {
        private String mUrl;                 // 下载视频的url
        private long mDownloadCreateTime;    // 下载创建的时间
        private int mTaskState;              // 当前任务的状态
        private String mMimeType;             // 视频url的mime type
        private String mFinalUrl;            // 30x跳转之后的url
        private int mErrorCode;              // 当前任务下载错误码
        private int mVideoType;              // 当前文件类型
        private M3U8 mM3U8;                  // M3U8结构,如果非M3U8,则为null
        private int mTotalTs;                // 当前M3U8的总分片
        private int mCurTs;                  // 当前M3U8已缓存的分片
        private float mSpeed;                // 当前下载速度
        private float mPercent;              // 当前下载百分比, 0 ~ 100
        private long mDownloadSize;          // 已下载大小
        private long mTotalSize;            // 文件总大小
        private String mFileHash;            // 文件名的md5
        private String mSaveDir;             // 保存视频文件的文件目录名
        private boolean mIsCompleted;        // 是否下载完成
        private boolean mIsInDatabase;       // 是否存到数据库中
        private long mLastUpdateTime;        // 上一次更新数据库的时间
        private String mFileName;           // 文件名
        private String mFilePath;           // 文件完整路径(包括文件名)
        private boolean mPaused;
    }