Skip to content

Commit

Permalink
Fix issue with stream audio
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ngr31 committed Nov 3, 2022
1 parent 3e0b984 commit 6072fcd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const appStatus: IAppStatus = {
const notFound = (_req, res) => res.status(404).send('404 not found');
const shutDown = () => {
try {
execSync('killall ffmpeg');
execSync('killall ffmpeg > /dev/null 2>&1');
} catch (e) {}

process.exit(0);
Expand Down
4 changes: 2 additions & 2 deletions services/espn-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import jwt_decode from 'jwt-decode';
import _ from 'lodash';
import url from 'url';

import { getUserAgent } from './user-agent';
import { userAgent } from './user-agent';

global.WebSocket = ws;

Expand Down Expand Up @@ -252,7 +252,7 @@ class EspnHandler {
headers: {
Authorization: this.account_token.access_token,
Accept: 'application/vnd.media-service+json; version=2',
'User-Agent': getUserAgent(),
'User-Agent': userAgent,
Origin: 'https://plus.espn.com',
}
});
Expand Down
21 changes: 17 additions & 4 deletions services/launch-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { tmpPath } from './init-directories';
import { sleep } from './sleep';
import { espnHandler } from './espn-handler';
import { killChildren } from './kill-processes';
import { getUserAgent } from './user-agent';
import { userAgent } from './user-agent';

const VALID_RESOLUTIONS = [
'720p60',
'720p',
'540p',
];

const getStreamProfile = () => {
const getStreamVideoMap = _.memoize(() => {
const setProfile = _.includes(VALID_RESOLUTIONS, process.env.STREAM_RESOLUTION) ? process.env.STREAM_RESOLUTION : '720p60';

switch (setProfile) {
Expand All @@ -29,7 +29,20 @@ const getStreamProfile = () => {
default:
return '0:20?';
}
};
});

const getStreamAudioMap = _.memoize(() => {
const setProfile = _.includes(VALID_RESOLUTIONS, process.env.STREAM_RESOLUTION) ? process.env.STREAM_RESOLUTION : '720p60';

switch (setProfile) {
case '720p60':
return '0:33?';
case '720p':
return '0:25?';
default:
return '0:21?';
}
});

let checkingStream = {};

Expand Down Expand Up @@ -59,7 +72,7 @@ const startChannelStream = async (channelId: string, appStatus, appUrl) => {
fs.writeFileSync(path.join(tmpPath, `${channelId}/${channelId}.m3u8`), currentM3u8, 'utf8');

const out = fs.openSync(path.join(tmpPath, `${channelId}-log.txt`), 'a');
const child = spawn(path.join(process.cwd(), 'stream_channel.sh'), [], {env: {CHANNEL: channelId, URL: url, AUTH_TOKEN: authToken, APP_URL: appUrl, STREAM_PROFILE: getStreamProfile(), USER_AGENT: getUserAgent()}, detached: true, stdio: ['ignore', out, out]});
const child = spawn(path.join(process.cwd(), 'stream_channel.sh'), [], {env: {CHANNEL: channelId, URL: url, AUTH_TOKEN: authToken, APP_URL: appUrl, VIDEO_MAP: getStreamVideoMap(), AUDIO_MAP: getStreamAudioMap(), USER_AGENT: userAgent}, detached: true, stdio: ['ignore', out, out]});

appStatus.channels[channelId].pid = child.pid;

Expand Down
5 changes: 3 additions & 2 deletions services/user-agent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const getUserAgent = () => userAgents[Math.floor(Math.random() * userAgents.length)];

const userAgents = [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
Expand All @@ -9,3 +7,6 @@ const userAgents = [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
];

// Will generate one random User Agent for the session
export const userAgent = (() => userAgents[Math.floor(Math.random() * userAgents.length)])();
12 changes: 8 additions & 4 deletions stream_channel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
ffmpeg \
-user_agent "${USER_AGENT}" \
-headers "Authorization: ${AUTH_TOKEN}" \
-protocol_whitelist file,http,https,tcp,tls,crypto \
-protocol_whitelist http,https,tcp,tls,crypto \
-i "${URL}" \
-tune zerolatency \
-map ${STREAM_PROFILE} \
-c copy \
-map ${VIDEO_MAP} \
-c:v copy \
-map ${AUDIO_MAP} \
-c:a copy \
-f 'hls' \
-hls_time 2 \
-hls_segment_type mpegts \
-hls_base_url "${APP_URL}/channels/${CHANNEL}/" \
-hls_flags append_list+omit_endlist \
-hls_segment_filename tmp/eplustv/${CHANNEL}/%09d.ts tmp/eplustv/${CHANNEL}/${CHANNEL}.m3u8

0 comments on commit 6072fcd

Please sign in to comment.