Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send video to server from android,which able to play back in browser when go to the URL assign to it?

I send a video file(capture from camera) to my PHP server using Retrofit 2,the video is successfully uploaded to a folder in server(I checked with FileZilla, the video exists in the folder),I assign the video to a URL,I go to the same Url(using browser) it cant play the video.

It just appears like this in Url(example: mydomain.cc/video/VID_2014.mp4)

enter image description here

So I test with a random video, send it via postman, the URL of that video is able to playback.

Like so:

enter image description here

My video file path in Android that I get in onActivityResult after capturing video looks like this

/storage/emulated/0/DCIM/ABC/VID_20171008_183129.mp4

Here is my code for sending video file

 private void uploadVideoToServer(String pathToVideoFile){
    File videoFile = new File(pathToVideoFile);
    RequestBody videoBody = RequestBody.create(MediaType.parse("video/*"), videoFile);
    MultipartBody.Part vFile = MultipartBody.Part.createFormData("video", videoFile.getName(), videoBody);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(SERVER_PATH)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    VideoInterface vInterface = retrofit.create(VideoInterface.class);
    Call<ResultObject>  serverCom = vInterface.uploadVideoToServer(vFile);
    serverCom.enqueue(new Callback<ResultObject>() {
        @Override
        public void onResponse(Call<ResultObject> call, Response<ResultObject> response) {
            ResultObject result = response.body();
            if(!TextUtils.isEmpty(result.getSuccess())){
                Toast.makeText(MainActivity.this, "Result " + result.getSuccess(), Toast.LENGTH_LONG).show();
                Log.d(TAG, "Result " + result.getSuccess());
            }
        }
        @Override
        public void onFailure(Call<ResultObject> call, Throwable t) {
            Log.d(TAG, "Error message " + t.getMessage());
        }
    });
}

VideoInterface.java

public interface VideoInterface {
    @Multipart
    @POST("video.php")
    Call<ResultObject> uploadVideoToServer(@Part MultipartBody.Part video);
}

So my question is, why video capture from the Android camera not able to playback in Url? Does this matter?

Cause I worried later I need to display back to my app by using the Url, if matter how to solve this?

So somebody please give me a complete solution for doing this..

EDIT: I checked the codec by using the ffmpeg, here is the result..

enter image description here enter image description here

like image 826
ken Avatar asked Oct 08 '17 10:10

ken


Video Answer


2 Answers

The audio and video codecs used by your videos matter; not all codecs are supported by all players. Mozilla has a nice table of supported codecs by platform and player. Based on this table, I think you want:

  1. VP8 and Vorbis in WebM (requires container change)
  2. H.264 and MP3 in MP4
  3. H.264 and AAC in MP4

You can check the codecs using the popular vlc media player, or ffmpeg.

After capturing your video, you may need to convert to suitable web codecs. ffmpeg is a free tool for converting codecs and containers.

The ffmpeg documentation example for converting to webm is:

ffmpeg -i input.mp4 -c:v libvpx -b:v 1M -c:a libvorbis output.webm

You may need to tweak the bitrate 1M for your quality/size preferences.

like image 154
mattm Avatar answered Oct 11 '22 02:10

mattm


Video MP4 store the metadata needed to play it at the and of the file. Did you wait for the complete download of video before give up ?

like image 33
alrama Avatar answered Oct 11 '22 02:10

alrama