Fix compressing and add a cancel to in progress conversions

This commit is contained in:
Isaac Shoebottom 2022-12-07 23:38:27 -04:00
parent 6e35ffc3cb
commit 24db0cef52
7 changed files with 119 additions and 46 deletions

View File

@ -43,7 +43,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'com.arthenica:ffmpeg-kit-full:5.1.LTS'
implementation 'com.arthenica:ffmpeg-kit-full-gpl:5.1.LTS'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

View File

@ -13,6 +13,8 @@ import android.provider.MediaStore
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
@ -22,22 +24,21 @@ import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.arthenica.ffmpegkit.FFmpegKit
import com.arthenica.ffmpegkit.FFmpegKitConfig
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityMainBinding
import ca.unb.lantau.ui.completed.CompletedAdapter
import ca.unb.lantau.ui.completed.CompletedItem
import ca.unb.lantau.ui.compressing.CompressingAdapter
import ca.unb.lantau.ui.compressing.CompressingItem
import ca.unb.lantau.ui.settings.SettingsViewModel
import com.arthenica.ffmpegkit.*
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView
import java.io.File
import java.util.*
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val settingsViewModel: SettingsViewModel by viewModels()
@ -144,9 +145,9 @@ class MainActivity : AppCompatActivity() {
Log.i("ethan", settingsViewModel.getSize().toString())
Log.i("ethan", fileSize.toString())
val bitrate = (settingsViewModel.getSize() * 1000000) / (duration!! / 1000)
val bitrate = (settingsViewModel.getSize() * 1_000_000) / (duration!! / 1_000)
val item = CompressingItem(fileName!!, 0.0, fileDate)
val item = CompressingItem(fileName!!, 0.0, fileDate, "Not started", ImageButton(this),null)
val outputFile = File(this.getExternalFilesDir(null), "converted_$fileName")
@ -154,27 +155,64 @@ class MainActivity : AppCompatActivity() {
val handler = Handler(Looper.getMainLooper())
val command = "-i $inUri -b:v $bitrate ${outputFile.absolutePath} -y"
Log.i("ethan", command)
val session = FFmpegKit.executeAsync(command) {
compressingItems.remove(item)
completedAdapter.refreshList(this)
val tempFile = File(this.cacheDir, "temp_$fileName")
val tempVideoFile = File(this.cacheDir, "tempVideo_$fileName")
val firstPass = "-i $inUri -codec:v libx264 -passlogfile ${tempFile.absolutePath} -preset veryfast -b:v $bitrate -maxrate $bitrate -minrate $bitrate -codec:a aac -pass 1 ${tempVideoFile.absolutePath} -y"
val secondPass = "-i ${tempVideoFile.absolutePath} -codec:v libx264 -passlogfile ${tempFile.absolutePath} -preset veryfast -b:v $bitrate -maxrate $bitrate -minrate $bitrate -codec:a aac -pass 2 ${outputFile.absolutePath} -y"
Log.i("Lantau", firstPass)
Log.i("Lantau", secondPass)
val firstPassSession = FFmpegKit.executeAsync(firstPass) { session ->
if (session.returnCode.isValueSuccess) {
val secondPassSession = FFmpegKit.executeAsync(secondPass) { session2 ->
if (session2.returnCode.isValueSuccess) {
handler.post {
handler.post {
Toast.makeText(
this,
applicationContext,
"Finished converting $fileName",
Toast.LENGTH_SHORT
).show()
compressingItems.remove(item)
compressingAdapter.notifyDataSetChanged()
completedAdapter.refreshList(applicationContext)
tempFile.delete()
tempVideoFile.delete()
}
}
} else {
handler.post {
Toast.makeText(
applicationContext,
"Failed to convert $fileName during second pass",
Toast.LENGTH_SHORT
).show()
compressingItems.remove(item)
compressingAdapter.notifyDataSetChanged()
}
}
Log.i("Tag", Arrays.deepToString(session.arguments))
Log.i("Tag", session.output)
}
item.status = "Second pass running"
item.session = secondPassSession
Log.i("Lantau", Arrays.deepToString(secondPassSession.arguments))
} else {
handler.post {
Toast.makeText(
applicationContext,
"Failed to convert $fileName during first pass",
Toast.LENGTH_SHORT
).show()
compressingItems.remove(item)
compressingAdapter.notifyDataSetChanged()
}
}
}
item.status = "First pass running"
item.session = firstPassSession
Log.i("Lantau", Arrays.deepToString(firstPassSession.arguments))
compressingAdapter.notifyDataSetChanged()
} else {
Toast.makeText(
@ -190,7 +228,7 @@ class MainActivity : AppCompatActivity() {
val compressingItems: MutableList<CompressingItem> = mutableListOf()
val compressingAdapter = CompressingAdapter(compressingItems)
val completedItems: MutableList<CompletedItem> = mutableListOf()
private val completedItems: MutableList<CompletedItem> = mutableListOf()
val completedAdapter = CompletedAdapter(completedItems)
}
}

View File

@ -3,6 +3,7 @@ package ca.unb.lantau.ui.compressing
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ProgressBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
@ -14,6 +15,8 @@ class CompressingAdapter(private val mCompressingItems: MutableList<CompressingI
val filename: TextView = itemView.findViewById(R.id.compressing_filename)
val date: TextView = itemView.findViewById(R.id.compressing_date)
val progress: ProgressBar = itemView.findViewById(R.id.compressing_progress)
val status: TextView = itemView.findViewById(R.id.compressing_status)
val cancelButton: ImageButton = itemView.findViewById(R.id.compressing_cancel)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@ -27,7 +30,12 @@ class CompressingAdapter(private val mCompressingItems: MutableList<CompressingI
holder.filename.text = compressingItem.filename
holder.date.text = compressingItem.date.toString()
holder.progress.progress = compressingItem.progress.toInt()
holder.progress.visibility = View.INVISIBLE
holder.status.text = compressingItem.status
holder.status.visibility = View.INVISIBLE
holder.cancelButton.setOnClickListener {
compressingItem.session!!.cancel()
}
}
override fun getItemCount(): Int {

View File

@ -1,5 +1,7 @@
package ca.unb.lantau.ui.compressing
import android.widget.ImageButton
import com.arthenica.ffmpegkit.FFmpegSession
import java.util.*
data class CompressingItem(val filename: String, val progress: Double, val date: Date)
data class CompressingItem(val filename: String, val progress: Double, val date: Date, var status: String, val cancelButton: ImageButton, var session: FFmpegSession?)

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2zM17,15.59L15.59,17 12,13.41 8.41,17 7,15.59 10.59,12 7,8.41 8.41,7 12,10.59 15.59,7 17,8.41 13.41,12 17,15.59z"/>
</vector>

View File

@ -1,20 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:id="@+id/compressing_filename"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/compressing_date"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/compressing_status"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
@ -22,5 +32,14 @@
style="@android:style/Widget.Holo.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageButton
android:id="@+id/compressing_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/ic_cancel"
android:contentDescription="@string/cancel" />
</LinearLayout>

View File

@ -14,4 +14,5 @@
<string name="youtube">Youtube</string>
<string name="share_button">Share</string>
<string name="delete">Delete</string>
<string name="cancel">Cancel</string>
</resources>