title: How to trim a video using FFmpeg in Objective-C for iOS application
body: <p>I am trying to implement a sample iOS application that can trim a bundled video (.mp4) using FFmpeg library in Objective-C environment. </p>
  
  <p>I used <a href="https://github.com/kewlbear/FFmpeg-iOS-build-script" rel="nofollow">this script</a> to compile and build the FFmpeg for iOS. Those libraries was added to the project but now I'm not sure how to continue it. How can I find which function do the trim behaviour and what are the supportive codecs and etc.</p>
  
  <p>I am interesting do achieve this with out any wrapper but directly accessing the library functions. How can I do this?</p>
  
  <p><strong><em>Update : 1</em></strong></p>
  
  <p>I believe following code snippet will contain the input video file details in a stream</p>
  
  <pre><code>NSString *filePath = [[NSBundle mainBundle] pathForResource:@"KeseMase" ofType:@".mp4"];
  
  AVInputFormat *inputFormat = av_find_input_format([@"mp4" UTF8String]);
  
  const char *utf8FilePath = [filePath UTF8String];
  
  avformat_open_input(&amp;pFormatCtx, utf8FilePath, inputFormat, nil);
  </code></pre>
  
  <p>Or does it contain only the header information of the video file?</p>
  
  <p><strong><em>Update : 2</em></strong></p>
  
  <p>With following code, I was able to convert input video to a stream of AVPacket and store them in a NSMutableArray</p>
  
  <pre><code>- (BOOL)readFrameIntoPacket:(AVPacket *)packet error:(NSError *__autoreleasing *)error
  {
      BOOL continueReading = YES;
  
      int frameReadValue = av_read_frame(pFormatCtx, packet);
  
      if (frameReadValue == 0)
      {
          NSLog(@"%s - %d # Read next frame", __PRETTY_FUNCTION__, __LINE__);
          continueReading = YES;
  
          NSValue *value = [NSValue valueWithBytes:&amp;packet objCType:@encode(AVPacket)];
          [srcPktArr addObject:value];
      }
      else
      {
          continueReading = NO;
          av_free_packet(packet);
      }
      return continueReading;
  }
  </code></pre>
  
  <p>Tasks remain to solve the question are:</p>
  
  <p>1.) How to use those AVPackets to and write a Video file</p>
  
  <p>2.) How to give a start time and end time and write only that part to a video file</p>
  
  <p><strong><em>Update : 3</em></strong></p>
  
  <p>Okay... then I try to write those packets to an out put file like below</p>
  
  <pre><code>- (BOOL)writePacket:(AVPacket *)packet error:(NSError *__autoreleasing *)error
  {
      int writeValue = av_write_frame(outputContext, packet);
  
      if (writeValue == 0)
      {
          return YES;
      }
      else if (writeValue == 1)
      {
          return YES;
      }
      else
      {
          return NO;
      }
  }
  </code></pre>
  
  <p>and ended up with following error</p>
  
  <blockquote>
    <p>[mp4 @ 0x1c04a200] Invalid packet stream index: 17467852</p>
  </blockquote>
  
  <p><strong><em>Update : 4</em></strong></p>
  
  <p>Okay guys, I get <a href="https://bitbucket.org/AnujAroshA/privaterepo/src/a8f7dc54a80ecea04b61149f0891ace1107e28e2/ViewController.m?at=master" rel="nofollow">this far</a>. </p>