|
| 1 | +# Image and Video Generation API |
| 2 | + |
| 3 | +This implementation is based on captured network requests from Meta AI's web interface for image and video generation. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Image Generation**: Generate images from text prompts with custom orientation |
| 8 | +- **Video Generation**: Generate videos from text prompts |
| 9 | +- **Cookie-based Authentication**: Uses browser cookies from Meta AI |
| 10 | +- **Multipart Response Parsing**: Handles Meta AI's multipart/mixed responses |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Make sure you have the package installed: |
| 15 | + |
| 16 | +```bash |
| 17 | +pip install -e . |
| 18 | +``` |
| 19 | + |
| 20 | +## Configuration |
| 21 | + |
| 22 | +1. Copy `.env.example` to `.env`: |
| 23 | + |
| 24 | + ```bash |
| 25 | + cp .env.example .env |
| 26 | + ``` |
| 27 | + |
| 28 | +2. Get your Meta AI cookies: |
| 29 | + - Open browser and go to https://meta.ai |
| 30 | + - Open Developer Tools (F12) |
| 31 | + - Go to Application/Storage > Cookies > https://meta.ai |
| 32 | + - Copy the values for: `datr`, `abra_sess`, `dpr`, `wd`, `c_user`, `xs`, `fr` |
| 33 | + |
| 34 | +3. Add cookies to `.env` file: |
| 35 | + ```env |
| 36 | + META_AI_DATR=your_datr_value |
| 37 | + META_AI_ABRA_SESS=your_abra_sess_value |
| 38 | + META_AI_DPR=your_dpr_value |
| 39 | + META_AI_WD=your_wd_value |
| 40 | + META_AI_C_USER=your_c_user_value |
| 41 | + META_AI_XS=your_xs_value |
| 42 | + META_AI_FR=your_fr_value |
| 43 | + ``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +### Image Generation |
| 48 | + |
| 49 | +```python |
| 50 | +from metaai_api import MetaAI |
| 51 | + |
| 52 | +# Initialize with cookies |
| 53 | +cookies = { |
| 54 | + 'datr': 'your_datr_value', |
| 55 | + 'abra_sess': 'your_abra_sess_value', |
| 56 | + 'dpr': '1', |
| 57 | + 'wd': '1920x1080' |
| 58 | +} |
| 59 | + |
| 60 | +ai = MetaAI(cookies=cookies) |
| 61 | + |
| 62 | +# Generate image |
| 63 | +result = ai.generate_image_new( |
| 64 | + prompt="Astronaut in space", |
| 65 | + orientation="VERTICAL", # or "HORIZONTAL", "SQUARE" |
| 66 | + num_images=1 |
| 67 | +) |
| 68 | + |
| 69 | +if result['success']: |
| 70 | + for url in result['image_urls']: |
| 71 | + print(f"Image URL: {url}") |
| 72 | +``` |
| 73 | + |
| 74 | +### Video Generation |
| 75 | + |
| 76 | +```python |
| 77 | +from metaai_api import MetaAI |
| 78 | + |
| 79 | +# Initialize with cookies |
| 80 | +cookies = { |
| 81 | + 'datr': 'your_datr_value', |
| 82 | + 'abra_sess': 'your_abra_sess_value', |
| 83 | + 'dpr': '1', |
| 84 | + 'wd': '1920x1080' |
| 85 | +} |
| 86 | + |
| 87 | +ai = MetaAI(cookies=cookies) |
| 88 | + |
| 89 | +# Generate video |
| 90 | +result = ai.generate_video_new( |
| 91 | + prompt="Astronaut floating in space" |
| 92 | +) |
| 93 | + |
| 94 | +if result['success']: |
| 95 | + for url in result['video_urls']: |
| 96 | + print(f"Video URL: {url}") |
| 97 | +``` |
| 98 | + |
| 99 | +## Testing |
| 100 | + |
| 101 | +Run the test script: |
| 102 | + |
| 103 | +```bash |
| 104 | +python test_generation.py |
| 105 | +``` |
| 106 | + |
| 107 | +This will test both image and video generation using the cookies from your `.env` file. |
| 108 | + |
| 109 | +## API Details |
| 110 | + |
| 111 | +### Endpoint |
| 112 | + |
| 113 | +- **URL**: `https://www.meta.ai/api/graphql` |
| 114 | +- **Method**: POST |
| 115 | +- **Doc ID**: `904075722675ba2c1a7b333d4c525a1b` |
| 116 | + |
| 117 | +### Image Generation |
| 118 | + |
| 119 | +**Operation**: `TEXT_TO_IMAGE` |
| 120 | + |
| 121 | +**Parameters**: |
| 122 | + |
| 123 | +- `prompt`: Text description of the image |
| 124 | +- `orientation`: Image orientation (`VERTICAL`, `HORIZONTAL`, `SQUARE`) |
| 125 | +- `numImages`: Number of images to generate |
| 126 | + |
| 127 | +### Video Generation |
| 128 | + |
| 129 | +**Operation**: `TEXT_TO_VIDEO` |
| 130 | + |
| 131 | +**Parameters**: |
| 132 | + |
| 133 | +- `prompt`: Text description of the video |
| 134 | + |
| 135 | +### Request Structure |
| 136 | + |
| 137 | +Both operations use similar request structure: |
| 138 | + |
| 139 | +```json |
| 140 | +{ |
| 141 | + "doc_id": "904075722675ba2c1a7b333d4c525a1b", |
| 142 | + "variables": { |
| 143 | + "conversationId": "uuid", |
| 144 | + "content": "Imagine/Animate <prompt>", |
| 145 | + "imagineOperationRequest": { |
| 146 | + "operation": "TEXT_TO_IMAGE" | "TEXT_TO_VIDEO", |
| 147 | + "textToImageParams": { |
| 148 | + "prompt": "<prompt>", |
| 149 | + "orientation": "VERTICAL" (for images) |
| 150 | + } |
| 151 | + }, |
| 152 | + ... |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +## Response Format |
| 158 | + |
| 159 | +The API returns responses in multipart/mixed format or JSON. The implementation automatically: |
| 160 | + |
| 161 | +- Parses multipart responses |
| 162 | +- Extracts media URLs (images/videos) |
| 163 | +- Returns structured data |
| 164 | + |
| 165 | +## Notes |
| 166 | + |
| 167 | +- Cookies may expire and need to be refreshed periodically |
| 168 | +- The API requires valid Meta AI cookies for authentication |
| 169 | +- Generated content follows Meta AI's usage policies |
| 170 | +- Network captures show this is the same API used by meta.ai web interface |
| 171 | + |
| 172 | +## Troubleshooting |
| 173 | + |
| 174 | +1. **Authentication Errors**: Make sure your cookies are fresh and valid |
| 175 | +2. **Missing URLs**: Check that the response contains the expected data structure |
| 176 | +3. **Rate Limiting**: Meta AI may rate limit requests, add delays if needed |
| 177 | + |
| 178 | +## Network Capture Details |
| 179 | + |
| 180 | +This implementation is based on analyzing captured network requests: |
| 181 | + |
| 182 | +- **network.json**: Contains video generation requests |
| 183 | +- **network-new.json**: Contains image and video generation requests |
| 184 | + |
| 185 | +Key patterns identified: |
| 186 | + |
| 187 | +- Same doc_id for both image and video generation |
| 188 | +- Different operation types (`TEXT_TO_IMAGE` vs `TEXT_TO_VIDEO`) |
| 189 | +- Consistent request structure with UUIDs and session tracking |
| 190 | +- Multipart/mixed response format for streaming results |
0 commit comments