FFMPEG-ga Firebase Cloud funksiyasida qo'ng'iroq qilishingiz mumkin

Per the Firebase Cloud Functions documentation, you can leverage ImageMagick from within a cloud function: https://firebase.google.com/docs/functions/use-cases

Shu kabi narsalarni qilish mumkinmi, lekin ImageMagick o'rniga FFMPEG-ga qo'ng'iroq qilish mumkinmi? Tasvirlarni kichraytirish ajoyib bo'lsa-da, Firebase Storage-da saqlangan video faylga kiruvchi tasvirlarni qo'shish imkoniyatini ham xohlayman.

9
Yodda tutingki, diskdagi bo'sh joy va xotira bilan ishlashingiz cheklangan. Aslida, xotira diskida xotirada saqlanadi, shuning uchun sizda katta video bo'lsa, osongina xotiradan o'chirilishi mumkin.
qo'shib qo'ydi muallif Doug Stevenson, manba
Yodda tutingki, diskdagi bo'sh joy va xotira bilan ishlashingiz cheklangan. Aslida, xotira diskida xotirada saqlanadi, shuning uchun sizda katta video bo'lsa, osongina xotiradan o'chirilishi mumkin.
qo'shib qo'ydi muallif Doug Stevenson, manba

7 javoblar

ffmpeg is not preinstalled (pretty much just ImageMagick); to see exactly what's installed check out the Dockerfile here: https://github.com/GoogleCloudPlatform/nodejs-docker/blob/master/runtime-image/Dockerfile.

Biroq, siz kodni o'zingizning kodingizni yuklashingiz bilan gcloud beta functionalities yordamida tasodifiy ikkiliklarni yuklashingiz mumkin, chunki mavjud katalogda ( node_modules tashqari) hamma narsa yuklanadi.

Note: you only have disk write access at /tmp/.

Varian-1: ffmpeg-static npm modulidan foydalaning

ffmpeg-static is an npm module that builds the correct ffmpeg binary based on the current system during npm install. Since Cloud Functions builds your code in the cloud, it'll build the correct ffmpeg binary.

https://github.com/eugeneware/ffmpeg-static

Buni amalda Firebase misollari uchun bulut funktsiyalarida ko'rishingiz mumkin .

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');

var cmd = ffmpeg('/tmp/video.avi')
  .setFfmpegPath(ffmpeg_static.path)
  .videoBitrate(1024)
  .videoCodec('divx')
  .format('avi')
  .on('end',() => {
   //...
  })
  .on('error', err => {
    console.error(err);
  })
  .save('/tmp/file-out.avi');
</div> </div>

(Daniel Lessanga ushbu modulga uning javobiga javob bergani uchun rahmat.)

Variant 2: O'zingiz ikkilikni yuklash

Siz ffmpeg ikkilikni yuklashning bir qismi sifatida qo'shishingiz mumkin va keyin child_process.exec . Siz maqsadli platforma (Debian/jessie) uchun to'plangan ffmpeg ikkilikiga ega bo'lishingiz kerak.

Oldindan olingan ffmpeg ikkilikli fayllar ro'yxati

./
../
index.js
ffmpeg

Then run for example gcloud beta functions deploy myFunc --trigger-http

index.js

var exec = require('child_process').exec;
var cmd = 'ffmpeg -i /tmp/myvideo.mp4 /tmp/image-%d.jpg';

exec(cmd, function(error, stdout, stderr) {
 //command output is in stdout
});
18
qo'shib qo'ydi
Bu hayotni qutqaruvchi edi - rahmat! Github sahifalarini yoritadigan bunday yordam bo'ldi. Faqat fluent-ffmpeg npm paketini ishlatib, bir tomonlama eslatma va Docker faylida ikkilik ffmpeg yo'lini ENV o'zgaruvchisi sifatida kiritish kerak edi: ENV PATH = "/ usr/src/app/node_modules/ffmpeg-static/bin/linux/x64: & zwnj; $ {PATH} "
qo'shib qo'ydi muallif Peza, manba
@OmarHossamAgar siz o'zingizning vazifangizning bir qismi sifatida yuklagan narsangiz serverdagi /user_code/ katalogida saqlanadi.
qo'shib qo'ydi muallif Bret McGowen, manba
Bulutli firebase funksiyasidagi faylga qanday murojaat qilishim mumkin? FFMPEG buyrug'ida firebase xotirasidagi faylga yozish uchun qanday yo'l bor?
qo'shib qo'ydi muallif Omar HossamEldin, manba
qo'shib qo'ydi muallif user473453, manba

