Skip to content

Commit

Permalink
Add file size to TransformationResult at the end of a Transformation.
Browse files Browse the repository at this point in the history
Tested:
  * Manually using both path and file descriptor.
PiperOrigin-RevId: 475860978
  • Loading branch information
Samrobbo authored and marcbaechinger committed Sep 30, 2022
1 parent f00e43c commit 9ec4e13
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import androidx.media3.extractor.mp4.Mp4Extractor;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Documented;
Expand Down Expand Up @@ -593,6 +594,8 @@ default void onFallbackApplied(

@Nullable private MuxerWrapper muxerWrapper;
@Nullable private ExoPlayer player;
@Nullable private String outputPath;
@Nullable private ParcelFileDescriptor outputParcelFileDescriptor;
private @ProgressState int progressState;
private boolean isCancelling;

Expand Down Expand Up @@ -705,6 +708,8 @@ public void startTransformation(MediaItem mediaItem, String path) throws IOExcep
throw new UnsupportedEncodingException(
"Clipping is not supported when slow motion flattening is requested");
}
this.outputPath = path;
this.outputParcelFileDescriptor = null;
startTransformation(mediaItem, muxerFactory.create(path, containerMimeType));
}

Expand Down Expand Up @@ -733,6 +738,8 @@ public void startTransformation(MediaItem mediaItem, String path) throws IOExcep
@RequiresApi(26)
public void startTransformation(MediaItem mediaItem, ParcelFileDescriptor parcelFileDescriptor)
throws IOException {
this.outputParcelFileDescriptor = parcelFileDescriptor;
this.outputPath = null;
startTransformation(mediaItem, muxerFactory.create(parcelFileDescriptor, containerMimeType));
}

Expand Down Expand Up @@ -876,6 +883,26 @@ private void verifyApplicationThread() {
}
}

/**
* Returns the current size in bytes of the current/latest output file, or {@link C#LENGTH_UNSET}
* if unavailable.
*/
private long getCurrentOutputFileCurrentSizeBytes() {
long fileSize = C.LENGTH_UNSET;

if (outputPath != null) {
fileSize = new File(outputPath).length();
} else if (outputParcelFileDescriptor != null) {
fileSize = outputParcelFileDescriptor.getStatSize();
}

if (fileSize <= 0) {
fileSize = C.LENGTH_UNSET;
}

return fileSize;
}

private static final class TransformerRenderersFactory implements RenderersFactory {

private final Context context;
Expand Down Expand Up @@ -1064,6 +1091,7 @@ private void handleTransformationEnded(@Nullable TransformationException excepti
.setAverageAudioBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_AUDIO))
.setAverageVideoBitrate(muxerWrapper.getTrackAverageBitrate(C.TRACK_TYPE_VIDEO))
.setVideoFrameCount(muxerWrapper.getTrackSampleCount(C.TRACK_TYPE_VIDEO))
.setFileSizeBytes(getCurrentOutputFileCurrentSizeBytes())
.build();

listeners.queueEvent(
Expand Down

0 comments on commit 9ec4e13

Please sign in to comment.