Friday, 29 September 2017

制造 fb live 时光机







如视频所示, 我可以下载打开 fb 直播之前的 15 分钟剧情 (从少林足球比赛进行中, 倒退 15 分钟 , 能看到刚刚开始集合的剧情。)

有时你打开直播, miss 掉之前重要的画面, 直播后也可能被隐藏或删除, 所以这招很有用。

Bash代码:


# rf: https://stackoverflow.com/questions/12498304/using-bash-to-display-a-progress-working-indicator
progressBarWidth=20
# Function to draw progress bar
progressBar () {

  taskCount="$1"
  tasksDone="$2"
  # Calculate number of fill/empty slots in the bar
  progress=$(echo "$progressBarWidth/$taskCount*$tasksDone" | bc -l)  
  fill=$(printf "%.0f\n" $progress)
  if [ $fill -gt $progressBarWidth ]; then
    fill=$progressBarWidth
  fi
  empty=$(($fill-$progressBarWidth))

  # Percentage Calculation
  percent=$(echo "100/$taskCount*$tasksDone" | bc -l)
  percent=$(printf "%0.2f\n" $percent)
  if [ $(echo "$percent>100" | bc) -gt 0 ]; then
    percent="100.00"
  fi

  # Output to screen
  printf "\r["
  printf "%${fill}s" '' | tr ' ' '#'
  printf "%${empty}s" '' | tr ' ' ' '
  printf "] $percent%% - $text "
}

fb_live_backward () 
{ 
    echo lili;
    ##no nid care if `firefox -no-remote -ProfileManager` then create lolo.default collision, overkill
    cookie_f='/tmp/fb_cookie';
    printf ".mode tabs \n
select host, case when host glob '.*' then 'TRUE' else 'FALSE' end, path, case when isSecure then 'TRUE' else 'FALSE' end, expiry, name, value
from moz_cookies where host = '.facebook.com';" | sqlite3 ~/.mozilla/firefox/*.default/cookies.sqlite > "$cookie_f";
    echo "$cookie_f";
    #echo > "$cookie_f";
    #curl -b "$cookie_f" -vLk "https://www.facebook.com/video/playback/playlist.m3u8?v=$vid" -H 'Host: www.facebook.com' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://www.facebook.com/live' -H 'Connection: keep-alive'
    #curl -b "$cookie_f" -vLk 'https://video.fkul8-1.fna.fbcdn.net/hvideo-prn1/v/rvgvfRr3mfgtrG9-5YYe0/live-md/320741391732419.m3u8?_nc_rl=AfACJUPO37BnG6-k&oh=ca49b289cfe55cc22554f06974a11197&oe=597BC9DD'
    vid="$1";
    url="$(curl -b "$cookie_f" -s "https://www.facebook.com/video/playback/playlist.m3u8?v=$vid" -H 'Host: www.facebook.com' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://www.facebook.com/live' -H 'Connection: keep-alive'  | tail -1)";
    if [[ $url != http* ]]; then
        echo "url failed";
    else
        curl -b "$cookie_f" -s "$url" | awk '/^#EXTINF/,/ts$/' > /tmp/tss;
        awk 'NR==2' /tmp/tss > /tmp/ts;
        awk 'NR==1 {first = $0} END {print}' /tmp/tss > /tmp/ts2;
        endI2="$(awk 'NR > 1 {print $1}' RS='__-' FS='.ts' /tmp/ts2 | tail -1)";
        endI="$(awk 'NR > 1 {print $1}' RS='__-' FS='.ts' /tmp/ts |  tail -1)";
        echo '###';
        echo "$endI";
        echo "$endI2";
        echo '#####';
        if [[ -z "$endI" || -z "$endI2" ]]; then
            echo "Abort.";
        else
            startI=$(($endI - 300)); #Backward up to 15 minutes * 60 seconds 
            if [ "$startI" -lt "0" ]; then
                startI=0;
            fi;
        fi;
        furl="$(awk -F'__-' '{print $1}' /tmp/ts)";
        startURL="${url%/*}";
        endURL="$(echo "${url##*/}" | awk -F? '{print $NF}')";
        rm /tmp/$vid.mp4 2> /dev/null;
        for n in `seq "$startI" "$endI2"`;
        do
            text=$(echo "Saving to /tmp/$vid.mp4 ..." `ls -lah /tmp/$vid.mp4 2>/dev/null | awk -F " " {'print $5'}` );
            progressBar $(($endI2 - $startI)) $(($n - $startI)) "$text";
            curl -b "$cookie_f" -s "$startURL"'/'"$furl"'__-'"$n"'.ts?'"$endURL" -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://www.facebook.com/live' -H 'Connection: keep-alive' >> /tmp/$vid.mp4;
        done;
        echo "Download completed :)";
    fi
}



我的一些相关笔记:




虽然这 2017 年的招已经 deprecated, 我也没时间研究 fb 新招, 不过希望可以带给你一点 idea 关于倒退直播的可能性 :D