40
40
import java .util .LinkedList ;
41
41
import java .util .List ;
42
42
import java .util .Map ;
43
+ import java .util .StringTokenizer ;
43
44
import java .util .concurrent .ExecutorService ;
44
45
import java .util .concurrent .Executors ;
45
46
import java .util .concurrent .Future ;
@@ -831,6 +832,20 @@ public static void setLogLevel(final Level level) {
831
832
}
832
833
}
833
834
835
+ static String extractExtensionFromSafDisplayName (final String safDisplayName ) {
836
+ String rawExtension = safDisplayName ;
837
+ if (safDisplayName .lastIndexOf ("." ) >= 0 ) {
838
+ rawExtension = safDisplayName .substring (safDisplayName .lastIndexOf ("." ));
839
+ }
840
+ try {
841
+ // workaround for https://issuetracker.google.com/issues/162440528: ANDROID_CREATE_DOCUMENT generating file names like "transcode.mp3 (2)"
842
+ return new StringTokenizer (rawExtension , " ." ).nextToken ();
843
+ } catch (final Exception e ) {
844
+ android .util .Log .w (TAG , String .format ("Failed to extract extension from saf display name: %s.%s" , safDisplayName , Exceptions .getStackTraceString (e )));
845
+ return "raw" ;
846
+ }
847
+ }
848
+
834
849
/**
835
850
* <p>Converts the given Structured Access Framework Uri (<code>"content:…"</code>) into an
836
851
* input/output url that can be used in FFmpeg and FFprobe commands.
@@ -863,14 +878,7 @@ private static String getSafParameter(final Context context, final Uri uri, fina
863
878
android .util .Log .e (TAG , String .format ("Failed to obtain %s parcelFileDescriptor for %s.%s" , openMode , uri .toString (), Exceptions .getStackTraceString (t )));
864
879
}
865
880
866
- // workaround for https://issuetracker.google.com/issues/162440528: ANDROID_CREATE_DOCUMENT generating file names like "transcode.mp3 (2)"
867
- if (displayName .lastIndexOf ('.' ) > 0 && displayName .lastIndexOf (' ' ) > displayName .lastIndexOf ('.' )) {
868
- String extension = displayName .substring (displayName .lastIndexOf ('.' ), displayName .lastIndexOf (' ' ));
869
- displayName += extension ;
870
- }
871
- // spaces can break argument list parsing, see https://github.com/alexcohn/mobile-ffmpeg/pull/1#issuecomment-688643836
872
- final char NBSP = (char ) 0xa0 ;
873
- return "saf:" + fd + "/" + displayName .replace (' ' , NBSP );
881
+ return "saf:" + fd + "." + FFmpegKitConfig .extractExtensionFromSafDisplayName (displayName );
874
882
}
875
883
876
884
/**
@@ -880,7 +888,7 @@ private static String getSafParameter(final Context context, final Uri uri, fina
880
888
* <p>Requires API Level ≥ 19. On older API levels it returns an empty url.
881
889
*
882
890
* @param context application context
883
- * @param uri saf uri
891
+ * @param uri saf uri
884
892
* @return input url that can be passed to FFmpegKit or FFprobeKit
885
893
*/
886
894
public static String getSafParameterForRead (final Context context , final Uri uri ) {
@@ -894,7 +902,7 @@ public static String getSafParameterForRead(final Context context, final Uri uri
894
902
* <p>Requires API Level ≥ 19. On older API levels it returns an empty url.
895
903
*
896
904
* @param context application context
897
- * @param uri saf uri
905
+ * @param uri saf uri
898
906
* @return output url that can be passed to FFmpegKit or FFprobeKit
899
907
*/
900
908
public static String getSafParameterForWrite (final Context context , final Uri uri ) {
0 commit comments