英文:
Instagram upload video from pc
问题
我明白你的需求,以下是代码部分的中文翻译:
我正在尝试自动化上传Instagram上的视频/图片的过程(不使用私有API)。目前,我已经自动化了图片上传,现在我正在尝试对视频做同样的事情。我是使用 `electron` 和 `Nodejs` 来实现的。
对于点击上传按钮并选择图片,我执行以下代码,它实际上运行良好。
```javascript
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;
}
////选择要上传的文件////
var files = UPLOAD_POST_FOLDER_CUSTOM
var file_to_upload = files[randomRange(0, files.length - 1)];
///////////////////////////////////////
async function upload_image(){
// 点击页面上的上传按钮
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();
`);
// 在文件选择器中写入文件路径并按回车键
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
文件路径,它会上传该图片,如果写入 .mp4
文件路径,它会关闭文件管理器并什么都不做,就好像它忽略了你要上传的内容。
复现步骤
- 转到Instagram网站
- 登录
- 按
F12
打开开发工具 - 按
CTRL + SHIFT + M
切换设备模拟 - 选择任何设备或调整页面大小以切换到移动视图
- 重新加载网站
- 尝试通过点击底部的
+
按钮上传内容。
(视频大小为6MB(小于15MB的最大值),持续时间为40秒(小于60秒的最大值))
请注意,这是对代码的翻译部分,如有需要,请继续使用这个翻译,或者如果需要其他帮助,请告诉我。
<details>
<summary>英文:</summary>
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)
</details>
# 答案1
**得分**: 1
如果您想通过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音频编解码器
<details>
<summary>英文:</summary>
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
</details>
# 答案2
**得分**: 0
我认为[instagram private api client][1]会比你的方法做得更好
[1]: https://github.com/dilame/instagram-private-api
<details>
<summary>英文:</summary>
I think [instagram private api client][1] will do better job than your approach
[1]: https://github.com/dilame/instagram-private-api
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论