【问题标题】:Given a video URL, how can I save a clip of the video onto the Android phone?给定一个视频 URL,我如何将视频片段保存到 Android 手机上?
【发布时间】:2015-03-05 18:58:26
【问题描述】:

给定一个视频网址(例如:www.example.com/video.mp4),我如何以编程方式保存视频的短片(例如:5 秒,00:00:10 - 00:00:15)在手机上的 Android 应用程序中?

目前,这就是我所拥有的 - 但这会将整个视频从 url 保存到设备。看来我需要使用 MediaExtractor 和 MediaMuxer。

URL url = new URL(http_url_path);
URLConnection ucon = url.openConnection();

// Define InputStreams to read from the URLConnection.
// uses 5KB download buffer
InputStream is = ucon.getInputStream();
BufferedInputStream in = new BufferedInputStream(is, BUFFER_SIZE);
FileOutputStream out = new FileOutputStream(file);
byte[] buff = new byte[BUFFER_SIZE];

int len = 0;
while ((len = in.read(buff)) != -1) {
    out.write(buff, 0, len);
}

【问题讨论】:

    标签: android video fileoutputstream mediamuxer mediaextractor


    【解决方案1】:

    依赖 isoviewer-1.0-RC-27。

    public static double[] startTrim(File src, File dst, int startMs, int endMs) throws IOException {
        Movie movie = MovieCreator.build(src.getAbsolutePath());
        List<Track> tracks = movie.getTracks();
        movie.setTracks(new LinkedList<Track>());
        double startTime = startMs/1000;
        double endTime = endMs/1000;
        boolean timeCorrected = false;
        // Here we try to find a track that has sync samples. Since we can only start decoding
        // at such a sample we SHOULD make sure that the start of the new fragment is exactly
        // such a frame
        for (Track track : tracks) {
            if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) {
                if (timeCorrected) {          
                    throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");
                }
                //false,Short interception;true,Long interception
                startTime = correctTimeToSyncSample(track, startTime, true);
                endTime = correctTimeToSyncSample(track, endTime, false);
                timeCorrected = true;
            }
        }
        int x = 0;
        for (Track track : tracks) {
            long currentSample = 0;
            double currentTime = 0;
            long startSample = -1;
            long endSample = -1;
            x++;
            for (int i = 0; i < track.getDecodingTimeEntries().size(); i++) {
                TimeToSampleBox.Entry entry = track.getDecodingTimeEntries().get(i);
                for (int j = 0; j < entry.getCount(); j++) {
                    // entry.getDelta() is the amount of time the current sample covers.
                    if (currentTime <= startTime) {
                        // current sample is still before the new starttime
                        startSample = currentSample;
                    }
                    if (currentTime <= endTime) {
                        // current sample is after the new start time and still before the new endtime
                        endSample = currentSample;
                    } else {
                        // current sample is after the end of the cropped video
                        break;
                    }
                    currentTime += (double) entry.getDelta() / (double) track.getTrackMetaData().getTimescale();
                    currentSample++;
                }
            }
            movie.addTrack(new CroppedTrack(track, startSample, endSample));
            break;
        }
        Container container = new DefaultMp4Builder().build(movie);  
        if (!dst.exists()) {
            dst.createNewFile();
        }
    
        FileOutputStream fos = new FileOutputStream(dst);
        FileChannel fc = fos.getChannel();
        container.writeContainer(fc);      
        fc.close();
        fos.close();
        double[] doubleArray = new double[2] ;
        doubleArray[0] = startTime;
        doubleArray[1] = endTime;
        return doubleArray;
    
    }
    

    【讨论】:

    • 谢谢@sundroid。是否有理由只使用 isoviewer-1.0-RC-27?我在这里看到一个 1.0-RC-35:code.google.com/p/mp4parser/downloads/…。另外,我在这里看到 isoviewer 1.0.5:github.com/sannies/isoviewer/releases。这里还有 mp4parser:github.com/sannies/mp4parser/releases。我应该使用哪个?
    • 你应该使用提供该功能的工具,你应该阅读api.and isoviewer-1.0-RC-27可以提供它。
    • 谢谢@sundroid 我发现只有 isoparser-1.0-RC-27.jar 会提供正确的 API。
    • 我有一个问题@sundroid。出于某种原因,我指定了 10000 (start) 和 20000 (end) ,但生成的视频剪辑是从 12.499999 到 17.499999 (仅 5 秒)。为什么不使用 10 秒的持续时间?
    • 检查您的设置,也许可以设置持续时间!
    【解决方案2】:

    :D 或者你可以很容易地使用这个网站 :D 只需使用意图发送你想要的链接

    http://www.videograbber.net/

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多