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

كيف يمكنني تغيير جودة الفيديو إلى 360 بكسل في تطبيق أندرويد؟

Drive Man

السؤال

حد يقدر يساعدنى فى كتابة كود يخلى اشوف جودة الفيديو لو اعلى من 360 بكسل احول جودة الفيديو 360 بكسل واحفظة على التليفون وبعد كده اشغل الفيديو الجديد 

تم التعديل في بواسطة Mustafa Suleiman
تعديل عنوان السؤال
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0
بتاريخ 17 ساعة قال Drive Man:
repositories {
    maven {
        url "https://github.com/wseemann/FFmpegMediaMetadataRetriever"
    }
}

فى مشكلة بسبب السطر ده 

عليك بإضافة مستودع JitPack  إلى إلى قسم repositories في ملف build.gradle:

repositories {
	...
	maven { url 'https://jitpack.io' }
}

ثم، تحتاج إلى إضافة تبعية FFmpegMediaMetadataRetriever إلى قسم dependencies في ملف build.gradle:

dependencies {
 implementation 'com.github.wseemann.FFmpegMediaMetadataRetriever:FFmpegMediaMetadataRetriever-core:v1.0.15'
	}

وفي حال أردت إضافة أي مستودع لمكتبة أخرى نتوجه إلى موقع https://jitpack.io ثم ألصق رابط المستودع الخاص بالمكتبة على GitHub وستظهر لك الإصدارات المتاحة ونضغط على get it وستجد الرابط بالأسفل:

2023-07-30_11-35-58.thumb.png.8a772901074b036c69d7f2ef36e626fa.png

وتستطيع قراءة المستند الرسمي الخاص بالمكتبة في حال واجهتك مشكلة معينة:

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

عليك باستخدام مكتبة للتعامل مع الفيديو وتغيير جودته، مثل مكتبة "FFmpegMediaMetadataRetriever".

وأولاً قم بإضافة مستودع  "FFmpegMediaMetadataRetriever" إلى ملف gradle الخاص بتطبيقك:

repositories {
    maven {
        url "https://github.com/wseemann/FFmpegMediaMetadataRetriever"
    }
}

ثم أضف تبعية المكتبة إلى نفس الملف gradle:

dependencies {
    implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.19'
}

والآن، استخدم المكتبة لتحميل الفيديو وتغيير جودته إلى 360 بكسل وحفظه بشكل مستقل، ومن ثم استعراضه.

import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import wseemann.media.FFmpegMediaMetadataRetriever;
import java.io.File;

public class VideoProcessingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_processing);

        String originalVideoPath = "path/to/original_video.mp4";
        String newVideoPath = Environment.getExternalStorageDirectory().getPath() + "/new_video.mp4";

        int targetWidth = 360; // العرض المستهدف بالبكسل

        FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();

        try {
            retriever.setDataSource(originalVideoPath);
            String videoWidthStr = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
            String videoHeightStr = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);

            int videoWidth = Integer.parseInt(videoWidthStr);
            int videoHeight = Integer.parseInt(videoHeightStr);

            if (videoWidth > targetWidth) {
                // حساب النسبة المئوية لتغيير الحجم
                float scaleRatio = (float) targetWidth / (float) videoWidth;
                int newHeight = Math.round(videoHeight * scaleRatio);

                // تغيير جودة الفيديو وحفظه
                retriever.release();
                FFmpegMediaMetadataRetriever newRetriever = new FFmpegMediaMetadataRetriever();
                newRetriever.setDataSource(originalVideoPath);

                File newFile = new File(newVideoPath);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_GENRE);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_TITLE);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_YEAR);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_MIMETYPE);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM_ARTIST);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DISC_NUMBER);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_NUM_TRACKS);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_WRITER);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_COMPILATION);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_HAS_AUDIO);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_HAS_IMAGE);
                newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_HAS_BINARY);

                // إعداد نسبة تغيير الحجم
                newRetriever.setOption("vf", "scale=" + targetWidth + ":" + newHeight);
                newRetriever.setOption("override_ffmpeg_path", getApplicationInfo().dataDir + "/lib");

                // حفظ الفيديو بالجودة المغيرة
                newRetriever.save(newFile.getAbsolutePath());

                newRetriever.release();

                // الآن يمكنك تشغيل الفيديو الجديد
                // يمكنك استخدام مكتبة مشغل الفيديو المفضل لديك
                // مثال:
                // VideoView videoView = findViewById(R.id.videoView);
                // videoView.setVideoPath(newVideoPath);
                // videoView.start();
            } else {
                Toast.makeText(this, "جودة الفيديو أصغر من 360 بكسل", Toast.LENGTH_SHORT).show();
            }

        } catch (NumberFormatException e) {
            e.printStackTrace();
            Toast.makeText(this, "خطأ في تحميل الفيديو", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "حدث خطأ ما", Toast.LENGTH_SHORT).show();
        } finally {
            retriever.release();
        }
    }
}

