title: FFmpeg .avi to .mp4 video transcoding - Android
body: <p>Using OpenCV 4.5.2 + FFMPEG on an android app</p>
  <p>I'm trying to <strong>convert an .avi video file into a .mp4</strong> file using x264, by running</p>
  <pre><code>ffmpeg -i input.avi -c:v libx264 output.mp4
  </code></pre>
  <p>The transcoding is processed correctly but, when I play the video, the colors are a bit... saturated?</p>
  <p>This transcoding is part of the following flow:</p>
  <ol>
  <li>Grab a .mov video file</li>
  <li>Use OpenCV VideoCapture and VideoWriter to write text on the video frames (output is .avi)</li>
  <li>Then I need to convert .avi file into .mp4 so it's reproducible on exoplayer.</li>
  </ol>
  <p>In step 2. I'm looping all video frames and writing them to a new file, writing a text on them.</p>
  <pre class="lang-kotlin prettyprint-override"><code>val videoWriter = VideoWriter(
              outputFilePath,
              VideoWriter.fourcc('M', 'J', 'P', 'G'),
              15.0,
              Size(1920.0, 1088.0),
              true
          )
  
   val frame = Mat()
   videoCapture.read(frame)
   Imgproc.putText(
      frame,
      &quot;This is a text&quot;,
      Point(200.0, 200.0),
      3,
      5.0,
      Scalar(
          255.0,
          124.0,
          124.0,
          255.0
      ), 
      1
  )
  videoWriter.write(frame)
  
  </code></pre>
  <p>I know that step 2. is probably not corrupting the frames because in my sample app, I'm displaying all frames in an ImageView, and they all match the original .mov video. So, my guess is that the issue is occurring on 3.</p>
  <p>I'm using <code>'com.arthenica:mobile-ffmpeg-min-gpl:4.4'</code> for android to execute the FFMPEG command as follows:</p>
  <pre class="lang-kotlin prettyprint-override"><code>FFmpeg.executeAsync(&quot;-i $outputFilePath -c:v libx264 -y ${mp4File.path}&quot;)
  </code></pre>
  <p>where <code>outputFilePath</code> is the path for the .avi file and <code>mp4File</code> is an existing empty .mp4 file.</p>
  <p>So I guess what I'm looking for is a way to have a lossless video color transcoding between .avi and .mp4 files.</p>
  <p>Here's a screenshot of my sample app. The image on top is the last frame of the .avi video. The image on the bottom is the last frame played on a video player for the .mp4 transcoded video. This frame color difference is noticeable throughout the whole video.</p>
  <p><strong>EDIT:</strong> After some digging, I found out that the issue is that the VideoWritter is messing with the RGB colors. I still don't know the reason why this is happeninng.</p>
  <p><a href="https://i.stack.imgur.com/dveUG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dveUG.jpg" alt="enter image description here" /></a></p>