ffmpeg is not preinstalled (pretty much just ImageMagick); to see exactly what's installed check out the Dockerfile here: https://github.com/GoogleCloudPlatform/nodejs-docker/blob/master/runtime-image/Dockerfile.

Biroq, siz kodni o'zingizning kodingizni yuklashingiz bilan gcloud beta functionalities yordamida tasodifiy ikkiliklarni yuklashingiz mumkin, chunki mavjud katalogda ( node_modules tashqari) hamma narsa yuklanadi.

Note: you only have disk write access at /tmp/.

Varian-1: ffmpeg-static npm modulidan foydalaning

ffmpeg-static is an npm module that builds the correct ffmpeg binary based on the current system during npm install. Since Cloud Functions builds your code in the cloud, it'll build the correct ffmpeg binary.

https://github.com/eugeneware/ffmpeg-static

Buni amalda Firebase misollari uchun bulut funktsiyalarida ko'rishingiz mumkin .

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');

var cmd = ffmpeg('/tmp/video.avi')
  .setFfmpegPath(ffmpeg_static.path)
  .videoBitrate(1024)
  .videoCodec('divx')
  .format('avi')
  .on('end',() => {
   //...
  })
  .on('error', err => {
    console.error(err);
  })
  .save('/tmp/file-out.avi');
</div> </div>

(Daniel Lessanga ushbu modulga uning javobiga javob bergani uchun rahmat.)

Variant 2: O'zingiz ikkilikni yuklash

Siz ffmpeg ikkilikni yuklashning bir qismi sifatida qo'shishingiz mumkin va keyin child_process.exec . Siz maqsadli platforma (Debian/jessie) uchun to'plangan ffmpeg ikkilikiga ega bo'lishingiz kerak.

Oldindan olingan ffmpeg ikkilikli fayllar ro'yxati

./
../
index.js
ffmpeg

Then run for example gcloud beta functions deploy myFunc --trigger-http

index.js

var exec = require('child_process').exec;
var cmd = 'ffmpeg -i /tmp/myvideo.mp4 /tmp/image-%d.jpg';

exec(cmd, function(error, stdout, stderr) {
 //command output is in stdout
});
18
qo'shib qo'ydi
Bu hayotni qutqaruvchi edi - rahmat! Github sahifalarini yoritadigan bunday yordam bo'ldi. Faqat fluent-ffmpeg npm paketini ishlatib, bir tomonlama eslatma va Docker faylida ikkilik ffmpeg yo'lini ENV o'zgaruvchisi sifatida kiritish kerak edi: ENV PATH = "/ usr/src/app/node_modules/ffmpeg-static/bin/linux/x64: & zwnj; $ {PATH} "
qo'shib qo'ydi muallif Peza, manba
@OmarHossamAgar siz o'zingizning vazifangizning bir qismi sifatida yuklagan narsangiz serverdagi /user_code/ katalogida saqlanadi.
qo'shib qo'ydi muallif Bret McGowen, manba
Bulutli firebase funksiyasidagi faylga qanday murojaat qilishim mumkin? FFMPEG buyrug'ida firebase xotirasidagi faylga yozish uchun qanday yo'l bor?
qo'shib qo'ydi muallif Omar HossamEldin, manba
qo'shib qo'ydi muallif user473453, manba

Use the lib https://github.com/eugeneware/ffmpeg-static

const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');


let cmd = ffmpeg.('filePath.mp4')
   .setFfmpegPath(ffmpeg_static.path)
   .setInputFormat('mp4')
   .output('outputPath.mp4')
   ...
   ...
   .run()
5
qo'shib qo'ydi
qo'shib qo'ydi muallif Henry, manba

Texnik ravishda Firefase Funktsiyalar misolida FFMPEG-ni ishlatishingiz mumkin, siz kichik kvota cheklovlarini tezlik bilan urib olasiz.

Ushbu javobga qarab, siz GCP ning yanada kuchli App Engine yoki Compute Engine xizmatlariga murojaat qilish uchun funktsiyalarni ishlatishingiz mumkin. App Engine protsedurasi faylni bir paqirdan ushlab, transkodlashni bajarishi va tugagan faylni orqaga qaytarishi mumkin. Agar boshqa javoblarni havolada tekshirib ko'rsangiz, bitta foydalanuvchi repo o'rnini bosadi.

2
qo'shib qo'ydi
Menimcha, bu eng aqlli javob
qo'shib qo'ydi muallif ZuzEL, manba
/**
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for t`he specific language governing permissions and
 * limitations under the License.
 */
'use strict';

const functions = require('firebase-functions');
const gcs = require('@google-cloud/storage')();
const path = require('path');
const os = require('os');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');

