parent
21fdc22351
commit
4d740446a1
@ -1,152 +0,0 @@ |
|||||||
package com.frank.ffmpeg.tool; |
|
||||||
|
|
||||||
import android.text.TextUtils; |
|
||||||
import android.util.Log; |
|
||||||
|
|
||||||
import com.frank.ffmpeg.model.AudioBean; |
|
||||||
import com.frank.ffmpeg.model.MediaBean; |
|
||||||
import com.frank.ffmpeg.model.VideoBean; |
|
||||||
|
|
||||||
import org.json.JSONArray; |
|
||||||
import org.json.JSONObject; |
|
||||||
|
|
||||||
/** |
|
||||||
* the tool of parsing json |
|
||||||
* Created by frank on 2020/1/8. |
|
||||||
*/ |
|
||||||
public class JsonParseTool { |
|
||||||
|
|
||||||
private final static String TAG = JsonParseTool.class.getSimpleName(); |
|
||||||
|
|
||||||
private final static String TYPE_VIDEO = "video"; |
|
||||||
|
|
||||||
private final static String TYPE_AUDIO = "audio"; |
|
||||||
|
|
||||||
public static MediaBean parseMediaFormat(String mediaFormat) { |
|
||||||
if (mediaFormat == null || mediaFormat.isEmpty()) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
MediaBean mediaBean = null; |
|
||||||
try { |
|
||||||
JSONObject jsonMedia = new JSONObject(mediaFormat); |
|
||||||
JSONObject jsonMediaFormat = jsonMedia.getJSONObject("format"); |
|
||||||
mediaBean = new MediaBean(); |
|
||||||
int streamNum = jsonMediaFormat.optInt("nb_streams"); |
|
||||||
mediaBean.setStreamNum(streamNum); |
|
||||||
Log.e(TAG, "streamNum=" + streamNum); |
|
||||||
String formatName = jsonMediaFormat.optString("format_name"); |
|
||||||
mediaBean.setFormatName(formatName); |
|
||||||
Log.e(TAG, "formatName=" + formatName); |
|
||||||
String bitRateStr = jsonMediaFormat.optString("bit_rate"); |
|
||||||
if (!TextUtils.isEmpty(bitRateStr)) { |
|
||||||
mediaBean.setBitRate(Integer.valueOf(bitRateStr)); |
|
||||||
} |
|
||||||
Log.e(TAG, "bitRate=" + bitRateStr); |
|
||||||
String sizeStr = jsonMediaFormat.optString("size"); |
|
||||||
if (!TextUtils.isEmpty(sizeStr)) { |
|
||||||
mediaBean.setSize(Long.valueOf(sizeStr)); |
|
||||||
} |
|
||||||
Log.e(TAG, "size=" + sizeStr); |
|
||||||
String durationStr = jsonMediaFormat.optString("duration"); |
|
||||||
if (!TextUtils.isEmpty(durationStr)) { |
|
||||||
float duration = Float.valueOf(durationStr); |
|
||||||
mediaBean.setDuration((long) duration); |
|
||||||
} |
|
||||||
|
|
||||||
JSONArray jsonMediaStream = jsonMedia.getJSONArray("streams"); |
|
||||||
if (jsonMediaStream == null) { |
|
||||||
return mediaBean; |
|
||||||
} |
|
||||||
for (int index = 0; index < jsonMediaStream.length(); index ++) { |
|
||||||
JSONObject jsonMediaStreamItem = jsonMediaStream.optJSONObject(index); |
|
||||||
if (jsonMediaStreamItem == null) continue; |
|
||||||
String codecType = jsonMediaStreamItem.optString("codec_type"); |
|
||||||
if (codecType == null) continue; |
|
||||||
if (codecType.equals(TYPE_VIDEO)) { |
|
||||||
VideoBean videoBean = new VideoBean(); |
|
||||||
mediaBean.setVideoBean(videoBean); |
|
||||||
String codecName = jsonMediaStreamItem.optString("codec_tag_string"); |
|
||||||
videoBean.setVideoCodec(codecName); |
|
||||||
Log.e(TAG, "codecName=" + codecName); |
|
||||||
int width = jsonMediaStreamItem.optInt("width"); |
|
||||||
videoBean.setWidth(width); |
|
||||||
int height = jsonMediaStreamItem.optInt("height"); |
|
||||||
videoBean.setHeight(height); |
|
||||||
Log.e(TAG, "width=" + width + "--height=" + height); |
|
||||||
String aspectRatio = jsonMediaStreamItem.optString("display_aspect_ratio"); |
|
||||||
videoBean.setDisplayAspectRatio(aspectRatio); |
|
||||||
Log.e(TAG, "aspectRatio=" + aspectRatio); |
|
||||||
String pixelFormat = jsonMediaStreamItem.optString("pix_fmt"); |
|
||||||
videoBean.setPixelFormat(pixelFormat); |
|
||||||
Log.e(TAG, "pixelFormat=" +pixelFormat); |
|
||||||
String profile = jsonMediaStreamItem.optString("profile"); |
|
||||||
videoBean.setProfile(profile); |
|
||||||
int level = jsonMediaStreamItem.optInt("level"); |
|
||||||
videoBean.setLevel(level); |
|
||||||
Log.e(TAG, "profile=" + profile + "--level=" + level); |
|
||||||
String frameRateStr = jsonMediaStreamItem.optString("r_frame_rate"); |
|
||||||
if (!TextUtils.isEmpty(frameRateStr)) { |
|
||||||
String[] frameRateArray = frameRateStr.split("/"); |
|
||||||
double frameRate = Math.ceil(Double.valueOf(frameRateArray[0]) / Double.valueOf(frameRateArray[1])); |
|
||||||
Log.e(TAG, "frameRate=" + (int) frameRate); |
|
||||||
videoBean.setFrameRate((int) frameRate); |
|
||||||
} |
|
||||||
} else if (codecType.equals(TYPE_AUDIO)) { |
|
||||||
AudioBean audioBean = new AudioBean(); |
|
||||||
mediaBean.setAudioBean(audioBean); |
|
||||||
String codecName = jsonMediaStreamItem.optString("codec_tag_string"); |
|
||||||
audioBean.setAudioCodec(codecName); |
|
||||||
Log.e(TAG, "codecName=" + codecName); |
|
||||||
String sampleRateStr = jsonMediaStreamItem.optString("sample_rate"); |
|
||||||
if (!TextUtils.isEmpty(sampleRateStr)) { |
|
||||||
audioBean.setSampleRate(Integer.valueOf(sampleRateStr)); |
|
||||||
} |
|
||||||
Log.e(TAG, "sampleRate=" + sampleRateStr); |
|
||||||
int channels = jsonMediaStreamItem.optInt("channels"); |
|
||||||
audioBean.setChannels(channels); |
|
||||||
Log.e(TAG, "channels=" + channels); |
|
||||||
String channelLayout = jsonMediaStreamItem.optString("channel_layout"); |
|
||||||
audioBean.setChannelLayout(channelLayout); |
|
||||||
Log.e(TAG, "channelLayout=" + channelLayout); |
|
||||||
} |
|
||||||
} |
|
||||||
} catch (Exception e) { |
|
||||||
Log.e(TAG, "parse error=" + e.toString()); |
|
||||||
} |
|
||||||
return mediaBean; |
|
||||||
} |
|
||||||
|
|
||||||
public static String stringFormat(MediaBean mediaBean) { |
|
||||||
if (mediaBean == null) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
StringBuilder formatBuilder = new StringBuilder(); |
|
||||||
formatBuilder.append("duration:").append(mediaBean.getDuration()).append("\n"); |
|
||||||
formatBuilder.append("size:").append(mediaBean.getSize()).append("\n"); |
|
||||||
formatBuilder.append("bitRate:").append(mediaBean.getBitRate()).append("\n"); |
|
||||||
formatBuilder.append("formatName:").append(mediaBean.getFormatName()).append("\n"); |
|
||||||
formatBuilder.append("streamNum:").append(mediaBean.getStreamNum()).append("\n"); |
|
||||||
if (mediaBean.getVideoBean() != null) { |
|
||||||
VideoBean videoBean = mediaBean.getVideoBean(); |
|
||||||
formatBuilder.append("width:").append(videoBean.getWidth()).append("\n"); |
|
||||||
formatBuilder.append("height:").append(videoBean.getHeight()).append("\n"); |
|
||||||
formatBuilder.append("aspectRatio:").append(videoBean.getDisplayAspectRatio()).append("\n"); |
|
||||||
formatBuilder.append("pixelFormat:").append(videoBean.getPixelFormat()).append("\n"); |
|
||||||
formatBuilder.append("frameRate:").append(videoBean.getFrameRate()).append("\n"); |
|
||||||
if (videoBean.getVideoCodec() != null) { |
|
||||||
formatBuilder.append("videoCodec:").append(videoBean.getVideoCodec()).append("\n"); |
|
||||||
} |
|
||||||
} |
|
||||||
if (mediaBean.getAudioBean() != null) { |
|
||||||
AudioBean audioBean = mediaBean.getAudioBean(); |
|
||||||
formatBuilder.append("sampleRate:").append(audioBean.getSampleRate()).append("\n"); |
|
||||||
formatBuilder.append("channels:").append(audioBean.getChannels()).append("\n"); |
|
||||||
formatBuilder.append("channelLayout:").append(audioBean.getChannelLayout()).append("\n"); |
|
||||||
if (audioBean.getAudioCodec() != null) { |
|
||||||
formatBuilder.append("audioCodec:").append(audioBean.getAudioCodec()).append("\n"); |
|
||||||
} |
|
||||||
} |
|
||||||
return formatBuilder.toString(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,147 @@ |
|||||||
|
package com.frank.ffmpeg.tool |
||||||
|
|
||||||
|
import android.text.TextUtils |
||||||
|
import android.util.Log |
||||||
|
|
||||||
|
import com.frank.ffmpeg.model.AudioBean |
||||||
|
import com.frank.ffmpeg.model.MediaBean |
||||||
|
import com.frank.ffmpeg.model.VideoBean |
||||||
|
|
||||||
|
import org.json.JSONObject |
||||||
|
|
||||||
|
/** |
||||||
|
* the tool of parsing json |
||||||
|
* Created by frank on 2020/1/8. |
||||||
|
*/ |
||||||
|
object JsonParseTool { |
||||||
|
|
||||||
|
private val TAG = JsonParseTool::class.java.simpleName |
||||||
|
|
||||||
|
private const val TYPE_VIDEO = "video" |
||||||
|
|
||||||
|
private const val TYPE_AUDIO = "audio" |
||||||
|
|
||||||
|
fun parseMediaFormat(mediaFormat: String?): MediaBean? { |
||||||
|
if (mediaFormat == null || mediaFormat.isEmpty()) { |
||||||
|
return null |
||||||
|
} |
||||||
|
var mediaBean: MediaBean? = null |
||||||
|
try { |
||||||
|
val jsonMedia = JSONObject(mediaFormat) |
||||||
|
val jsonMediaFormat = jsonMedia.getJSONObject("format") |
||||||
|
mediaBean = MediaBean() |
||||||
|
val streamNum = jsonMediaFormat.optInt("nb_streams") |
||||||
|
mediaBean.streamNum = streamNum |
||||||
|
Log.e(TAG, "streamNum=$streamNum") |
||||||
|
val formatName = jsonMediaFormat.optString("format_name") |
||||||
|
mediaBean.formatName = formatName |
||||||
|
Log.e(TAG, "formatName=$formatName") |
||||||
|
val bitRateStr = jsonMediaFormat.optString("bit_rate") |
||||||
|
if (!TextUtils.isEmpty(bitRateStr)) { |
||||||
|
mediaBean.bitRate = Integer.valueOf(bitRateStr) |
||||||
|
} |
||||||
|
Log.e(TAG, "bitRate=$bitRateStr") |
||||||
|
val sizeStr = jsonMediaFormat.optString("size") |
||||||
|
if (!TextUtils.isEmpty(sizeStr)) { |
||||||
|
mediaBean.size = java.lang.Long.valueOf(sizeStr) |
||||||
|
} |
||||||
|
Log.e(TAG, "size=$sizeStr") |
||||||
|
val durationStr = jsonMediaFormat.optString("duration") |
||||||
|
if (!TextUtils.isEmpty(durationStr)) { |
||||||
|
val duration = java.lang.Float.valueOf(durationStr) |
||||||
|
mediaBean.duration = duration.toLong() |
||||||
|
} |
||||||
|
|
||||||
|
val jsonMediaStream = jsonMedia.getJSONArray("streams") ?: return mediaBean |
||||||
|
for (index in 0 until jsonMediaStream.length()) { |
||||||
|
val jsonMediaStreamItem = jsonMediaStream.optJSONObject(index) ?: continue |
||||||
|
val codecType = jsonMediaStreamItem.optString("codec_type") ?: continue |
||||||
|
if (codecType == TYPE_VIDEO) { |
||||||
|
val videoBean = VideoBean() |
||||||
|
mediaBean.videoBean = videoBean |
||||||
|
val codecName = jsonMediaStreamItem.optString("codec_tag_string") |
||||||
|
videoBean.videoCodec = codecName |
||||||
|
Log.e(TAG, "codecName=$codecName") |
||||||
|
val width = jsonMediaStreamItem.optInt("width") |
||||||
|
videoBean.width = width |
||||||
|
val height = jsonMediaStreamItem.optInt("height") |
||||||
|
videoBean.height = height |
||||||
|
Log.e(TAG, "width=$width--height=$height") |
||||||
|
val aspectRatio = jsonMediaStreamItem.optString("display_aspect_ratio") |
||||||
|
videoBean.displayAspectRatio = aspectRatio |
||||||
|
Log.e(TAG, "aspectRatio=$aspectRatio") |
||||||
|
val pixelFormat = jsonMediaStreamItem.optString("pix_fmt") |
||||||
|
videoBean.pixelFormat = pixelFormat |
||||||
|
Log.e(TAG, "pixelFormat=$pixelFormat") |
||||||
|
val profile = jsonMediaStreamItem.optString("profile") |
||||||
|
videoBean.profile = profile |
||||||
|
val level = jsonMediaStreamItem.optInt("level") |
||||||
|
videoBean.level = level |
||||||
|
Log.e(TAG, "profile=$profile--level=$level") |
||||||
|
val frameRateStr = jsonMediaStreamItem.optString("r_frame_rate") |
||||||
|
if (!TextUtils.isEmpty(frameRateStr)) { |
||||||
|
val frameRateArray = frameRateStr.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() |
||||||
|
val frameRate = Math.ceil(java.lang.Double.valueOf(frameRateArray[0]) / java.lang.Double.valueOf(frameRateArray[1])) |
||||||
|
Log.e(TAG, "frameRate=" + frameRate.toInt()) |
||||||
|
videoBean.frameRate = frameRate.toInt() |
||||||
|
} |
||||||
|
} else if (codecType == TYPE_AUDIO) { |
||||||
|
val audioBean = AudioBean() |
||||||
|
mediaBean.audioBean = audioBean |
||||||
|
val codecName = jsonMediaStreamItem.optString("codec_tag_string") |
||||||
|
audioBean.audioCodec = codecName |
||||||
|
Log.e(TAG, "codecName=$codecName") |
||||||
|
val sampleRateStr = jsonMediaStreamItem.optString("sample_rate") |
||||||
|
if (!TextUtils.isEmpty(sampleRateStr)) { |
||||||
|
audioBean.sampleRate = Integer.valueOf(sampleRateStr) |
||||||
|
} |
||||||
|
Log.e(TAG, "sampleRate=$sampleRateStr") |
||||||
|
val channels = jsonMediaStreamItem.optInt("channels") |
||||||
|
audioBean.channels = channels |
||||||
|
Log.e(TAG, "channels=$channels") |
||||||
|
val channelLayout = jsonMediaStreamItem.optString("channel_layout") |
||||||
|
audioBean.channelLayout = channelLayout |
||||||
|
Log.e(TAG, "channelLayout=$channelLayout") |
||||||
|
} |
||||||
|
} |
||||||
|
} catch (e: Exception) { |
||||||
|
Log.e(TAG, "parse error=$e") |
||||||
|
} |
||||||
|
|
||||||
|
return mediaBean |
||||||
|
} |
||||||
|
|
||||||
|
fun stringFormat(mediaBean: MediaBean?): String? { |
||||||
|
if (mediaBean == null) { |
||||||
|
return null |
||||||
|
} |
||||||
|
val formatBuilder = StringBuilder() |
||||||
|
formatBuilder.append("duration:").append(mediaBean.duration).append("\n") |
||||||
|
formatBuilder.append("size:").append(mediaBean.size).append("\n") |
||||||
|
formatBuilder.append("bitRate:").append(mediaBean.bitRate).append("\n") |
||||||
|
formatBuilder.append("formatName:").append(mediaBean.formatName).append("\n") |
||||||
|
formatBuilder.append("streamNum:").append(mediaBean.streamNum).append("\n") |
||||||
|
if (mediaBean.videoBean != null) { |
||||||
|
val videoBean = mediaBean.videoBean |
||||||
|
formatBuilder.append("width:").append(videoBean!!.width).append("\n") |
||||||
|
formatBuilder.append("height:").append(videoBean.height).append("\n") |
||||||
|
formatBuilder.append("aspectRatio:").append(videoBean.displayAspectRatio).append("\n") |
||||||
|
formatBuilder.append("pixelFormat:").append(videoBean.pixelFormat).append("\n") |
||||||
|
formatBuilder.append("frameRate:").append(videoBean.frameRate).append("\n") |
||||||
|
if (videoBean.videoCodec != null) { |
||||||
|
formatBuilder.append("videoCodec:").append(videoBean.videoCodec).append("\n") |
||||||
|
} |
||||||
|
} |
||||||
|
if (mediaBean.audioBean != null) { |
||||||
|
val audioBean = mediaBean.audioBean |
||||||
|
formatBuilder.append("sampleRate:").append(audioBean!!.sampleRate).append("\n") |
||||||
|
formatBuilder.append("channels:").append(audioBean.channels).append("\n") |
||||||
|
formatBuilder.append("channelLayout:").append(audioBean.channelLayout).append("\n") |
||||||
|
if (audioBean.audioCodec != null) { |
||||||
|
formatBuilder.append("audioCodec:").append(audioBean.audioCodec).append("\n") |
||||||
|
} |
||||||
|
} |
||||||
|
return formatBuilder.toString() |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue