如何从Base64解码视频?

5
我希望将视频转换为base64字符串,所以我通过我的Android设备将视频转换成了字符串。虽然它成功地将视频转换为字符串,但当我将字符串解码成视频时,它并没有正确地转换为视频。如果有人知道,请帮助我。
我尝试了以下代码:
      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.

我的问题是将字符串解码为视频,我的原始视频大小为1 MB,解码后的视频大小为1.1 kb,它没有转换成我的原始视频,请帮助我。


使用FFmpeg进行视频处理。 - Hafiz.M.Usman
请尝试访问以下链接:https://github.com/churnlabs/android-ffmpeg-sample - Hafiz.M.Usman
但是通过base64无法转换? - Rajneesh
ffmpeg可以将视频转换为字符串吗? - Rajneesh
是的,Base64类可以将图像或视频编码为字符串,并解码原始文件。 - Rajneesh
显示剩余3条评论
1个回答

9
我解决了我的问题,我发布了代码以便有人提供帮助。
//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());

        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接