وبالطبع التعديلات الأخيرة تم تنفيذها على فرض أن لديك الإذن المناسب للوصول إلى ذاكرة التخزين الخارجية (READ_EXTERNAL_STORAGE و WRITE_EXTERNAL_STORAGE)، ولا تنس تجهيز الإذن في ملف AndroidManifest.xml أيضًا.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_YEAR);
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_MIMETYPE);
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DISC_NUMBER);
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_NUM_TRACKS);
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_WRITER);
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_COMPILATION);
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_HAS_AUDIO);
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_HAS_IMAGE);
newRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_HAS_BINARY);**Cannot resolve symbol 'METADATA_KEY_HAS_BINARY'
newRetriever.setOption("vf", "scale=" + targetWidth + ":" + newHeight);**Cannot resolve method 'setOption' in 'FFmpegMediaMetadataRetriever'
newRetriever.setOption("override_ffmpeg_path", getApplicationInfo().dataDir + "/lib");**Cannot resolve method 'setOption' in 'FFmpegMediaMetadataRetriever'
 newRetriever.save(newFile.getAbsolutePath());**Cannot resolve method 'setOption' in 'FFmpegMediaMetadataRetriever'

 

// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package wseemann.media; import android.content.ContentResolver; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.MediaDataSource; import android.net.Uri; import android.util.Log; import androidx.annotation.RequiresApi; import java.io.FileDescriptor; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.TimeZone; public class FFmpegMediaMetadataRetriever { private static final String TAG = "FMMR"; public static Bitmap.Config IN_PREFERRED_CONFIG; private static final String[] JNI_LIBRARIES = new String[]{"crypto", "ssl", "avutil", "swscale", "avcodec", "avformat", "ffmpeg_mediametadataretriever_jni"}; private long mNativeContext; public static final int OPTION_PREVIOUS_SYNC = 0; public static final int OPTION_NEXT_SYNC = 1; public static final int OPTION_CLOSEST_SYNC = 2; public static final int OPTION_CLOSEST = 3; public static final String METADATA_KEY_ALBUM = "album"; public static final String METADATA_KEY_ALBUM_ARTIST = "album_artist"; public static final String METADATA_KEY_ARTIST = "artist"; public static final String METADATA_KEY_COMMENT = "comment"; public static final String METADATA_KEY_COMPOSER = "composer"; public static final String METADATA_KEY_COPYRIGHT = "copyright"; public static final String METADATA_KEY_CREATION_TIME = "creation_time"; public static final String METADATA_KEY_DATE = "date"; public static final String METADATA_KEY_DISC = "disc"; public static final String METADATA_KEY_ENCODER = "encoder"; public static final String METADATA_KEY_ENCODED_BY = "encoded_by"; public static final String METADATA_KEY_FILENAME = "filename"; public static final String METADATA_KEY_GENRE = "genre"; public static final String METADATA_KEY_LANGUAGE = "language"; public static final String METADATA_KEY_PERFORMER = "performer"; public static final String METADATA_KEY_PUBLISHER = "publisher"; public static final String METADATA_KEY_SERVICE_NAME = "service_name"; public static final String METADATA_KEY_SERVICE_PROVIDER = "service_provider"; public static final String METADATA_KEY_TITLE = "title"; public static final String METADATA_KEY_TRACK = "track"; public static final String METADATA_KEY_VARIANT_BITRATE = "bitrate"; public static final String METADATA_KEY_DURATION = "duration"; public static final String METADATA_KEY_AUDIO_CODEC = "audio_codec"; public static final String METADATA_KEY_VIDEO_CODEC = "video_codec"; public static final String METADATA_KEY_VIDEO_ROTATION = "rotate"; public static final String METADATA_KEY_ICY_METADATA = "icy_metadata"; public static final String METADATA_KEY_FRAMERATE = "framerate"; public static final String METADATA_KEY_CHAPTER_START_TIME = "chapter_start_time"; public static final String METADATA_KEY_CHAPTER_END_TIME = "chapter_end_time"; public static final String METADATA_CHAPTER_COUNT = "chapter_count"; public static final String METADATA_KEY_FILESIZE = "filesize"; public static final String METADATA_KEY_VIDEO_WIDTH = "video_width"; public static final String METADATA_KEY_VIDEO_HEIGHT = "video_height"; public FFmpegMediaMetadataRetriever() { this.native_setup(); } public native void setDataSource(String var1) throws IllegalArgumentException; public void setDataSource(String uri, Map<String, String> headers) throws IllegalArgumentException { int i = 0; String[] keys = new String[headers.size()]; String[] values = new String[headers.size()]; for(Iterator var6 = headers.entrySet().iterator(); var6.hasNext(); ++i) { Map.Entry<String, String> entry = (Map.Entry)var6.next(); keys[i] = (String)entry.getKey(); values[i] = (String)entry.getValue(); } this._setDataSource(uri, keys, values); } private native void _setDataSource(String var1, String[] var2, String[] var3) throws IllegalArgumentException; public native void setDataSource(FileDescriptor var1, long var2, long var4) throws IllegalArgumentException; public void setDataSource(FileDescriptor fd) throws IllegalArgumentException { this.setDataSource(fd, 0L, 576460752303423487L); } public void setDataSource(Context context, Uri uri) throws IllegalArgumentException, SecurityException { if (uri == null) { throw new IllegalArgumentException(); } else { String scheme = uri.getScheme(); if (scheme != null && !scheme.equals("file")) { AssetFileDescriptor fd = null; label128: { try { ContentResolver resolver = context.getContentResolver(); try { fd = resolver.openAssetFileDescriptor(uri, "r"); } catch (FileNotFoundException var17) { throw new IllegalArgumentException(); } if (fd == null) { throw new IllegalArgumentException(); } FileDescriptor descriptor = fd.getFileDescriptor(); if (!descriptor.valid()) { throw new IllegalArgumentException(); } if (fd.getDeclaredLength() < 0L) { this.setDataSource(descriptor); } else { this.setDataSource(descriptor, fd.getStartOffset(), fd.getDeclaredLength()); } } catch (SecurityException var18) { Log.e("FMMR", "SecurityException: ", var18); break label128; } finally { try { if (fd != null) { fd.close(); } } catch (IOException var16) { Log.e("FMMR", "IOException: ", var16); } } return; } this.setDataSource(uri.toString()); } else { this.setDataSource(uri.getPath()); } } } @RequiresApi(23) public void setDataSource(MediaDataSource dataSource) throws IllegalArgumentException { this._setDataSource(dataSource); } private native void _setDataSource(MediaDataSource var1) throws IllegalArgumentException; public native String extractMetadata(String var1); public native String extractMetadataFromChapter(String var1, int var2); public Metadata getMetadata() { Metadata data = new Metadata(); HashMap metadata; if ((metadata = this.native_getMetadata(false, false, (HashMap)null)) == null) { return null; } else { return !data.parse(metadata) ? null : data; } } private final native HashMap<String, String> native_getMetadata(boolean var1, boolean var2, HashMap<String, String> var3); public Bitmap getFrameAtTime(long timeUs, int option) { if (option >= 0 && option <= 3) { Bitmap b = null; BitmapFactory.Options bitmapOptionsCache = new BitmapFactory.Options(); bitmapOptionsCache.inDither = false; byte[] picture = this._getFrameAtTime(timeUs, option); if (picture != null) { b = BitmapFactory.decodeByteArray(picture, 0, picture.length, bitmapOptionsCache); } return b; } else { throw new IllegalArgumentException("Unsupported option: " + option); } } public Bitmap getFrameAtTime(long timeUs) { Bitmap b = null; BitmapFactory.Options bitmapOptionsCache = new BitmapFactory.Options(); bitmapOptionsCache.inDither = false; byte[] picture = this._getFrameAtTime(timeUs, 2); if (picture != null) { b = BitmapFactory.decodeByteArray(picture, 0, picture.length, bitmapOptionsCache); } return b; } public Bitmap getFrameAtTime() { return this.getFrameAtTime(-1L, 2); } private native byte[] _getFrameAtTime(long var1, int var3); public Bitmap getScaledFrameAtTime(long timeUs, int option, int width, int height) { if (option >= 0 && option <= 3) { Bitmap b = null; BitmapFactory.Options bitmapOptionsCache = new BitmapFactory.Options(); bitmapOptionsCache.inDither = false; byte[] picture = this._getScaledFrameAtTime(timeUs, option, width, height); if (picture != null) { b = BitmapFactory.decodeByteArray(picture, 0, picture.length, bitmapOptionsCache); } return b; } else { throw new IllegalArgumentException("Unsupported option: " + option); } } public Bitmap getScaledFrameAtTime(long timeUs, int width, int height) { Bitmap b = null; BitmapFactory.Options bitmapOptionsCache = new BitmapFactory.Options(); bitmapOptionsCache.inDither = false; byte[] picture = this._getScaledFrameAtTime(timeUs, 2, width, height); if (picture != null) { b = BitmapFactory.decodeByteArray(picture, 0, picture.length, bitmapOptionsCache); } return b; } private native byte[] _getScaledFrameAtTime(long var1, int var3, int var4, int var5); public native byte[] getEmbeddedPicture(); public native void release(); private native void native_setup(); private static native void native_init(); private final native void native_finalize(); protected void finalize() throws Throwable { try { this.native_finalize(); } finally { super.finalize(); } } public native void setSurface(Object var1); static { String[] var0 = JNI_LIBRARIES; int var1 = var0.length; for(int var2 = 0; var2 < var1; ++var2) { String jniLibrary = var0[var2]; System.loadLibrary(jniLibrary); } native_init(); } public class Metadata { public static final int STRING_VAL = 1; public static final int INTEGER_VAL = 2; public static final int BOOLEAN_VAL = 3; public static final int LONG_VAL = 4; public static final int DOUBLE_VAL = 5; public static final int DATE_VAL = 6; public static final int BYTE_ARRAY_VAL = 7; private HashMap<String, String> mParcel; public Metadata() { } public boolean parse(HashMap<String, String> metadata) { if (metadata == null) { return false; } else { this.mParcel = metadata; return true; } } public boolean has(String metadataId) { if (!this.checkMetadataId(metadataId)) { throw new IllegalArgumentException("Invalid key: " + metadataId); } else { return this.mParcel.containsKey(metadataId); } } public HashMap<String, String> getAll() { return this.mParcel; } public String getString(String key) { this.checkType(key, 1); return String.valueOf(this.mParcel.get(key)); } public int getInt(String key) { this.checkType(key, 2); return Integer.valueOf((String)this.mParcel.get(key)); } public boolean getBoolean(String key) { this.checkType(key, 3); return Integer.valueOf((String)this.mParcel.get(key)) == 1; } public long getLong(String key) { this.checkType(key, 4); return Long.valueOf((String)this.mParcel.get(key)); } public double getDouble(String key) { this.checkType(key, 5); return Double.valueOf((String)this.mParcel.get(key)); } public byte[] getByteArray(String key) { this.checkType(key, 7); return ((String)this.mParcel.get(key)).getBytes(); } public Date getDate(String key) { this.checkType(key, 6); long timeSinceEpoch = Long.valueOf((String)this.mParcel.get(key)); String timeZone = (String)this.mParcel.get(key); if (timeZone.length() == 0) { return new Date(timeSinceEpoch); } else { TimeZone tz = TimeZone.getTimeZone(timeZone); Calendar cal = Calendar.getInstance(tz); cal.setTimeInMillis(timeSinceEpoch); return cal.getTime(); } } private boolean checkMetadataId(String val) { return true; } private void checkType(String key, int expectedType) { String type = (String)this.mParcel.get(key); if (type == null) { throw new IllegalStateException("Wrong type " + expectedType + " but got " + type); } } } }

رابط هذا التعليق
شارك على الشبكات الإجتماعية

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...