Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim media file #82

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package com.github.khangnt.mcp.ui.presetcmd.aac

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.transition.Fade
import android.support.transition.Slide
import android.support.transition.TransitionManager
import android.support.transition.TransitionSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.github.khangnt.mcp.R
import com.github.khangnt.mcp.ui.presetcmd.ConvertFragment
import com.github.khangnt.mcp.ui.presetcmd.common.SingleInputOutputFragment
import com.github.khangnt.mcp.ui.presetcmd.common.TrimmerFragment
import com.github.khangnt.mcp.util.onSeekBarChanged
import com.github.khangnt.mcp.worker.ConverterService
import kotlinx.android.synthetic.main.fragment_convert_aac.*
Expand Down Expand Up @@ -38,6 +44,8 @@ class ConvertAacFragment : ConvertFragment() {
sbQuality.max = CBR_MAX - CBR_MIN
sbQuality.progress = CBR_RECOMMEND - CBR_MIN
sbQuality.onSeekBarChanged { updateQualityText() }

btnAdvancedToggle.setOnClickListener { toggleAdvanced() }
btnStartConversion.setOnClickListener { validateAndStartConversion() }
}

Expand All @@ -46,6 +54,21 @@ class ConvertAacFragment : ConvertFragment() {
updateQualityText()
}

private fun toggleAdvanced() {
val transition = TransitionSet()
transition.addTransition(Fade())
transition.addTransition(Slide(Gravity.TOP))
TransitionManager.beginDelayedTransition(advancedLayout, transition)

if (advancedLayout.visibility == View.GONE) {
advancedLayout.visibility = View.VISIBLE
btnAdvancedToggle.text = getString(R.string.advanced_arrow_up)
} else {
advancedLayout.visibility = View.GONE
btnAdvancedToggle.text = getString(R.string.advanced_arrow_down)
}
}

