|
14 | 14 | * Created by Brian on 11-12-17. |
15 | 15 | */ |
16 | 16 | public class ExampleActivity extends AppCompatActivity { |
| 17 | + private static final int MOVIE_OPEN_REQUEST = 1; |
17 | 18 | private Handler handler = new Handler(); |
| 19 | + private String mediaFile; |
18 | 20 |
|
19 | 21 | @Override |
20 | 22 | public void onCreate(Bundle savedInstanceState) { |
@@ -114,4 +116,59 @@ public void run() { |
114 | 116 | } |
115 | 117 | }, 8000); |
116 | 118 | } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onResume() { |
| 122 | + super.onResume(); |
| 123 | + |
| 124 | + if (mediaFile == null) { |
| 125 | + startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT) |
| 126 | + .setType("video/*") |
| 127 | + .putExtra("android.content.extra.SHOW_ADVANCED", true) // thanks Commonsware, https://issuetracker.google.com/issues/72053350 |
| 128 | + .putExtra("android.content.extra.FANCY", true) |
| 129 | + .putExtra("android.content.extra.SHOW_FILESIZE", true) |
| 130 | + .addCategory(Intent.CATEGORY_OPENABLE), DOC_OPEN_REQUEST); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
| 136 | + if (requestCode == MOVIE_OPEN_REQUEST && resultCode == RESULT_OK && data != null) { |
| 137 | + Uri uri = data.getData(); |
| 138 | + try { |
| 139 | + ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r"); |
| 140 | + int fd = parcelFileDescriptor.getFd(); |
| 141 | + int pid = Process.myPid(); |
| 142 | + mediaFile = "/proc/" + pid + "/fd/" + fd; |
| 143 | + Timber.i("opened " + uri + " as " + mediaFile); |
| 144 | + runFFmpeg(); |
| 145 | + } catch (Throwable e) { |
| 146 | + Timber.e(e, "cannot work with " + uri); |
| 147 | + } |
| 148 | + } else { |
| 149 | + super.onActivityResult(requestCode, resultCode, data); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private void runFFmpeg() { |
| 154 | + String outPath = getExternalFilesDir(null) + "/out.mp4" |
| 155 | + new File(outPath).delete(); |
| 156 | + FFmpeg.getInstance(this).execute(new String[]{"-i", mediaFile, "-frames:v", "500", outPath}, new ExecuteBinaryResponseHandler() { |
| 157 | + @Override |
| 158 | + public void onSuccess(String message) { |
| 159 | + Timber.e(message.substring(0, 100)); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void onProgress(String message) { |
| 164 | + Timber.i(message); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void onFailure(String message) { |
| 169 | + Timber.e(message); |
| 170 | + } |
| 171 | + }); |
| 172 | + } |
| 173 | + |
117 | 174 | } |
0 commit comments