Skip to content

Commit

Permalink
Fixed Bugs in main branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweetdevil144 committed Nov 10, 2023
1 parent 08130ce commit 5f024dc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
9 changes: 5 additions & 4 deletions api/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const cors = require("cors"); // Require the CORS package

const fetchResults = require("./fetchresults");
const youtube = require("./youtube");
Expand All @@ -10,6 +11,7 @@ const app = express();

const PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(cors()); // Use the CORS middleware

app.use("/static", express.static(path.join(__dirname, "static")));

Expand All @@ -22,15 +24,14 @@ app.post("/process_video", async (req, res) => {
try {
const transcripts = await youtube.getVideoCaptions(videoId);
const shorts = await fetchResults.extractShorts(transcripts.transcript);
console.log(`shorts are \n ${JSON.stringify(shorts)}`);
return res.json({ success: true, shorts });
} catch (error) {
console.log("Catched error in app.js");
console.error("An Error occured -> " + error);
console.log("Caught error in app.js");
console.error("An Error occurred -> " + error);
return res.json({ success: false, error: error.toString() });
}
});

app.listen(PORT, () => {
console.log("Server is running on port 3000");
console.log(`Server is running on port ${PORT}`);
});
17 changes: 13 additions & 4 deletions api/fetchresults.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ const analyzeCaptions = async (text) => {
{
role: "system",
content:
"You are an expert in analyzing video transcripts to identify coherent and engaging parts suitable for creating YouTube shorts. Evaluate the provided text chunks based on their clarity, relevance, and ability to stand alone as engaging content without needing external context. Identify the sections that can be turned into stand-alone YouTube shorts while ensuring they are clear, engaging, and not abruptly starting or ending. Make sure to remember all the data that is being passed and give back results based on the total data sent to you. If any error occurs, mention what the error is in short. If rate limit occurs, notify me of that too. ",
"You are an expert in analyzing video transcripts to identify coherent and engaging parts suitable for creating YouTube shorts. Evaluate the provided text chunks based on their clarity, relevance, and ability to stand alone as engaging content without needing external context. Identify the sections that can be turned into stand-alone YouTube shorts while ensuring they are clear, engaging, and not abruptly starting or ending. Make sure to remember all the daathat is being passed and give back results based on the total data sent to you. If any error occurs, mention what the error is. If you are unable to process the given video transcript at the moment, give a deatiled message why is it so ",
},
{
role: "user",
content: `From the given video transcript, identify the chunks that can best be transformed into compelling YouTube shorts and extract only 3 high quality shorts from this. Here's the text: ${text} Now extract shorts in the following JSON format: { [ { 'start_time:': float (in seconds), 'end_time': float (in seconds), 'title': string }, ... ] } The start and end timings are provided in minutes.
content: `From the given video transcript, identify the chunks that can best be transformed into compelling YouTube shorts and extract only 3 high quality shorts from this. Here's the text: ${text} Now extract shorts strictly in the following JSON format: {
"data": [
{
"start_time": start_time,
"end_time": end_time,
"title": title (in string)
},
// rest of objects
]
} The start and end timings are provided in minutes.
However. Using the provided timing, convert that necessarily into seconds when returning output. For example, 2:28 is 2 minutes and 28 seconds, which is 148 seconds so return 148 instead of 2.28. One more necessary condition should be that the extracted short time should lie between 15-20 seconds.
The difference between start_time and end_time should necessarily lie between 12 to 23 seconds. The content of video lies in provided captions, whereas the corresponding timings lie in the given start_time
`,
Expand All @@ -41,13 +50,13 @@ const analyzeCaptions = async (text) => {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
"Content-Type": "application/json",
},
},
}
);

if (response.data.choices && response.data.choices[0]) {
console.log(
"response.data.choices[0].message.content is",
JSON.parse(response.data.choices[0].message.content),
JSON.parse(response.data.choices[0].message.content)
);
return JSON.parse(response.data.choices[0].message.content);
} else {
Expand Down
40 changes: 25 additions & 15 deletions api/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ document.addEventListener("DOMContentLoaded", function () {
})
.then((response) => response.json())
.then((data) => {
console.log(`Type of data is ${typeof data}`);
console.log(data);
if (data.success) {
console.log("Success");
console.log(`data is ${JSON.stringify(data)}`);
embedVideos(data.shorts, url);
embedVideos(data.shorts[0], url);
} else {
console.log("An Error occurred in script.js in fetching shorts");
}
Expand All @@ -26,22 +27,31 @@ document.addEventListener("DOMContentLoaded", function () {
});
});

function embedVideos(shorts, videoId) {
function embedVideos(data, videoId) {
console.log("Going Good in embedVideos");
const container = document.querySelector(".videos-container");
container.innerHTML = "";
shorts[0].data.forEach((short, index) => {
const { start_time, end_time } = short;
console.log("Start :" + start_time + " End: " + end_time);

console.log(
`Shorts recieved in embedVideos is ${JSON.stringify(
data.data
)} \n data length is ${data.data.length}`
);
for (let i = 0; i < data.data.length; i++) {
const { start_time, end_time, title } = data.data[i];
console.log(
"Start: " + start_time + " End: " + end_time + " Title: " + title
);
const embedCode = `<iframe allowFullScreen="allowFullScreen"
src="https://www.youtube.com/embed/${videoId}?ecver=1&amp;iv_load_policy=3&amp;rel=0&amp;showinfo=0&amp;yt:stretch=16:9&amp;autohide=1&amp;color=red&amp;start=${Math.round(
src="https://www.youtube.com/embed/${videoId}?start=${Math.round(
start_time
)}&amp;end=${end_time}&amp"
width="260"
height="140"
allowtransparency="true"
>
</iframe>`;
)}&end=${Math.round(end_time)}&autoplay=0&mute=1"
width="260"
height="140"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>`;
container.innerHTML += embedCode;
});
}
}
}
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"axios": "^1.5.1",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2"
}
Expand Down

0 comments on commit 5f024dc

Please sign in to comment.