How to get InputStream and OutputStream from Cloud Storage on App Engine Java?

2,722 views
Skip to first unread message

Richard Arnold

unread,
Apr 24, 2021, 9:27:56 AM4/24/21
to Google App Engine
I am attempting to use FFMPEG on Google App Engine (Java) to re-encode some video files I have that are sitting in Google Cloud Storage. I found a Java wrapper around ffmpeg that I would like to use here: https://github.com/kokorin/Jaffree

My issue is that this library requires an InputStream and OutputStream. I am trying to specify the InputStream as a file in Google Cloud Storage and the OutputStream as a new file. 

I think I have done specifying the InputStream correctly, but I do not know what to do about the OutputStream.

How can I specify the OutputStream to be a new file?

Or if possible, can I just overwrite the file in Google Cloud Storage?

Here is my Java code in Google App Engine so far:

Storage storage = StorageOptions.getDefaultInstance().getService();

// This is the input file
BlobId id = BlobId.of("storageBucket", "videos/video_1.mp4");
byte[] content = storage.readAllBytes(id);
InputStream inputStream = new ByteArrayInputStream(content);

FFmpeg.atPath()
    .addInput(PipeInput.pumpFrom(inputStream))
    .setOverwriteOutput(true)
    .addArguments("-movflags", "faststart")
    .addOutput(PipeOutput.pumpTo(outputStream)) <---- I need to set this
    .setProgressListener(new ProgressListener() {
        @Override
        public void onProgress(FFmpegProgress progress) {
            double percents = 100. * progress.getTimeMillis() / duration.get();
            System.out.println("Progress: " + percents + "%");
        }
    })
    .execute();

Linus Larsen

unread,
Apr 24, 2021, 1:48:28 PM4/24/21
to Google App Engine
How about

// Create your resource
BlobId blobId = BlobId.of(YOUR_BUCKET, YOUR_FILENAME);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("video/mp4").build();
Blob blob = getStorage().create(blobInfo);

WriteChannel writeChannel = blob.writer();
// Then use writeChannel to write your bytes, it extends WritableByteChannel which has

public int write(ByteBuffer src)

I.e you have to create an OutputStream that writes to a ByteBuffer first.


Sort of.

/ Linus

Richard Arnold

unread,
Apr 25, 2021, 9:09:38 AM4/25/21
to Google App Engine
Thanks for the reply. So is this going to overwrite the file specified? Or is it going to create a new file in memory that I then have to save to cloud storage myself? Could you show me how the code you wrote fits in with the code I have?

Linus Larsen

unread,
Apr 25, 2021, 4:34:43 PM4/25/21
to Google App Engine
Well, I can't write all code for you. Basically I would not advice you to read and write potentially large video data files into memory. You will probably
run out of memory. Lets walk thru your code:


// This is the input file
BlobId id = BlobId.of("storageBucket", "videos/video_1.mp4");
byte[] content = storage.readAllBytes(id);
InputStream inputStream = new ByteArrayInputStream(content);

// You can probably use this instead to avoid reading everything in memory
Blob input = storage.get(id);
ReadChannel readChannel = input.reader();
InputStream inputStream = Channels.newInputStream(readChanne);

// Create output, write to a separate file then rename it to the original instead of overwriting input with output
BlobId blobId = BlobId.of("storageBucket", "videos/video_1_retrancoded.mp4");

BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("video/mp4").build();
Blob output = storage.create(blobInfo);
WriteChannel writeChannel = output.writer();
OutputStream outputStream = Channels.newOutputStream(writeChannel);



FFmpeg.atPath()
    .addInput(PipeInput.pumpFrom(inputStream))
    .setOverwriteOutput(true) // Dont see why you would need this?
    .addArguments("-movflags", "faststart")
    .addOutput(PipeOutput.pumpTo(outputStream)) <---- I need to set this
    .setProgressListener(new ProgressListener() {
        @Override
        public void onProgress(FFmpegProgress progress) {
            double percents = 100. * progress.getTimeMillis() / duration.get();
            System.out.println("Progress: " + percents + "%");
        }
    })
    .execute();

// When done rename output file to input file on google storage (or maybe it's a move you need to do.)

I just provided you with some pseudo code, you need to do the implementation, error handling etc. Also there are a bunch of
parameters that you can set on the readable / writable channels, you'll figure it out.

/ Linus

George (Cloud Platform Support)

unread,
Apr 26, 2021, 12:34:09 PM4/26/21
to Google App Engine
Hello Richard, 

Maybe worth mentioning: the present discussion group is for general ideas on the Google App Engine, and related problems and trends. For coding in Java, you’ll be at an advantage to rather post your questions on Stackoverflow, to gain this way access to a large number of experts; it is meant for providing you help with coding.

Reply all
Reply to author
Forward
0 new messages