原文發(fā)布于 我的博客Android猿
最近需要手機(jī)錄制gif圖片,找了好多軟件也沒理想的,最后確定了先通過命令screenrecord 錄制MP4視頻再轉(zhuǎn)gif的方案。分享出來和大家交流,誰有更好的方案請(qǐng)告訴我(嘖嘖,這像釣魚貼)。
之前沒注意到Android studio的Android Monitor選項(xiàng)卡有個(gè)錄制視頻的按鈕,經(jīng)@一息尚存和@Wing_Li提醒才發(fā)現(xiàn)還真有這么個(gè)東東(AS真人性化,有木有覺得?。H缦聢D

不習(xí)慣用命令方式的可以直接點(diǎn)擊那個(gè)按鈕錄制視頻。下面介紹命令的方式。
screenrecord 命令介紹
Android4.4(API level 19)以上支持screenrecord命令,首先我們?cè)贏ndroid studio中打開terminal

輸入
adb shell screenrecord --help
看到如下結(jié)果,詳細(xì)解釋請(qǐng)看我打的注釋
C:\Users\xialo\Desktop\WelcomPage>adb shell screenrecord --help
Usage: screenrecord [options] <filename>
Records the device's display to a .mp4 file.
Options:
--size WIDTHxHEIGHT
Set the video size, e.g. "1280x720". Default is the device's main
display resolution (if supported), 1280x720 if not. For best results,
use a size supported by the AVC encoder.
--bit-rate RATE
Set the video bit rate, in megabits per second. Default 4Mbps.
--time-limit TIME
Set the maximum recording time, in seconds. Default / maximum is 180.
--rotate
Rotate the output 90 degrees.
--verbose
Display interesting information on stdout.
--help
Show this message.
Recording continues until Ctrl-C is hit or the time limit is reached.
由上面我們看出,screenrecord基本的使用方式為:
screenrecord [options] <filename>
其中options是可選參數(shù),主要包含:--size,--bit-rate,--time-limit,--rotate,--verbose,--help等。下面簡單示例常用參數(shù)的使用:
--size 指定視頻分辨率大小
adb shell screenrecord --size 1280x720 /sdcard/test.mp4
說明:錄制視頻,分辨率為1280x720(注意,這里是字母x,不是星號(hào),否則會(huì)出錯(cuò)),如果不指定默認(rèn)使用手機(jī)的分辨率,為獲得最佳效果,請(qǐng)使用設(shè)備上的高級(jí)視頻編碼(AVC)支持的大小
--bit-rate 指定視頻的比特率
adb shell screenrecord --bit-rate 5000000 /sdcard/test.mp4
說明:指定視頻的比特率為5Mbps,如果不指定,默認(rèn)為4Mbps. 你可以增加比特率以提高視頻質(zhì)量或?yàn)榱俗屛募《档捅忍芈?/p>
--time-limit 限制錄制時(shí)間
adb shell screenrecord --time-limit 30 /sdcard/demo.mp4
說明:限制視頻錄制時(shí)間為30s,如果不限制,默認(rèn)180s
用screenrecord 命令錄制視頻
我們用USB鏈接手機(jī),繼續(xù)在Android studio的terminal中輸入命令
adb shell screenrecord --size 480x640 /sdcard/test.mp4
這里我們沒有指定錄制時(shí)間,所以錄制完成后點(diǎn)擊關(guān)閉terminal即可,這時(shí),我們的SD卡根目錄會(huì)出現(xiàn)剛剛錄制的視頻test.mp4

mp4轉(zhuǎn)gif
這里我們用http://ezgif.com/ 在線將mp4轉(zhuǎn)換為gif,傻瓜式操作,這里不再贅述。

收工。