اذهب إلى المحتوى

السؤال

نشر
    public void convert3() throws IOException {
        dila = ProgressDialog.show(uploadved.this, "برجاء الانتظار", "جاري تجهيز الفيديو");
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(uploadved.this, videoUri);
        String height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
        String width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
        int w = Integer.parseInt(width);
        int h = Integer.parseInt(height);

        int quality = w*h/1024*1024;
        String qul = String.valueOf(quality);
        Log.e("tyyyyy", "videoResolution = " + qul);
        Log.e("tyyyyy", "hi = " +height);
        Log.e("tyyyyy", "wi = " +width);
        retriever.release();

        if (h > 1024) {
            try {
                File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), displayName + ".mp4");
                String outputPath = file.getPath();
                String rettopatha=videoUri.getPath().toString();
                Log.d("tyyyyy","rettopath"+"="+rettopatha);
                Bitmap bmp = retriever.getFrameAtTime();
                int newWidth = 576;
                int newHeight = 1024;
                Bitmap resizedBmp = Bitmap.createScaledBitmap(bmp, newWidth, newHeight, true);
                retriever.release();

                MediaMuxer muxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

                // Add the video track
                int videoTrackIndex = muxer.addTrack(MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, newWidth, newHeight));
                Log.e("tyyyyy", "videoResolution = " + videoTrackIndex);

                // Add the audio track
                int audioTrackIndex = muxer.addTrack(MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_AAC, 44100, 2));
                Log.e("tyyyyy", "audioTrackIndex = " + audioTrackIndex);

                muxer.start();

                // Write the video frames to the muxer
                ByteBuffer buffer = ByteBuffer.allocateDirect(resizedBmp.getByteCount());
                resizedBmp.copyPixelsToBuffer(buffer);
                buffer.flip();

                MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
                info.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
                info.offset = 0;

                muxer.writeSampleData(videoTrackIndex, buffer, info);

                // Write the audio samples to the muxer
                short[] samples = new short[resizedBmp.getWidth() * resizedBmp.getHeight()];
                buffer.clear();
                buffer.asShortBuffer().put(samples);
                buffer.flip();

                info = new MediaCodec.BufferInfo();
                info.offset = 0;
                info.size = buffer.remaining();

                muxer.writeSampleData(audioTrackIndex, buffer, info);

                muxer.stop();
                muxer.release();

                Uri uritoupload = Uri.parse(outputPath);
                vedgetr.setMediaController(new MediaController(this));
                dila.dismiss();
                vedgetr.setVideoURI(uritoupload);
                vedgetr.start();
            } catch (IOException e) {
                Log.e("tyyyyy", "catch: " + e.getMessage());
            }
        }
       else if (w > 576) {
            try {
                File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), displayName + ".mp4");
                String outputPath = file.getPath();
                File filea=new File(videoUri.getPath());
                retriever.setDataSource(outputPath);
                Bitmap bmp = retriever.getFrameAtTime();
                int newWidth = 576;
                int newHeight = 1024;
                Bitmap resizedBmp = Bitmap.createScaledBitmap(bmp, newWidth, newHeight, true);
                retriever.release();

                MediaMuxer muxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

                // Add the video track
                int videoTrackIndex = muxer.addTrack(MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, newWidth, newHeight));
                Log.e("tyyyyy", "videoResolution = " + videoTrackIndex);

                // Add the audio track
                int audioTrackIndex = muxer.addTrack(MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_AAC, 44100, 2));
                Log.e("tyyyyy", "audioTrackIndex = " + audioTrackIndex);

                muxer.start();

                // Write the video frames to the muxer
                ByteBuffer buffer = ByteBuffer.allocateDirect(resizedBmp.getByteCount());
                resizedBmp.copyPixelsToBuffer(buffer);
                buffer.flip();

                MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
                info.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
                info.offset = 0;

                muxer.writeSampleData(videoTrackIndex, buffer, info);

                // Write the audio samples to the muxer
                short[] samples = new short[resizedBmp.getWidth() * resizedBmp.getHeight()];
                buffer.clear();
                buffer.asShortBuffer().put(samples);
                buffer.flip();

                info = new MediaCodec.BufferInfo();
                info.offset = 0;
                info.size = buffer.remaining();

                muxer.writeSampleData(audioTrackIndex, buffer, info);

                muxer.stop();
                muxer.release();

                Uri uritoupload = Uri.parse(outputPath);
                vedgetr.setMediaController(new MediaController(this));
                dila.dismiss();
                vedgetr.setVideoURI(uritoupload);
                vedgetr.start();
            } catch (IOException e) {
                Log.e("tyyyyy", "catch: " + e.getMessage());
            }
        }
       else {
           dila.dismiss();
           vedgetr.setVideoURI(videoUri);
           vedgetr.start();
            Toast.makeText(this, "try", Toast.LENGTH_SHORT).show();
        }
    }

 

Recommended Posts

لا توجد أي إجابات على هذا السؤال بعد

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...