VideoHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. namespace Vinno.vCloud.FIS.CrossPlatform.Windows.LiveVideo
  2. {
  3. public class VideoHelper
  4. {
  5. /// <summary>
  6. /// 计算码率
  7. /// </summary>
  8. /// <param name="frameRate">帧率</param>
  9. /// <param name="width">宽度</param>
  10. /// <param name="height">高度</param>
  11. /// <returns></returns>
  12. public static int CalBitRate(int frameRate, int width, int height)
  13. {
  14. int kbit_rate = 2000;
  15. int area = width * height;
  16. if (area <= (320 * 300))
  17. {
  18. kbit_rate = 280;
  19. }
  20. else if (area <= (360 * 320))
  21. {
  22. kbit_rate = 360;
  23. }
  24. else if (area <= (640 * 480))
  25. {
  26. kbit_rate = 580;
  27. }
  28. else if (area <= (800 * 600))
  29. {
  30. kbit_rate = 620;
  31. }
  32. else if (area <= (900 * 700))
  33. {
  34. kbit_rate = 820;
  35. }
  36. else if (area <= (1280 * 720))
  37. {
  38. kbit_rate = 1600;
  39. }
  40. else if (area <= (1366 * 768))
  41. {
  42. kbit_rate = 2000;
  43. }
  44. else if (area <= (1600 * 900))
  45. {
  46. kbit_rate = 2300;
  47. }
  48. else if (area <= (1600 * 1050))
  49. {
  50. kbit_rate = 2500;
  51. }
  52. else
  53. {
  54. kbit_rate = 2800;
  55. }
  56. kbit_rate = kbit_rate * frameRate / 20;
  57. if (kbit_rate < 80)
  58. kbit_rate = 80;
  59. return kbit_rate;
  60. }
  61. /// <summary>
  62. /// 计算最大码率
  63. /// </summary>
  64. /// <param name="frameRate">帧率</param>
  65. /// <param name="width">宽度</param>
  66. /// <param name="height">高度</param>
  67. /// <param name="isVarBitrate">是否使用平均码率</param>
  68. /// <returns></returns>
  69. public static int CalMaxKBitRate(int frameRate, int width, int height, bool isVarBitrate)
  70. {
  71. int max_kbit_rate = 2000;
  72. int area = width * height;
  73. if (area <= (320 * 300))
  74. {
  75. max_kbit_rate = isVarBitrate ? 320 : 600;
  76. }
  77. else if (area <= (360 * 320))
  78. {
  79. max_kbit_rate = isVarBitrate ? 400 : 800;
  80. }
  81. else if (area <= (640 * 360))
  82. {
  83. max_kbit_rate = isVarBitrate ? 600 : 1000;
  84. }
  85. else if (area <= (640 * 480))
  86. {
  87. max_kbit_rate = isVarBitrate ? 680 : 1300;
  88. }
  89. else if (area <= (800 * 600))
  90. {
  91. max_kbit_rate = isVarBitrate ? 700 : 1500;
  92. }
  93. else if (area <= (900 * 700))
  94. {
  95. max_kbit_rate = isVarBitrate ? 920 : 2200;
  96. }
  97. else if (area <= (1280 * 720))
  98. {
  99. max_kbit_rate = isVarBitrate ? 1600 : 3000;
  100. }
  101. else if (area <= (1366 * 768))
  102. {
  103. max_kbit_rate = isVarBitrate ? 1700 : 3300;
  104. }
  105. else if (area <= (1600 * 900))
  106. {
  107. max_kbit_rate = isVarBitrate ? 2400 : 3400;
  108. }
  109. else if (area <= (1600 * 1050))
  110. {
  111. max_kbit_rate = isVarBitrate ? 2600 : 3600;
  112. }
  113. else if (area <= (1920 * 1080))
  114. {
  115. max_kbit_rate = isVarBitrate ? 2900 : 3800;
  116. }
  117. else
  118. {
  119. max_kbit_rate = isVarBitrate ? 3500 : 5500;
  120. }
  121. max_kbit_rate = max_kbit_rate * frameRate / 25;
  122. if (area <= (320 * 240))
  123. {
  124. if (max_kbit_rate < 150)
  125. max_kbit_rate = 150;
  126. }
  127. else if (area <= (640 * 480))
  128. {
  129. if (max_kbit_rate < 300)
  130. max_kbit_rate = 300;
  131. }
  132. else if (area <= (1280 * 720))
  133. {
  134. if (max_kbit_rate < 600)
  135. max_kbit_rate = 600;
  136. }
  137. else if (area <= (1920 * 1080))
  138. {
  139. if (max_kbit_rate < 960)
  140. max_kbit_rate = 960;
  141. }
  142. else
  143. {
  144. if (max_kbit_rate < 1500)
  145. max_kbit_rate = 1500;
  146. }
  147. return max_kbit_rate;
  148. }
  149. /// <summary>
  150. /// 计算视频质量
  151. /// </summary>
  152. /// <param name="width">宽度</param>
  153. /// <param name="height">高度</param>
  154. /// <param name="ish264">是否使用h264</param>
  155. /// <returns></returns>
  156. public static int CalVideoQuality(int width, int height, bool ish264)
  157. {
  158. int area = width * height;
  159. int quality = ish264 ? 23 : 28;
  160. if (area <= (320 * 240))
  161. {
  162. quality = ish264 ? 23 : 27;
  163. }
  164. else if (area <= (640 * 360))
  165. {
  166. quality = ish264 ? 25 : 28;
  167. }
  168. else if (area <= (640 * 480))
  169. {
  170. quality = ish264 ? 25 : 28;
  171. }
  172. else if (area <= (960 * 600))
  173. {
  174. quality = ish264 ? 26 : 28;
  175. }
  176. else if (area <= (1280 * 720))
  177. {
  178. quality = ish264 ? 27 : 29;
  179. }
  180. else if (area <= (1600 * 900))
  181. {
  182. quality = ish264 ? 28 : 30;
  183. }
  184. else if (area <= (1920 * 1080))
  185. {
  186. quality = ish264 ? 29 : 31;
  187. }
  188. else
  189. {
  190. quality = ish264 ? 30 : 32;
  191. }
  192. return quality;
  193. }
  194. /// <summary>
  195. /// 计算视频编码速度
  196. /// </summary>
  197. /// <param name="width">宽度</param>
  198. /// <param name="height">高度</param>
  199. /// <param name="ish264">是否使用h264</param>
  200. /// <returns></returns>
  201. public static int CalVideoEncoderSpeed(int width, int height, bool ish264)
  202. {
  203. if (ish264)
  204. return 3;
  205. int area = width * height;
  206. if (area <= (960 * 600))
  207. {
  208. return 3;
  209. }
  210. else if (area <= (1280 * 720))
  211. {
  212. return 2;
  213. }
  214. else
  215. {
  216. return 1;
  217. }
  218. }
  219. }
  220. }