What is a faster way of uploading video to server

2019-07-29 07:44发布

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();
        }

    }

}

1条回答
SAY GOODBYE
2楼-- · 2019-07-29 08:21

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).

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

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.

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

查看更多
登录 后发表回答