@SuppressLint("SetTextI18n")
private fun updateQualityText() {
tvQualityValue.text = "${sbQuality.progress + CBR_MIN} kbps"
Expand All @@ -56,23 +79,36 @@ class ConvertAacFragment : ConvertFragment() {
return fragment as SingleInputOutputFragment
}

private fun getTrimFragment(): TrimmerFragment {
val fragment = childFragmentManager.findFragmentById(R.id.fragmentTrimmer)
return fragment as TrimmerFragment
}

private fun validateAndStartConversion() {
getIoFragment().validateAndGetInputOutputData { inputOutputData ->
val cmdArgsBuilder = StringBuffer()
cmdArgsBuilder.append("-hide_banner -map 0:a -map_metadata 0:g -codec:a aac ")
.append("-b:a ${CBR_MIN + sbQuality.progress}k ")

ConverterService.newJob(
context!!,
title = inputOutputData.title,
inputs = listOf(inputOutputData.inputUri),
args = cmdArgsBuilder.toString(),
outputUri = inputOutputData.outputUri,
outputFormat = "ipod"
)

(activity as? OnSubmittedListener)?.onSubmitted(this)
?: Timber.w("Host activity does not implement OnSubmittedListener")
getTrimFragment().validateAndGetTrimConfig { TrimConfig ->
if (TrimConfig.isTrimmed) {
cmdArgsBuilder.append("-ss ${TrimConfig.startPos} -t ${TrimConfig.duration} ")
}

if (!TrimConfig.isError) {
ConverterService.newJob(
context!!,
title = inputOutputData.title,
inputs = listOf(inputOutputData.inputUri),
args = cmdArgsBuilder.toString(),
outputUri = inputOutputData.outputUri,
outputFormat = "ipod"
)

(activity as? OnSubmittedListener)?.onSubmitted(this)
?: Timber.w("Host activity does not implement OnSubmittedListener")
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.github.khangnt.mcp.ui.presetcmd.common

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import com.github.khangnt.mcp.R
import com.github.khangnt.mcp.ui.BaseFragment
import com.github.khangnt.mcp.util.openKeyboard
import kotlinx.android.synthetic.main.fragment_trimmer.*

/**
* Created by Simon Pham on 3/30/18.
* Email: [email protected]
*/

data class TrimConfig(val startPos: Float, val duration: Float, val isError: Boolean, val isTrimmed: Boolean)

class TrimmerFragment : BaseFragment() {

companion object {
private const val MAX_DURATION = "99999999"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.fragment_trimmer, container, false)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

btnMarkBegin.setOnClickListener {
edStartPos.setText("0")
}

btnMarkEnd.setOnClickListener {
edEndPos.setText(MAX_DURATION)
}
}

@SuppressLint("SetTextI18n")
fun validateAndGetTrimConfig(callback: (TrimConfig) -> Unit) {
// validate
if ((edStartPos.text.toString() == "" && edEndPos.text.toString() == "")) {
callback(TrimConfig(0.0f, 0.0f, false, false))
return
}

if (edStartPos.text.toString() == "" && edEndPos.text.toString() != "") {
edStartPos.setText("0")
}

if (edEndPos.text.toString() == "" && edStartPos.text.toString() != "") {
edEndPos.setText(MAX_DURATION)
}

if (edStartPos.text.toString().startsWith(".")) {
edStartPos.setText("0" + edStartPos.text)
}
if (edStartPos.text.toString().endsWith(".")) {
edStartPos.setText("${edStartPos.text}0")
}

if (edEndPos.text.toString().startsWith(".")) {
edEndPos.setText("0" + edEndPos.text)
}
if (edEndPos.text.toString().endsWith(".")) {
edEndPos.setText("${edEndPos.text}0")
}

val startPoint = edStartPos.text.toString().toFloat()
val duration = edEndPos.text.toString().toFloat() - startPoint

if (duration <= 0.0f) {
edEndPos.error = getString(R.string.error_invalid_position)
edEndPos.openKeyboard()
callback(TrimConfig(startPoint, duration, true, false))
return
}

callback(TrimConfig(startPoint, duration,false, true))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package com.github.khangnt.mcp.ui.presetcmd.flac

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.transition.Fade
import android.support.transition.Slide
import android.support.transition.TransitionManager
import android.support.transition.TransitionSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.github.khangnt.mcp.R
import com.github.khangnt.mcp.ui.presetcmd.ConvertFragment
import com.github.khangnt.mcp.ui.presetcmd.common.SingleInputOutputFragment
import com.github.khangnt.mcp.ui.presetcmd.common.TrimmerFragment
import com.github.khangnt.mcp.util.onSeekBarChanged
import com.github.khangnt.mcp.worker.ConverterService
import kotlinx.android.synthetic.main.fragment_convert_flac.*
Expand All @@ -30,6 +36,8 @@ class ConvertFlacFragment : ConvertFragment() {
super.onViewCreated(view, savedInstanceState)
getIoFragment().autoFillExt = "flac"
sbCompressionLevel.onSeekBarChanged { updateQualityText() }

btnAdvancedToggle.setOnClickListener { toggleAdvanced() }
btnStartConversion.setOnClickListener { validateAndStartConversion() }
}

Expand All @@ -38,6 +46,21 @@ class ConvertFlacFragment : ConvertFragment() {
updateQualityText()
}

private fun toggleAdvanced() {
val transition = TransitionSet()
transition.addTransition(Fade())
transition.addTransition(Slide(Gravity.TOP))
TransitionManager.beginDelayedTransition(advancedLayout, transition)

if (advancedLayout.visibility == View.GONE) {
advancedLayout.visibility = View.VISIBLE
btnAdvancedToggle.text = getString(R.string.advanced_arrow_up)
} else {
advancedLayout.visibility = View.GONE
btnAdvancedToggle.text = getString(R.string.advanced_arrow_down)
}
}

@SuppressLint("SetTextI18n")
private fun updateQualityText() {
tvCompressionLevel.text = "${sbCompressionLevel.progress}"
Expand All @@ -48,23 +71,37 @@ class ConvertFlacFragment : ConvertFragment() {
return fragment as SingleInputOutputFragment
}

private fun getTrimFragment(): TrimmerFragment {
val fragment = childFragmentManager.findFragmentById(R.id.fragmentTrimmer)
return fragment as TrimmerFragment
}

private fun validateAndStartConversion() {
getIoFragment().validateAndGetInputOutputData { inputOutputData ->
val cmdArgsBuilder = StringBuffer()
cmdArgsBuilder.append("-hide_banner -map 0:a -map_metadata 0:g -codec:a flac ")
.append("-compression_level ${sbCompressionLevel.progress} ")

ConverterService.newJob(
context!!,
title = inputOutputData.title,
inputs = listOf(inputOutputData.inputUri),
args = cmdArgsBuilder.toString(),
outputUri = inputOutputData.outputUri,
outputFormat = "flac"
)

(activity as? OnSubmittedListener)?.onSubmitted(this)
?: Timber.w("Host activity does not implement OnSubmittedListener")

getTrimFragment().validateAndGetTrimConfig { TrimConfig ->
if (TrimConfig.isTrimmed) {
cmdArgsBuilder.append("-ss ${TrimConfig.startPos} -t ${TrimConfig.duration} ")
}

if (!TrimConfig.isError) {
ConverterService.newJob(
context!!,
title = inputOutputData.title,
inputs = listOf(inputOutputData.inputUri),
args = cmdArgsBuilder.toString(),
outputUri = inputOutputData.outputUri,
outputFormat = "flac"
)

(activity as? OnSubmittedListener)?.onSubmitted(this)
?: Timber.w("Host activity does not implement OnSubmittedListener")
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package com.github.khangnt.mcp.ui.presetcmd.mp3

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.transition.Fade
import android.support.transition.Slide
import android.support.transition.TransitionManager
import android.support.transition.TransitionSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.github.khangnt.mcp.R
import com.github.khangnt.mcp.ui.presetcmd.ConvertFragment
import com.github.khangnt.mcp.ui.presetcmd.common.SingleInputOutputFragment
import com.github.khangnt.mcp.ui.presetcmd.common.TrimmerFragment
import com.github.khangnt.mcp.util.onItemSelected
import com.github.khangnt.mcp.util.onSeekBarChanged
import com.github.khangnt.mcp.worker.ConverterService
Expand Down Expand Up @@ -63,6 +69,8 @@ class ConvertMp3Fragment : ConvertFragment() {
}
}

btnAdvancedToggle.setOnClickListener { toggleAdvanced() }

btnStartConversion.setOnClickListener { validateAndStartConversion() }
}

Expand All @@ -71,6 +79,21 @@ class ConvertMp3Fragment : ConvertFragment() {
updateQualityText()
}

private fun toggleAdvanced() {
val transition = TransitionSet()
transition.addTransition(Fade())
transition.addTransition(Slide(Gravity.TOP))
TransitionManager.beginDelayedTransition(advancedLayout, transition)

if (advancedLayout.visibility == View.GONE) {
advancedLayout.visibility = View.VISIBLE
btnAdvancedToggle.text = getString(R.string.advanced_arrow_up)
} else {
advancedLayout.visibility = View.GONE
btnAdvancedToggle.text = getString(R.string.advanced_arrow_down)
}
}

@SuppressLint("SetTextI18n")
private fun updateQualityText() {
if (spinnerEncoder.selectedItemPosition == 0 && sbQuality.progress <= 9) {
Expand All @@ -85,6 +108,11 @@ class ConvertMp3Fragment : ConvertFragment() {
return fragment as SingleInputOutputFragment
}

private fun getTrimFragment(): TrimmerFragment {
val fragment = childFragmentManager.findFragmentById(R.id.fragmentTrimmer)
return fragment as TrimmerFragment
}

private fun validateAndStartConversion() {
getIoFragment().validateAndGetInputOutputData { inputOutputData ->
val cmdArgsBuilder = StringBuffer("-hide_banner -map 0:a -map_metadata 0:g -codec:a ")
Expand All @@ -94,17 +122,25 @@ class ConvertMp3Fragment : ConvertFragment() {
cmdArgsBuilder.append("libshine -b:a ${CBR_MIN + sbQuality.progress}k ")
}

ConverterService.newJob(
context!!,
title = inputOutputData.title,
inputs = listOf(inputOutputData.inputUri),
args = cmdArgsBuilder.toString(),
outputUri = inputOutputData.outputUri,
outputFormat = "mp3"
)

(activity as? OnSubmittedListener)?.onSubmitted(this)
?: Timber.w("Host activity does not implement OnSubmittedListener")
getTrimFragment().validateAndGetTrimConfig { TrimConfig ->
if (TrimConfig.isTrimmed) {
cmdArgsBuilder.append("-ss ${TrimConfig.startPos} -t ${TrimConfig.duration} ")
}

if (!TrimConfig.isError) {
ConverterService.newJob(
context!!,
title = inputOutputData.title,
inputs = listOf(inputOutputData.inputUri),
args = cmdArgsBuilder.toString(),
outputUri = inputOutputData.outputUri,
outputFormat = "mp3"
)

(activity as? OnSubmittedListener)?.onSubmitted(this)
?: Timber.w("Host activity does not implement OnSubmittedListener")
}
}
}
}

Expand Down
Loading