我正在编写一个应用程序,使用FFMPEG
修剪android中的视频。我需要像FFMPEG
那样以时间单位给00:00:10
赋值。目前我有一个videoview
和两个buttons
。第一个按钮是startTime
,第二个按钮是endTime
。当点击按钮时,我使用getCurrentPosition
来获取视频的当前位置。我也有一个MediaController
附加到我的videoview
。
现在我面临的问题是,我得到当前位置的int
值,我无法传递给FFMPEG
,我如何准确地将其转换为以时间单位获取值,以便我可以将其传递给FFMPEG
来修剪视频。我已经给出了以下代码供您参考。有没有其他方法来获得除此之外的当前时间。提前致谢。
tVideoView = (VideoView) findViewById(R.id.tVideoView);
startBtn = (Button) findViewById(R.id.tStartBtn);
endBtn = (Button) findViewById(R.id.tEndBtn);
trimBtn = (Button) findViewById(R.id.trimBtn);
tVideoView.setVideoPath(videoPath);
duration = tVideoView.getDuration();
mMedia = new MediaController(this);
mMedia.setMediaPlayer(tVideoView);
mMedia.setAnchorView(tVideoView);
tVideoView.setMediaController(mMedia);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTime = tVideoView.getCurrentPosition();
startBtn.setText(String.valueOf(startTime));
}
});
endBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
endTime = tVideoView.getCurrentPosition();
endBtn.setText(String.valueOf(endTime));
}
});
将毫秒转换为时间格式的功能
String getVideoTime(long ms)
{
ms/=1000;
return (ms/3600)+":"+((ms%3600)/60)+":"+((ms%3600)%60);
}
并在您的runnable或计时器中调用它来更新UI
String tm = getVideoTime(
videoView.getCurrentPosition()) +
" / " +
getVideoTimeFormat(videoView.getCurrentPosition()
);
tvVideoTime.setText(tm);
您可以使用简单方法以Date格式获取毫秒时间,
/**
* Return date in specified format.
* @param milliSeconds Date in milliseconds
* @param dateFormat Date format
* @return String representing date in specified format
*/
public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
要调用该方法,
String time = (getDate(12184, "hh:mm:ss"));
这将返回标准时间格式,如06:00:12