Search code examples
videoaws-lambdaffmpegmobile-safari

FFMPEG on AWS Lambda Only outputs half a second of video when converting video from mobile safari


I am running FFMPEG on AWS Lambda using nodejs and fluent-ffmpeg. I am able to convert videos recorded on most devices / browsers, but when I attempt to convert videos recorded on Mobile Safari, the length of the output video is ~1/2 a second regardless of the input length.

My Typescript code is:

    import * as ffmpeg from 'fluent-ffmpeg';
    public static StandardizeVideoFormat(
      inputSignedUrl: string,
      filename: string,
    ): Promise<string> {
      const localFilePath = `/tmp/${Date.now()}_${filename}.mp4`;
    
      return new Promise((resolve, reject) => {
        ffmpeg(inputSignedUrl)
          .outputOptions([
            '-crf',
            '18',
            '-vcodec',
            'libx264',
            '-codec:a',
            'aac',
            '-preset',
            'medium',
          ])
          .output(localFilePath)
          .on('start', function (commandLine) {
            console.log('Spawned Ffmpeg with command: ' + commandLine);
          })
          .on('error', function (err) {
            reject(err);
          })
          .on('progress', function (progress) {
            console.log('progress', progress);
          })
          .on('end', function () {
            resolve(localFilePath);
          })
          .run();
      });
    }

Using FFprobe, The metadata of the input file is:

{
  streams: [
    {
      index: 0,
      codec_name: 'h264',
      codec_long_name: 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10',
      profile: 'Baseline',
      codec_type: 'video',
      codec_tag_string: 'avc1',
      codec_tag: '0x31637661',
      width: 1920,
      height: 1080,
      coded_width: 1920,
      coded_height: 1080,
      closed_captions: 0,
      film_grain: 0,
      has_b_frames: 0,
      sample_aspect_ratio: 'N/A',
      display_aspect_ratio: 'N/A',
      pix_fmt: 'yuvj420p',
      level: 40,
      color_range: 'pc',
      color_space: 'unknown',
      color_transfer: 'unknown',
      color_primaries: 'unknown',
      chroma_location: 'left',
      field_order: 'progressive',
      refs: 1,
      is_avc: 'true',
      nal_length_size: 4,
      id: '0x1',
      r_frame_rate: '30000/1001',
      avg_frame_rate: '60000/1991',
      time_base: '1/600',
      start_pts: 26,
      start_time: 0.043333,
      duration_ts: 4008,
      duration: 6.68,
      bit_rate: 10175170,
      max_bit_rate: 'N/A',
      bits_per_raw_sample: 8,
      nb_frames: 'N/A',
      nb_read_frames: 'N/A',
      nb_read_packets: 'N/A',
      extradata_size: 31,
      side_data_type: 'Display Matrix',
      displaymatrix: '',
      rotation: '-90',
      tags: [Object],
      disposition: [Object]
    },
    {
      index: 1,
      codec_name: 'aac',
      codec_long_name: 'AAC (Advanced Audio Coding)',
      profile: 'LC',
      codec_type: 'audio',
      codec_tag_string: 'mp4a',
      codec_tag: '0x6134706d',
      sample_fmt: 'fltp',
      sample_rate: 48000,
      channels: 1,
      channel_layout: 'mono',
      bits_per_sample: 0,
      initial_padding: 0,
      id: '0x2',
      r_frame_rate: '0/0',
      avg_frame_rate: '0/0',
      time_base: '1/48000',
      start_pts: 0,
      start_time: 0,
      duration_ts: 319488,
      duration: 6.656,
      bit_rate: 181302,
      max_bit_rate: 'N/A',
      bits_per_raw_sample: 'N/A',
      nb_frames: 'N/A',
      nb_read_frames: 'N/A',
      nb_read_packets: 'N/A',
      extradata_size: 2,
      tags: [Object],
      disposition: [Object]
    }
  ],
  format: {
    filename: 'input.mp4',
    nb_streams: 2,
    nb_programs: 0,
    format_name: 'mov,mp4,m4a,3gp,3g2,mj2',
    format_long_name: 'QuickTime / MOV',
    start_time: 0,
    duration: 6.723333,
    size: 8651792,
    bit_rate: 10294646,
    probe_score: 100,
    tags: {
      major_brand: 'iso5',
      minor_version: '1',
      compatible_brands: 'isomiso5hlsf',
      creation_time: '2023-08-05T19:03:15.000000Z'
    }
  },
  chapters: []
}

Any ideas on why the output of my command on this file always results in a ~0.5 second long output?


Solution

  • Turns out that the version of FFmpeg that I had installed via the @ffmpeg-installer/ffmpeg npm package was ~5 years out of date (compiled in 2017). I manually updated to the latest version and the problem with ios Safari videos disappeared.