使用Android应用程序将视频上​​传到服务器的更快方法是什么? [英] What is a faster way of uploading video to server using android app?

查看:230
本文介绍了使用Android应用程序将视频上​​传到服务器的更快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个应用程序,允许用户使用设备相机录制视频,完成录制后,视频将上传到服务器.我目前正在使用以下代码上传视频,并且效果很好,我唯一遇到的问题是视频处理时间过长. 30秒的视频大约需要20分钟才能上传.有什么方法可以使用其他更快的方法来上传这些视频?我到处都看过了,只找到与我使用的代码相同的帖子.这是我的代码:

I currently have an app that allows the user to record a video with the device camera, after they are done recording, the video uploads to a server. I am currently using the following code to upload the video and it works good, the only issue I am having is that it is taking way too long. A 30 second video can take about 20 minutes to upload. Is there any way to use a different and faster method to upload these videos? I have looked everywhere and I am only finding posts with the same code as I am using. This is my code:

public class UploadActivity extends Activity {

    private String filePath = null;
    private String name, email, videoTitle;
    long totalSize = 0;

    private DonutProgress donutProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);

        donutProgress = (DonutProgress) findViewById(R.id.donut_progress);

        Intent intent = getIntent();

        filePath = intent.getStringExtra("filePath");

        name = intent.getStringExtra("name");
        email = intent.getStringExtra("email");

        new UploadFileToServer().execute();
    }

    private class UploadFileToServer extends AsyncTask<Void, Integer, String> {

        @Override
        protected void onPreExecute() {
            donutProgress.setProgress(0);
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            donutProgress.setProgress(progress[0]);
        }

        @Override
        protected String doInBackground(Void... params) {
            return uploadFile();
        }

        @SuppressWarnings("deprecation")
        private String uploadFile() {
            String responseString;

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(AppConfig.URL_UPLOAD);

            try {
                AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new ProgressListener() {
                    @Override
                    public void transferred(long num) {
                        publishProgress((int) ((num / (float) totalSize) * 100));
                    }
                });
                File sourceFile = new File(filePath);

                entity.addPart("image", new FileBody(sourceFile));

                totalSize = entity.getContentLength();
                httpPost.setEntity(entity);

                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity r_entity = response.getEntity();

                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    responseString = EntityUtils.toString(r_entity);
                } else {
                    responseString = "Error occurred. Http Status Code: " + statusCode;
                }
            } catch (IOException e) {
                responseString = e.toString();
            }
            return responseString;
        }

        @Override
        protected void onPostExecute(String result) {
            videoTitle = result;
            super.onPostExecute(result);
            finish();
        }

    }

}

推荐答案

减少上传时间最明显的方法是压缩视频,前提是您可以降低视频质量(很难量化质量)因此,可能值得您离线尝试使用具有不同压缩率的视频,然后再进行观看).

The most obvious way to reduce the upload time is to compress the video, assuming you can live with any reduction in quality this may bring (its hard to quantify quality so its probably worth you experimenting offline with a video with various compression rates to see first).

不幸的是,在Android中没有方便的'compress()'方法,但是有许多不同的方法:

Unfortunately, in Android there is no handy 'compress()' method but there are a number of different approaches:

  • use ffmpeg to compress the video
  • use videoresampler library (see: https://stackoverflow.com/a/33313421/334402)

我亲自使用了ffmpeg方法-当我这样做时,我找不到自己满意的Android ffmpeg库,因此我自己创建了一个包装器,但是如果我再次这样做,则可能会使用以下ffmpeg包装器:

I have personally used the ffmpeg approach - when I did it I could not find an Android ffmpeg library I was happy with so I created a wrapper myself, but if I was doing ti again I would probably use the following ffmpeg wrapper:

这似乎已被很好地记录和使用,并且包括经过深思熟虑的功能,例如根据处理器体系结构加载不同二进制文件的能力.

This seems well documented and used and it includes well thought out features like the ability to load different binaries depending on the processor architecture.

一旦有了ffmpeg包装器,您就可以简单地使用标准ffmpeg命令行(例如语法)来压缩视频.

Once you have an ffmpeg wrapper you can simply use a standard ffmpeg command line like syntax to compress the video.

这篇关于使用Android应用程序将视频上​​传到服务器的更快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