728x90
반응형

주요 라이브러리:

  • libavcodec: 비디오 및 오디오 코덱에 대한 인코딩 및 디코딩 기능 제공
  • libavformat: 미디어 파일 형식을 다루는 기능 제공
  • libswscale: 이미지 및 비디오의 크기 조절과 색 공간 변환 기능 제공
  • libswresample: 오디오 리샘플링 및 포맷 변환 기능 제공
  • libavfilter: 비디오와 오디오 필터링 기능 제공

다양한 포맷간의 변환, 인코딩, 디코딩, 필터링, 스트리밍 등 여러 작업을 지원

 

공식 문서에서 다양한 설명 제공

import { Injectable } from '@nestjs/common';
import { exec } from 'child_process';  // 'child_process' 모듈을 사용하여 FFmpeg 명령어를 실행
import { promisify } from 'util';  // exec을 Promise 기반으로 사용하기 위해 promisify 사용

const execPromise = promisify(exec);  // exec을 Promise 방식으로 변환

@Injectable()
export class VideoService {
  // 영상 자르기 함수
  async cutVideo(inputPath: string, startTime: string, endTime: string, outputPath: string): Promise<string> {
    try {
    
    
    
      // FFmpeg 명령어 생성
      // -i: 입력 파일 경로
      // -ss: 시작 시간
      // -to: 종료 시간
      // -c copy: 비디오와 오디오 스트림을 복사하여 빠르게 처리
      // outputPath: 자른 후 저장할 파일 경로
      const command = `ffmpeg -i ${원본영상경로로} -ss ${시작 시간} -to ${종료 시간} -c copy ${저장할 경로}`;
      
      
      
      // 명령어 실행
      const { stdout, stderr } = await execPromise(command);
      //execPromise(command)는 Node.js의 child_process.exec을 Promise 기반으로 감싼 버전입니다.
	  //command는 실행할 FFmpeg 명령어로, 이 명령어는 영상 자르기 작업을 수행하게 됩니다.
      
      
      
      
      console.log('FFmpeg Output:', stdout);  // FFmpeg 실행 결과 출력
      if (stderr) {
        console.error('FFmpeg Error:', stderr);  // FFmpeg 에러 메시지 출력
      }



      // 작업 성공 시 반환 메시지
      return `Video cut successfully from ${startTime} to ${endTime}. Output: ${outputPath}`;
    } catch (error) {
      console.error('Error cutting video:', error);  // 에러 발생 시 로그 출력
      throw new Error('Failed to cut video');  // 에러 발생 시 예외 처리
    }
  }
}
728x90
반응형

+ Recent posts