Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to decode video from base64?

I want convert video in base64 String so i convert migBase64 Method through my video in android it convert video to string successfully but when i decode String to Video then it not proper converting in video. so please help me if anyone knows.

i try code like below:

      String encodedString;

    //Decode Video To String

          File tempFile = new File(Environment.getExternalStorageDirectory()+ "/my/part/my_0.mp4");

                byte fileContent[] = new byte[3000];

                try {
                    FileInputStream fin = new FileInputStream(tempFile);
                    while (fin.read(fileContent) >= 0) {

                        // b.append(Base64.encodeToString(fileContent, true));

                        encodedString = Base64.encodeToString(fileContent, true);

                    }
                } catch (IOException e) {

                }
//Encoding Video To String Successfully.

//Decode String To Video

   try {

            byte[] decodedBytes = Base64.decodeF
            File file2 = new File(Environment.getExternalStorageDirectory()
                    + "/my/Converted.mp4");
            FileOutputStream os = new FileOutputStream(file2, true);
            os.write(decodedBytes);
            os.close();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("Error", e.toString());
        }
// Problem is in Decoding.

my problem is decoding string to video, my original video is 1 MB and after decoding video is 1.1 kb it not convert my original video please help me.

like image 542
Rajneesh Avatar asked Nov 10 '14 11:11

Rajneesh


People also ask

How do I decode a Base64 file?

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

Can a video be encoded in Base64?

Sometimes you have to send or output a video file within a text document (for example, HTML, JSON, XML), but you cannot do this because binary characters will damage the syntax of the text document. To prevent this, for example, you can encode video file to Base64 and embed it using the data URI.


1 Answers

I solve my problem, i post code for someone help.

//Encode Video To String With mig Base64.

    File tempFile = new File(Environment.getExternalStorageDirectory()
                + "/my/part/my_0.mp4");
        String encodedString = null;

        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(tempFile);
        } catch (Exception e) {
            // TODO: handle exception
        }
        byte[] bytes;
        byte[] buffer = new byte[8192];
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        bytes = output.toByteArray();
        encodedString = Base64.encodeToString(bytes, true);
        Log.i("Strng", encodedString);


//Decode String To Video With mig Base64.
        byte[] decodedBytes = Base64.decodeFast(encodedString.getBytes());

        try {

            FileOutputStream out = new FileOutputStream(
                    Environment.getExternalStorageDirectory()
                            + "/my/Convert.mp4");
            out.write(decodedBytes);
            out.close();
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("Error", e.toString());

        }
like image 182
Rajneesh Avatar answered Oct 04 '22 22:10

Rajneesh