PHP + FFMPEG + S3. Transcode video directly between S3 buckets

眉间皱痕 提交于 2021-01-29 08:20:39

问题


I have:

  1. S3 INPUT bucket
  2. S3 OUTPUT bucket
  3. PHP
  4. ffmpeg

Is it possible to read file directly from INPUT bucket → Transcode to another format → Save into OUTPUT bucket.

Please let me know manuals, libraries, frameworks, any stuff that helps to understand how to do it.
At least python realisations also welcome. At least at least some different language also OK.

Input file size may be more than 10Gb so writing whole file in RAM is undesirable. Some chunk-based way is preferable.

Output format is mostly .mp4, but input format may be ts video or image (jpg, gif, etc)

Looks like PHP-FFmpeg-video-streaming fit to my task, but it can work only with web streaming formats.


回答1:


Okay, answering by myself:

aws s3 cp s3://bucketA/input.jpg - | ffmpeg -y -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -f jpeg_pipe -loop 1 -i - -c:v 'libx264' -r 29.97 -t 30 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2,setsar=1" -c:a aac -shortest -movflags frag_keyframe+empty_moov -f mp4 /dev/stdout | aws s3 cp - s3://bucketB/output.mp4

Explanation:

  1. Stream input file into stdout
aws s3 cp s3://bucketA/input.jpg - 
  1. Read input stream from stdin and stream it into stdout again
ffmpeg ... -f jpeg_pipe ... -i - ... -movflags frag_keyframe+empty_moov ... /dev/stdout

details:

      2.1. -f jpeg_pipe input format must be pipe type. ffmpeg can detect type automatically so that is no necessary parameter
      2.2. -i - read from input (pipe)
      2.3. -movflags frag_keyframe+empty_moov still don't understand what that means, but can't create mp4 without this parameter. If somebody knows, please edit this place.
      2.4. /dev/stdout write output to stdout

  1. Copy stdin stream to output bucket
aws s3 cp - s3://bucketB/output.mp4

But! I don't like this solution. Here is less control on situation and we don't able to raise/catch Exceptions.

I want to control stream by myself. Using PHP SDK S3 client looks smarter... So I still open for considerations.



来源:https://stackoverflow.com/questions/64889909/php-ffmpeg-s3-transcode-video-directly-between-s3-buckets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!