<commit_before>/**
   Copyright (c) 2014 <NAME>
   
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
   
       http://www.apache.org/licenses/LICENSE-2.0
   
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 **/

package com.roryhool.videoinfoviewer.data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.util.Log;

import com.coremedia.iso.IsoFile;
import com.coremedia.iso.boxes.Box;
import com.coremedia.iso.boxes.SampleSizeBox;
import com.google.gson.annotations.SerializedName;
import com.roryhool.videoinfoviewer.utils.BoxUtils;

public class Video implements Comparable<Video> {

   @SerializedName( "FileName" )
   public String FileName;

   @SerializedName( "FilePath" )
   public String FilePath;

   @SerializedName( "Format" )
   public String Format;

   @SerializedName( "FormatProfile" )
   public String FormatProfile;

   @SerializedName( "CodecID" )
   public String CodecID;

   @SerializedName( "FileSize" )
   public String FileSize;

   @SerializedName( "MimeType" )
   public String MimeType;

   @SerializedName( "FrameRate" )
   public String FrameRate;

   @SerializedName( "Duration" )
   public String Duration;

   @SerializedName( "BitRate" )
   public String BitRate;

   @SerializedName( "Date" )
   public String Date;
   
   @SerializedName( "ThumbnailPath" )
   public String ThumbnailPath;

   @SerializedName( "VideoWidth" )
   public int VideoWidth;

   @SerializedName( "VideoHeight" )
   public int VideoHeight;

   public transient int CacheId = -1;

   private transient IsoFile mIsoFile;

   @Override
   public int compareTo( Video video ) {
      Log.d( "this", "Comparing path " + video.FilePath + " to " + FilePath );
      return video.FilePath.equals( FilePath ) ? 0 : -1;
   }

   public String getThumbnailFilePath( Context context ) {
      return context.getFilesDir().getAbsolutePath() + "/" + FileName + ".png";
   }

   public static Video CreateFromFilePath( Context context, String filePath ) {

      File file = new File( filePath );

      if ( !file.exists() ) {
         return null;
      }

      MediaMetadataRetriever retriever = new MediaMetadataRetriever();
      retriever.setDataSource( filePath );

      Video video = new Video();
      video.FilePath = filePath;
      video.FileName = file.getName();
      video.Format = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_TITLE );
      video.FileSize = Long.toString( file.length() );
      video.MimeType = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_MIMETYPE );
      video.Duration = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_DURATION );
      video.BitRate = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_BITRATE );
      video.Date = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_DATE );
      video.VideoWidth = Integer.parseInt( retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH ) );
      video.VideoHeight = Integer.parseInt( retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT ) );
      
      Bitmap bitmap = retriever.getFrameAtTime( Long.parseLong( video.Duration ) / 2 );

      try {
         FileOutputStream out = new FileOutputStream( video.getThumbnailFilePath( context ) );
         bitmap.compress( Bitmap.CompressFormat.PNG, 90, out );
         out.close();
      } catch ( Exception e ) {
         e.printStackTrace();
      }

      IsoFile isoFile = null;
      try {
         isoFile = new IsoFile( video.FilePath );<commit_msg>Wrap extraction of width and height in try catch (NumberFormatException), common issue apparently<commit_after>/**
   Copyright (c) 2014 <NAME>
   
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
   
       http://www.apache.org/licenses/LICENSE-2.0
   
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 **/

package com.roryhool.videoinfoviewer.data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.util.Log;

import com.coremedia.iso.IsoFile;
import com.coremedia.iso.boxes.Box;
import com.coremedia.iso.boxes.SampleSizeBox;
import com.google.gson.annotations.SerializedName;
import com.roryhool.videoinfoviewer.utils.BoxUtils;

public class Video implements Comparable<Video> {

   @SerializedName( "FileName" )
   public String FileName;

   @SerializedName( "FilePath" )
   public String FilePath;

   @SerializedName( "Format" )
   public String Format;

   @SerializedName( "FormatProfile" )
   public String FormatProfile;

   @SerializedName( "CodecID" )
   public String CodecID;

   @SerializedName( "FileSize" )
   public String FileSize;

   @SerializedName( "MimeType" )
   public String MimeType;

   @SerializedName( "FrameRate" )
   public String FrameRate;

   @SerializedName( "Duration" )
   public String Duration;

   @SerializedName( "BitRate" )
   public String BitRate;

   @SerializedName( "Date" )
   public String Date;
   
   @SerializedName( "ThumbnailPath" )
   public String ThumbnailPath;

   @SerializedName( "VideoWidth" )
   public int VideoWidth;

   @SerializedName( "VideoHeight" )
   public int VideoHeight;

   public transient int CacheId = -1;

   private transient IsoFile mIsoFile;

   @Override
   public int compareTo( Video video ) {
      Log.d( "this", "Comparing path " + video.FilePath + " to " + FilePath );
      return video.FilePath.equals( FilePath ) ? 0 : -1;
   }

   public String getThumbnailFilePath( Context context ) {
      return context.getFilesDir().getAbsolutePath() + "/" + FileName + ".png";
   }

   public static Video CreateFromFilePath( Context context, String filePath ) {

      File file = new File( filePath );

      if ( !file.exists() ) {
         return null;
      }

      MediaMetadataRetriever retriever = new MediaMetadataRetriever();
      retriever.setDataSource( filePath );

      Video video = new Video();
      video.FilePath = filePath;
      video.FileName = file.getName();
      video.Format = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_TITLE );
      video.FileSize = Long.toString( file.length() );
      video.MimeType = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_MIMETYPE );
      video.Duration = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_DURATION );
      video.BitRate = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_BITRATE );
      video.Date = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_DATE );
      try {
         video.VideoWidth = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
      } catch (NumberFormatException e) {

      }
      try {
         video.VideoHeight = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
      } catch (NumberFormatException e) {

      }
      
      Bitmap bitmap = retriever.getFrameAtTime( Long.parseLong( video.Duration ) / 2 );

      try {
         FileOutputStream out = new FileOutputStream( video.getThumbnailFilePath( context ) );
         bitmap.compress( Bitmap.CompressFormat.PNG, 90, out );
         out.close();
      } catch ( Exception e ) {
         e.printStackTrace();
      }

      IsoFile isoFile = null;
      try {
         isoFile = new IsoFile( video.FilePath );
{'fraction_non_alphanumeric': 0.09120248625388477, 'fraction_numerical': 0.0033468802295003584, 'mean_word_length': 3.520259319286872, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4554109', 'n_tokens_mistral': 2504, 'n_tokens_neox': 2416, 'n_words': 630}