/**
 * When an audio is uploaded in the Storage bucket We generate a mono channel audio automatically using
 * node-fluent-ffmpeg.
 */
exports.generateMonoAudio = functions.storage.object().onChange(event => {
  const object = event.data;//The Storage object.

  const fileBucket = object.bucket;//The Storage bucket that contains the file.
  const filePath = object.name;//File path in the bucket.
  const contentType = object.contentType;//File content type.
  const resourceState = object.resourceState;//The resourceState is 'exists' or 'not_exists' (for file/folder deletions).
  const metageneration = object.metageneration;//Number of times metadata has been generated. New objects have a value of 1.

 //Exit if this is triggered on a file that is not an audio.
  if (!contentType.startsWith('audio/')) {
    console.log('This is not an audio.');
    return;
  }

 //Get the file name.
  const fileName = path.basename(filePath);
 //Exit if the audio is already converted.
  if (fileName.endsWith('_output.flac')) {
    console.log('Already a converted audio.');
    return;
  }

 //Exit if this is a move or deletion event.
  if (resourceState === 'not_exists') {
    console.log('This is a deletion event.');
    return;
  }

 //Exit if file exists but is not new and is only being triggered
 //because of a metadata change.
  if (resourceState === 'exists' && metageneration > 1) {
    console.log('This is a metadata change event.');
    return;
  }

 //Download file from bucket.
  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = path.join(os.tmpdir(), fileName);
 //We add a '_output.flac' suffix to target audio file name. That's where we'll upload the converted audio.
  const targetTempFileName = fileName.replace(/\.[^/.]+$/, "") + '_output.flac';
  const targetTempFilePath = path.join(os.tmpdir(), targetTempFileName);
  const targetStorageFilePath = path.join(path.dirname(filePath), targetTempFileName);

  return bucket.file(filePath).download({
    destination: tempFilePath
  }).then(() => {
    console.log('Audio downloaded locally to', tempFilePath);
   //Convert the audio to mono channel using FFMPEG.
    const command = ffmpeg(tempFilePath)
      .setFfmpegPath(ffmpeg_static.path)    
      .audioChannels(1)
      .audioFrequency(16000)
      .format('flac')
      .on('error', (err) => {
        console.log('An error occurred: ' + err.message);
      })
      .on('end',() => {
        console.log('Output audio created at', targetTempFilePath);

       //Uploading the audio.
        return bucket.upload(targetTempFilePath, {destination: targetStorageFilePath}).then(() => {
          console.log('Output audio uploaded to', targetStorageFilePath);

         //Once the audio has been uploaded delete the local file to free up disk space.     
          fs.unlinkSync(tempFilePath);
          fs.unlinkSync(targetTempFilePath);

          console.log('Temporary files removed.', targetTempFilePath);
        });
      })
      .save(targetTempFilePath);
  });
});

https://github.com/ firebase/funktsiyalar-namunalar/blob/master/ffmpeg-convert-audio/functions/index.js

0
qo'shib qo'ydi

Bilsam, ffmpeg konteynerlarda oldindan o'rnatilgan emas.

Men ommaviy axborot vositalarini transkod qilish kerak bo'lganda, men ffmpegning o'zaro o'xshashlikdagi versiyasini ko'rib chiqdim. . Mahalliy tugunli muhitlarda yaxshi ishlashi mumkin edi, lekin uni tarqatish uchun hech qachon yo'q edim (chunki transcode kerak emas edi).

0
qo'shib qo'ydi
qo'shib qo'ydi muallif Henry, manba
Yo'q, shunday emas: github.com/GoogleCloudPlatform/nodejs-docker/blob/master/& hellip; . O'zingiz aloqador misol, nfmdan statik ffmpeg ikkilikini to'playdi
qo'shib qo'ydi muallif Mbrevda, manba

Bilsam, ffmpeg konteynerlarda oldindan o'rnatilgan emas.

Men ommaviy axborot vositalarini transkod qilish kerak bo'lganda, men ffmpegning o'zaro o'xshashlikdagi versiyasini ko'rib chiqdim. . Mahalliy tugunli muhitlarda yaxshi ishlashi mumkin edi, lekin uni tarqatish uchun hech qachon yo'q edim (chunki transcode kerak emas edi).

0
qo'shib qo'ydi
qo'shib qo'ydi muallif Henry, manba
Yo'q, shunday emas: github.com/GoogleCloudPlatform/nodejs-docker/blob/master/& hellip; . O'zingiz aloqador misol, nfmdan statik ffmpeg ikkilikini to'playdi
qo'shib qo'ydi muallif Mbrevda, manba