javascript - Instagram 从电脑上传视频

标签 javascript node.js electron instagram

我正在尝试自动化在 Instagram 上上传视频/图像的过程(不使用私有(private) API)。现在我自动上传图片,我正在尝试为视频做同样的事情。我正在使用 electronNodejs .

单击上传按钮并选择一个图像,我执行此代码实际上可以正常工作。

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);

}


此代码适用于图像 .jpg .我面临的问题是,在上传过程中,当它打开文件选择器以选择要发布的内容时 它无法识别视频 .我尝试了所有可能的视频扩展。

我还尝试在文件选择器中写入文件路径,而不是手动选择它,我看到如果你写一个非 .jpg/.mp4如果您将路径写入 .jpg,它会显示仅允许使用图像的警告。 file 它会上传图像,如果您将文件写入 .mp4它关闭文件管理器并且什么也不做,就像它忽略了你正在尝试上传的东西一样。

转载
  • 转到 Instagram
  • 登录
  • 点击F12用于打开开发工具
  • 点击CTRL + SHIFT + M用于切换设备仿真
  • 选择任何设备或调整页面大小以切换网站的移动 View
  • 重新加载网站
  • 尝试通过单击底部+ 上传内容按钮。

  • (视频为 6mb(< 15mb 为最大值)和 40 秒(<60s 为最大值)

    最佳答案

    如果您想通过 API 发布视频,您必须执行以下两个步骤:

  • 使用 POST /{ig-user-id}/media 创建媒体对象容器端点
  • 使用 POST /{ig-user-id}/media_publish 发布容器

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

    尝试使用 .mov 文件。如果这不能解决问题,您可以检查您的视频是否具有以下特征:
  • 3,500 kbps 视频比特率
  • H.264 编解码器/MP4 格式
  • 3 到 60 秒的持续时间
  • 最大宽度 1080 像素 (1080P)
  • 每秒 29.96 帧
  • 44.1 kHz 单声道 AAC 音频编解码器
  • 关于javascript - Instagram 从电脑上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59609003/

    相关文章:

    javascript - 类型错误 : $ is not a function WordPress

    javascript - 使用 jquery.inputmask 插件验证 parsley.js

    javascript - 意外 token = 用于 Node 8.4 中的类属性

    reactjs - Centos 6 的 Electron/nw.js 的 Web 运行时替代品?

    javascript - Angular JS : HTML in $scope throws jshint error?

    javascript - Buffer + writeUInt32LE 从 NodeJS 到 Javascript

    node.js - NodeJS/ExpressJS 上的 Sequelize 在运行 CLI 命令 db :migrate 时返回错误

    javascript - Sails.JS 或 Electron 应用程序的 Loopback

    electron - 不能通过TypeScript Create Electro App使用Electron中的 `require`或预加载脚本

    javascript - HTML/JS - 创建 "double-level"的 anchor