Skip to content

Commit

Permalink
[FEAT] - Adicionando layer
Browse files Browse the repository at this point in the history
  • Loading branch information
otavio-code authored Jan 26, 2025
1 parent 0badd65 commit cf7a58e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-lambda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
# Baixar e enviar ffmpeg para o bucket S3
- name: Baixar e enviar FFmpeg para o S3
run: make upload-ffmpeg
run: make build-ffmpeg-layer

# Copiar o arquivo de deployment para o diretório onde o Terraform pode acessá-lo
- name: Mover arquivo para o diretório do Terraform
Expand Down
24 changes: 16 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
ZIP_FILE=deployment_package.zip
FFMPEG_URL=https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz
S3_BUCKET=ffmpeg-package-for-lambda
FFMPEG_S3_KEY=ffmpeg/ffmpeg-release-amd64-static.tar.xz
FFMPEG_S3_KEY=ffmpeg/ffmpeg-release-amd64-static.zip

LAYER_ZIP=ffmpeg-layer.zip
LAYER_DIR=ffmpeg-layer

.PHONY: build
build:
Expand All @@ -15,16 +18,21 @@ build:
zip -g $(ZIP_FILE) app/lambda_function.py app/src/**/*
@echo "Pacote de implantação criado: $(ZIP_FILE)"

.PHONY: upload-ffmpeg
upload-ffmpeg:
.PHONY: build-ffmpeg-layer
build-ffmpeg-layer:
@echo "Baixando FFmpeg..."
curl -L $(FFMPEG_URL) -o ffmpeg-release-amd64-static.tar.xz
@echo "Enviando FFmpeg para o S3..."
aws s3 cp ffmpeg-release-amd64-static.tar.xz s3://$(S3_BUCKET)/$(FFMPEG_S3_KEY)
@echo "FFmpeg enviado para o S3: s3://$(S3_BUCKET)/$(FFMPEG_S3_KEY)"
@echo "Extraindo FFmpeg..."
mkdir -p $(LAYER_DIR)/bin
tar -xJf ffmpeg-release-amd64-static.tar.xz --strip-components=1 -C $(LAYER_DIR)/bin
@echo "Compactando camada FFmpeg..."
cd $(LAYER_DIR) && zip -r9 ../$(LAYER_ZIP) .
@echo "Enviando camada FFmpeg para o S3..."
aws s3 cp $(LAYER_ZIP) s3://$(S3_BUCKET)/$(FFMPEG_S3_KEY)
@echo "FFmpeg Layer enviada para o S3: s3://$(S3_BUCKET)/$(FFMPEG_S3_KEY)"

.PHONY: clean
clean:
@echo "Limpando arquivos temporários..."
rm -rf package $(ZIP_FILE) ffmpeg-release-amd64-static.tar.xz
@echo "Limpeza concluída."
rm -rf package $(ZIP_FILE) $(LAYER_DIR) $(LAYER_ZIP) ffmpeg-release-amd64-static.tar.xz
@echo "Limpeza concluída."
35 changes: 8 additions & 27 deletions app/src/service/ffmpeg_extract_frames.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import os
import subprocess
import tarfile
import boto3
from app.src.config.config import logger

s3 = boto3.client('s3')
S3_BUCKET = 'ffmpeg-package-for-lambda'
FFMPEG_S3_KEY = 'ffmpeg/ffmpeg-release-amd64-static.tar.xz'
FFMPEG_LOCAL_PATH = '/tmp/ffmpeg-release-amd64-static.tar.xz'
FFMPEG_BIN_PATH = '/tmp/ffmpeg'


def extract_ffmpeg():
try:
logger.info("Tentando extrair FFmpeg com tar via subprocess...")
subprocess.run(
['tar', '-xJf', FFMPEG_LOCAL_PATH, '-C', '/tmp', '--strip-components', '1'],
check=True,
capture_output=True,
text=True
)
logger.info("Extração via subprocess concluída com sucesso.")
except subprocess.CalledProcessError as e:
logger.error(f"Erro ao extrair com tar: {e.stderr}")
raise e
# Definição do caminho do FFmpeg a partir da camada da Lambda
FFMPEG_BIN_PATH = '/opt/bin/ffmpeg' # O binário será armazenado na camada Lambda


def extract_frames_with_ffmpeg(video_path, output_dir):
Expand All @@ -38,7 +18,8 @@ def extract_frames_with_ffmpeg(video_path, output_dir):
os.makedirs(output_dir, exist_ok=True)

if not os.path.isfile(FFMPEG_BIN_PATH):
extract_ffmpeg()
logger.error(f"O executável FFmpeg não foi encontrado em {FFMPEG_BIN_PATH}")
raise FileNotFoundError(f"O executável FFmpeg não foi encontrado em {FFMPEG_BIN_PATH}")

if not os.path.isfile(video_path):
logger.error(f"O arquivo de vídeo não foi encontrado: {video_path}")
Expand All @@ -47,9 +28,9 @@ def extract_frames_with_ffmpeg(video_path, output_dir):
output_frame_pattern = os.path.join(output_dir, 'frame_%04d.jpg')

command = [
FFMPEG_BIN_PATH, # Caminho do executável FFmpeg
'-i', video_path, # Caminho do vídeo de entrada
'-vf', 'fps=1', # Extrair 1 frame por segundo
FFMPEG_BIN_PATH, # Caminho do executável FFmpeg da camada
'-i', video_path, # Caminho do vídeo de entrada
'-vf', 'fps=1', # Extrair 1 frame por segundo
output_frame_pattern # Padrão para salvar os frames
]

Expand All @@ -62,4 +43,4 @@ def extract_frames_with_ffmpeg(video_path, output_dir):
logger.error(f"Erro ao executar FFmpeg: {e.stderr}")
raise e

return output_dir
return output_dir
15 changes: 14 additions & 1 deletion infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ resource "aws_lambda_function" "lambda_function" {
runtime = "python3.13"
timeout = 60
memory_size = 512

layers = [
aws_lambda_layer_version.ffmpeg_layer.arn
]
ephemeral_storage {
size = 10240
}
Expand All @@ -54,6 +56,17 @@ resource "aws_lambda_function" "lambda_function" {
}
}

resource "aws_lambda_layer_version" "ffmpeg_layer" {
layer_name = "ffmpeg-layer"
description = "FFmpeg static binary layer"
compatible_runtimes = ["python3.9", "python3.10", "python3.11", "python3.12", "python3.13"]

s3_bucket = var.bucket_name_ffmpeg
s3_key = var.object_key_ffmpeg

retain_old_version = true
}

resource "aws_lambda_event_source_mapping" "sqs_event_source" {
event_source_arn = var.sqs_queue_arn
function_name = aws_lambda_function.lambda_function.arn
Expand Down
4 changes: 2 additions & 2 deletions infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ variable "bucket_name_ffmpeg" {
}

variable "object_key_ffmpeg" {
description = "Chave do objeto FFmpeg dentro do bucket S3"
description = "Caminho do arquivo FFmpeg no S3"
type = string
default = "ffmpeg/ffmpeg-release-amd64-static.tar.xz"
default = "ffmpeg/ffmpeg-release-amd64-static.zip"
}

variable "download_path_ffmpeg" {
Expand Down

0 comments on commit cf7a58e

Please sign in to comment.