Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instagram upload video from pc

I'm trying to automate the process of uploading videos/images on instagram (without using a private API). For now i automated the image uploading and i'm trying to do the same thing for the videos. I'm doing this with electron and Nodejs.

for click the upload button and select an image I execute this code that actually works fine.

const fs = require('fs'),
      {remote} = require('electron'),
      clipboardy    = require('clipboardy'),
      BrowserWindow = remote.BrowserWindow;

const LOAD_IMAGE = '.UP43G',
      NEW_POST = '.glyphsSpriteNew_post__outline__24__grey_9.u-__7';

function get_files(path){
    return fs.readdirSync(path, { withFileTypes: true })
    .filter(dirent => dirent.isFile())
    .map(dirent => __dirname + '/../../' + path + '/' + dirent.name);
}

function randomRange(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

function createWindow (session_id, hidden) {
    win = new BrowserWindow({
        width: 500,
        height: 500
    });
    win.loadURL('https://www.instagram.com');
    return win;
}

////select the files to upload////

var files = UPLOAD_POST_FOLDER_CUSTOM
var file_to_upload = files[randomRange(0, files.length - 1)];

///////////////////////////////////////

function async upload_image(){
    // click the upload button on the page
    await electron_window.webContents.executeJavaScript(`
        async function click_upload_button(){
            let new_post_button = document.querySelector('${NEW_POST}');
            await sleep(1000);
            new_post_button.click()
        }
        click_upload_button();
    `);
    // write the path of the file and press enter in the file selector
    await sleep(500);
    let previous_clipboard = clipboardy.readSync();
    clipboardy.writeSync(file_to_upload);
    await fake_input.keyTap('l', 'control');
    await fake_input.keyTap('v', 'control');
    await fake_input.keyTap('enter');
    clipboardy.writeSync(previous_clipboard);       
    await sleep(2000);

}

This code works fine for images .jpg. The problem that i'm facing is that during the uploading, when it opens the file selector for choose something to post it doesn't recognize the videos. I tried all the possible video extensions.

I also tried to write the file path in the file selector instead select it manually and I saw that if u write a non .jpg/.mp4 file it show a warning only images are allowed, instead, if you write the path to a .jpg file it uploads the image and if you write a file to .mp4 it closes the file manager and do nothing, like that it ignores that you are trying to upload something.

To reproduce

  • go to instagram
  • do the login
  • click F12 for open the dev tools
  • click CTRL + SHIFT + M for toggle the device emulation
  • select any device or resize the page for toggle the mobile view of the site
  • reload the site
  • try to upload something by clicking the bottom + button.

(The video is 6mb (< 15mb that is the maximum) and 40seconds (<60s that is the maximum)

like image 818
Mat.C Avatar asked Jan 06 '20 08:01

Mat.C


1 Answers

If you want to publish a video via the API, you have to do the following two steps:

  • create a media object container by using the POST /{ig-user-id}/media endpoint
  • publish the container with POST /{ig-user-id}/media_publish

Example Request:

POST graph.facebook.com/89751400008469714/media
  ?media_type=VIDEO
  &video_url=https//www.example.com/videos/my-video.mov
  &caption=#myfirstvideo!

Try using a .mov file. If this doesn´t fix the problem, you can check if your video has the following characteristics:

  • 3,500 kbps video bitrate
  • H.264 codec / MP4 format
  • 3 to 60 seconds duration
  • maximum width 1080 pixels (1080P)
  • 29.96 frames per second
  • AAC audio codec at 44.1 kHz mono
like image 71
muellerra Avatar answered Nov 13 '22 05:11

muellerra